我正在尝试创建一个简单的 JPA 2.0 风格的单向映射。对象模型很简单
一个用户可以有一个地址。
@Entity
@Table(name = "USERS")
public class User {
@Id
@SequenceGenerator(name="user_seq",sequenceName = "sequence",allocationSize=1)
@GeneratedValue (generator="user_seq",strategy=GenerationType.SEQUENCE)
@Column(name="userid")
private Long userId;
@Column(name="emailid")
private String emailId;
@OneToOne(cascade=CascadeType.PERSIST)
@PrimaryKeyJoinColumn
private Address address;
public User() {
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
@Entity
@Table(name="address")
public class Address {
@Id
@SequenceGenerator(name="address_seq",sequenceName = "hibernate_sequence",allocationSize=1)
@GeneratedValue (generator="address_seq",strategy=GenerationType.SEQUENCE)
private long id;
@Column(name="userid")
private long userId;
@Column(name="address")
private String completeAddress;
public Address() {
super();
}
public Address (String completeAddress) {
super();
this.completeAddress = completeAddress;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getCompleteAddress() {
return completeAddress;
}
public void setCompleteAddress(String completeAddress) {
this.completeAddress = completeAddress;
}
}
这就是我试图执行以创建新用户并设置该用户的地址。
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
User user = new User();
user.setEmailId("test123");
Address address = new Address();
address.setCompleteAddress("some address");
user.setAddress(address);
em.persist(user);
tx.commit();
} catch (Exception e) {
em.getTransaction().rollback();
} finally {
em.close();
}
这是我得到的错误 -
[EL Warning]: 2013-02-04 16:00:03.376--UnitOfWork(1827795025)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "address" violates foreign key constraint "address_userid_fkey"
Detail: Key (userid)=(0) is not present in table "users".
Error Code: 0
Call: INSERT INTO address (ID, userid, address) VALUES (?, ?, ?)
bind => [43, 0, some address]
Query: InsertObjectQuery(com.jpa.test.Address@5c001eb0)
该错误非常基本,并指出未在地址记录上设置用户 ID 值。似乎这里的持久性管理器有足够的信息来完成这个简单的事务。映射出了什么问题?