在我的测试项目中,我尝试做以下工作:
@Entity
@IdClass(PersonId.class)
public class Person {
@Id
private String name;
@Id
@ManyToOne
private Home home;
public class PersonId implements Serializable {
private String name;
private HomeId home;
@Entity
@IdClass(HomeId.class)
public class Home {
@Id
private String address;
@Id
private String bed;
@OneToMany(mappedBy = "home")
private List<Person> people = new LinkedList<>();
public class HomeId implements Serializable {
private String address;
private String bed;
我试图存放房屋,然后是房屋的人:
Home home = new Home();
home.setAddress("address");
home.setBed("bed");
entityManager.persist(home);
Person person = new Person();
person.setName("name");
person.setHome(home);
entityManager.persist(person);
家庭得到的保存是正确的,但当涉及到一个人时,我得到以下例外:
org.springframework.orm.jpa.JpaSystemException: No part of a composite identifier may be null; nested exception is org.hibernate.HibernateException: No part of a composite identifier may be null
我怎样才能让它发挥作用?请添加任何想法,任何赞赏!