我想創建Entity Customer和OptIn之間的一對一映射。 OptIn實體是可選的。 這就是爲什麼外鍵必須在OptIn中。在部署我得到以下錯誤,因爲 映射無法找到:休眠/ JBoss/Seam的OneToOne映射
OneToOneSecondPass.java:135
值: 阿瑟賽德=選擇啓用,的mappedBy =客戶
otherSideProperty = BinderHelper.findPropertyByName(阿瑟賽德,mappedBy);
顯示java.lang.NullPointerException 在org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135) 在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130) 在org.hibernate作爲.cfg.AnnotationConfiguration.secondPassCompile在org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115) (AnnotationConfiguration.java:296) ...
我能做些什麼,以獲得正確的映射?
@Entity
@Table(name = "KPS_OPT_IN", schema = "EB")
public class OptIn extends KmsEntity implements java.io.Serializable {
private static final long serialVersionUID = -8818445355079384264L;
private int id; /* kps_kunden_nr */
private Customer customer;
public OptIn() {
}
@Id
@Column(name = "KPS_KUNDEN_NR", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@OneToOne
@PrimaryKeyJoinColumn(name="KPS_KUNDEN_NR", referencedColumnName="KPS_KUNDEN_NR")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
this.setId(customer.getId());
}
}
@Entity
@Table(name = "KPS_KUNDEN", schema = "EB")
public class Customer extends KmsEntity implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private int id;
private OptIn optIn;
public Customer() {
}
public Customer(int id) {
this.id = id;
}
@Id
@GeneratedValue(generator="seqkpskunde")
@SequenceGenerator(name="seqkpskunde",sequenceName="SEQ_KPS_KUNDE")
@Column(name = "KPS_KUNDEN_NR", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
if(optIn!=null){
optIn.setId(id);
}
}
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "customer")
public OptIn getOptIn() {
return optIn;
}
public void setOptIn(OptIn optIn) {
this.optIn = optIn;
}
}
非常感謝,迄今爲止。我明天會試試這個。 您的意思是,如果密鑰/ ID不在這兩個表中,OptIn只是可選的? – 2009-10-28 17:42:28
客戶是這種關係的所有者。所以如果你有pk = 314的客戶和pk = 314的OptIn,那麼關係就在那裏。如果您沒有pk = 314的OptIn,則Customer.optIn屬性將爲NULL。 – ChssPly76 2009-10-28 17:46:22
不幸的是,如果連接的id是來自OptIn(mappedBy =「customer」)的引用,那麼我不能工作,只有當我在Customer中使用id時。我使用的是hibernate 3.2.4sp1和註釋3.3.0GA。 使用客戶ID的解決方案對我來說是令人滿意的。 – 2009-10-29 08:58:09