Spring Data JPA保存子项而不获取父项

时间:2017-04-05 20:29:50

标签: java spring hibernate jpa spring-data

如果您知道父ID?

,如何使用Spring数据JPA存储库保存子实体

例如,如果我们有一对多的关系:

@Entity
public class Customer {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    @ManyToOne
    private CustomerCategory category;
}

@Entity
public class CustomerCategory {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String name;
}

可以做类似的事情:

CustomerCategory someCategory = customerRepository.findOne(1L);// how to skip that line. The id should be enough. 
Customer cust = new Customer("Evgeni", "Dimitrov", someCategory);           
customerRepository.save(cust);

JPA可以load(只创建代理并设置Id)而不是'get'(从数据库中选择)。 Spring Data JPA可以实现吗?

0 个答案:

没有答案