JPA复合键问题:列'Person_ID'不能为空

时间:2011-02-11 05:24:02

标签: java hibernate jpa

这是今晚43号的障碍,试图让JPA / Hibernate在一个新项目中工作。

在尝试创建然后保留我的Staffer课程时,我得到:

SEVERE: Column 'Person_ID' cannot be null
SEVERE: Could not synchronize database state with session

Staffer包含PersonOfficeLocation,其中Person是主键。我手动创建每个这些实体,持久化它们,附加它们,验证它们不是null,它们的id不为null,然后保持我的Staffer

我无法弄清楚为什么当我试图坚持时,JPA / Hibernate抱怨一个(显然)空人字段:

    EntityManager em = EMF.get().createEntityManager();
    em.getTransaction().begin();
    Location location = new Location();
    location.setCity("my city");
    location = em.merge(location);
    assertNotNull(location.getId());

    Office office = new Office();
    office.setName("my office");
    office.setLocation(location);
    office = em.merge(office);
    assertNotNull(office.getId());

    Person person = new Person();
    person.setFirstName("first");
    person.setLastName("last");
    person = em.merge(person);
    assertNotNull(person.getId());

    Staffer staffer = new Staffer();
    staffer.setPerson(person);
    staffer.setCellPhone("555-555-5555");
    staffer.setOffice(office);
    staffer.setHomeLocation(location);
    staffer.setPersonalEmail("me@home.com");

    assertNotNull(staffer.getPerson());
    assertNotNull(staffer.getPerson().getId());
    staffer = em.merge(staffer);  // CODE FAILS HERE
    em.getTransaction().commit();

这是我的型号代码:

@Entity
@Table(name = "Staffer")
public class Staffer implements Serializable
{
    @Id
    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "Person_ID")
    private Person person;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "Office_ID")
    private Office office;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "Home_Address_Location_ID")
    private Location homeLocation;   

    @Column(name = "Home_Phone")
    private String homePhone;

    @Column(name = "Cell_Phone")
    private String cellPhone;

    @Column(name = "Personal_Email")
    private String personalEmail;

    @Override
    public boolean equals(Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Staffer staffer = (Staffer) o;

        if (homeLocation != null ? !homeLocation.equals(staffer.homeLocation) : staffer.homeLocation != null)
            return false;
        if (office != null ? !office.equals(staffer.office) : staffer.office != null) return false;
        if (person != null ? !person.equals(staffer.person) : staffer.person != null) return false;

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = person != null ? person.hashCode() : 0;
        result = 31 * result + (office != null ? office.hashCode() : 0);
        result = 31 * result + (homeLocation != null ? homeLocation.hashCode() : 0);
        return result;
    }

    // getters & setters, etc.
}

@Entity
@Table(name = "Person")
public class Person
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Person_ID")
    private Long id;

    @Column(name = "First_Name")
    private String firstName;

    @Column(name = "Last_Name")
    private String lastName;
    // getters & setters, etc.
}

@Entity
@Table(name = "Office")
public class Office
{
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "Office_ID")
    private Long id;

    @Column(name = "Name")
    private String name;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "Location_ID")
    private Location location;

    // getters & setters, etc.
}

@Entity
@Table(name = "Location")
public class Location
{
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name = "Location_ID")
    private Long id;

    @Column(name ="City")
    private String city;

    // getters & setters, etc.
}

另外,我不确定为什么当表由Hibernate创建时,它会创建Home_Address_Location_ID和Office字段作为Staffer密钥的一部分;我的意图是只使用Person作为主键。

+--------------------------+--------------+------+-----+---------+-------+
| Field                    | Type         | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------+-------+
| Cell_Phone               | varchar(255) | YES  |     | NULL    |       | 
| Home_Phone               | varchar(255) | YES  |     | NULL    |       | 
| Personal_Email           | varchar(255) | YES  |     | NULL    |       | 
| Person_ID                | bigint(20)   | NO   | PRI | NULL    |       | 
| Home_Address_Location_ID | bigint(20)   | YES  | MUL | NULL    |       | 
| Office_ID                | bigint(20)   | YES  | MUL | NULL    |       | 
+--------------------------+--------------+------+-----+---------+-------+

1 个答案:

答案 0 :(得分:0)

在我看来Staffer实际上是Person的子类。我会这样建模(Hibernate会更快乐)。可能使用每子类表设置。

其他选项包括Staffer(ugh)上的代理ID或某种嵌入式ID伏都教(没想到最后一个)。