这是今晚尝试让JPA / Hibernate在新项目中工作的第43个障碍。

当尝试创建然后持久保存我的Staffer类时,我得到:

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


Staffer包含一个Person,一个Office和一个Location,其中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("[email protected]");

    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    |       |
+--------------------------+--------------+------+-----+---------+-------+

最佳答案

在我看来Staffer确实是Person的子类。我将以这种方式建模(Hibernate会更快乐)。可能使用每个子类的表设置。

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

10-06 15:34