這是我的實體類作者JPA @OneToOne關係
@Entity
@Column(name = "ID")
private Long id;
@Column(name = "LAST1")
private String last;
@Column(name="FIRST1")
private String first;
@Lob
@Column(name = "BIO")
private String bio;
@OneToOne
private AuthorDetail authorId;
個getter和setter &零參數的構造函數
,這是我的其他實體AuthorDetail這裏我使用@OneToOne映射(可選= false,mappedBy =「authorDetail」)
@Column(name = "ID")
private Long id;
@Column(name = "ADDRESS1")
private String address1;
@Column(name="ADDRESS2")
private String address2;
@Column(name = "CITY")
private String city;
@Column(name = "STATE1")
private String state;
@Column(name = "ZIP")
private String zip;
@Column(name = "START_DATE")
@Temporal(TemporalType.DATE)
private Date startDate;
@Lob
@Column(name = "NOTES")
private String notes;
@OneToOne(optional = false,mappedBy = "authorDetail")
private Author authorId;
getter和setter 這是我的主類
`EntityTransaction entr=em.getTransaction();
entr.begin();
Author author=new Author();
author.setFirst("MD");
author.setLast("RAHMATH");
author.setBio("A Software Developer");
Set detailSet=new HashSet<AuthorDetail>();
AuthorDetail detail=new AuthorDetail();
detail.setAddress1("Address1");
detail.setAddress2("Address2");
detail.setCity("NoMansLand");
detail.setState("ZZ");
detail.setZip("12345");
detail.setNotes("This is test detail");
detailSet.add(detail);
em.persist(author);
entr.commit();`
我得到異常,如果我嘗試如果你想有一個單向關係運行程序
您使用的是detailSet什麼設置?你不應該使用新的細節設置作者的authId嗎?請同時發佈你得到的例外,因爲他們幫助指出問題 – Chris