如果您知道父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可以实现吗?