gpt4 book ai didi

java - JPA复合键问题: Column 'Person_ID' cannot be null

转载 作者:行者123 更新时间:2023-11-29 09:22:58 25 4
gpt4 key购买 nike

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

当尝试创建并保留我的 Staffer 类时,我得到:

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

Staffer 包含一个Person、一个Office 和一个Location,其中Person 是主键。我正在手动创建这些实体中的每一个,持久化它们,附加它们,验证它们不为空,它们的 ID 不为空,然后持久化我的 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 | |
+--------------------------+--------------+------+-----+---------+-------+

最佳答案

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

其他选项包括 Staffer 上的代理 ID(呃)或某种嵌入式 ID 巫术(没想到最后一个通过)。

关于java - JPA复合键问题: Column 'Person_ID' cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4965902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com