OneToOne Mapping with hibernate/JBoss/Seam
I want to create a one to one mapping between the Entity Customer and OptIn. The OptIn Entity is optional. That is why the foreign key must be in OptIn. At deployment I get the following error because the mapping can not be found:
OneToOneSecondPass.java:135
Values: otherSide= optIn, mappedBy=customer
otherSideProperty = BinderHelper.findPropertyByName( otherSide, mappedBy );
java.lang.NullPointerException at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135) at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115) ...
What can I do to get a correct mapping?
@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")
p开发者_JAVA百科ublic OptIn getOptIn() {
return optIn;
}
public void setOptIn(OptIn optIn) {
this.optIn = optIn;
}
}
I'm not sure what you mean by "foreign key must be in OptIn". You've mapped your @OneToOne
association via @PrimaryKeyJoinColumn
which means your entities will be linked via their ID values. It also means that:
- You can't specify column names within
@PrimaryKeyJoinColumn
annotation; they will be taken from appropriate@Id
columns on both entities instead. - Marking fetch as
LAZY
is pointless and going to be ignored; optional @OneToOne associations are always eagerly fetched. - The only way
OptIn
would be optional on this association is if there was no entry with given ID in the database.
What Hibernate / Annotations versions are you using? If they're rather old it could be a bug in Hibernate code. But I believe it should work if you fix (1) and (2) above.
精彩评论