我想創建一個簡單的JPA 2.0樣式單向映射。對象模型簡單JPA 2.0簡單@OneToOne映射插入失敗
一個用戶可以有一個地址。
@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([email protected])
的錯誤是非常基本的,並指出該用戶ID值是沒有得到的地址記錄集。好像有足夠的信息讓持久性管理者在這裏完成這個簡單的事務。映射中出了什麼問題?
顯然,持久性管理器不喜歡子表中的外鍵映射到相應的對象。由於這種關係是單向的,它只能從父母到孩子實施導航。因此讓外鍵不暴露在孩子身上將確保沒有方法直接找到和/或直接導航孩子而不是父母。 所以在上面的例子中,所有需要的是刪除 @Column(name =「userid」) private long userId; public long getUserId(){ return userId; } public void setUserId(long userId){this.userId = userId; } – user1796571
好吧,刪除讓我通過插入,但它實際上沒有用。 user_id不會保留在地址表中。所以實際上這是作爲一個空值。 – user1796571