有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

包含外部复合密钥的java JPA复合密钥失败

在我的测试项目中,我试图实现以下目标:

@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);

home get已正确保存,但对于一个人,我有以下例外:

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

我该怎么做?请添加任何想法,谢谢


共 (0) 个答案