Hi all I’ve a small problem to map one-to-one using JPA 2 persistence with EclipseLink vendor and maybe any of you will be able to help.
I want to map one table with another but the field in second table is optional. So I’ve created @Entity for the first table:
Amp class
private AmpApInfo ampApInfo;
private String apid;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public String getApid() {
return this.apid;
}
public void setApid(String apid) {
this.apid = apid;
}
@OneToOne
@JoinColumn(name="apid")
public AmpApInfo getAmpApInfo() {
return this.ampApInfo;
}
public void setAmpApInfo(AmpApInfo ampApInfo) {
this.ampApInfo = ampApInfo;
}
and @Entity for the second table
AmpApInfo class
private String apid;
private Amp amp;
private String prodOrderNo;
public AmpApInfo() {
}
@OneToOne(optional=true, mappedBy="ampApInfo")
public Amp getAmp() {
return this.amp;
}
public void setAmp(Amp amp) {
this.amp = amp;
}
@Id
public String getApid() {
return apid;
}
public void setApid(String apid) {
this.apid = apid;
}
@Column(name="prod_order_no")
public String getProdOrderNo() {
return this.prodOrderNo;
}
public void setProdOrderNo(String prodOrderNo) {
this.prodOrderNo = prodOrderNo;
}
Now when I want to find prodOrderNo like
public Amp selectProdInfo(String name) {
// TODO Auto-generated method stub
Amp amp = Transactions.getEntityManager().find(Amp.class, name.trim());
System.out.println("order number" + amp.getAmpApInfo().getProdOrderNo());
return amp;
}
I’m expecting to get null cos its not there but I’m getting java.lang.NullPointerException on the line
System.out.println("order number" + amp.getAmpApInfo().getProdOrderNo());
Any one can help???
Either amp is null or amp.getAmpApInfo() is returning null, and you are trying to call methods on them.