Reputation: 899
I have 2 tables vanTb and vanSiteTb. The primary key for vanTb is vId. The vanSiteTb has a composite primary key of 2 columns viz. vNum and vSiteC.
The vanTb have also 2 columns vNum and vSiteC.
There is a one to one relation between these 2 tables.
The database schema is from a legacy database, so it cannot be changed.
In hibernate, I have defined 2 entity beans viz. VanTb.java and VanSiteTb.java.
For VanSiteTb.java, I have defined a new java class using @Embeddable. This class's object is defined in VanSiteTb.java using @EmbeddedId.
Now, I want to define a one-one relationship between these 2 tables.
So,
@Entity
@Table(name = "<name>", schema = "<name>")
public class vanTb implements Serializable{
@Id
@Column(name="vId", insertable = false, updatable = false)
private int vId;
@Column(name="vNum", insertable = false, updatable = false)
private String vNum;
@Column(name="vSiteC", insertable = false, updatable = false)
private String vSiteC;
@OneToOne
@JoinColumn(???)
private vanSiteTb v;
}
@Embeddable
public class PmKey implements Serializable {
@Column(name = "vNum", insertable = false, updatable = false)
private String vNum;
@Column(name = "vSiteC", insertable = false, updatable = false)
private String vsiteC;
....
getter and setter for both properties.
...
}
@Entity
@Table(name = ".vSiteTb", schema="")
public class vSiteTb implements Serializable {
@EmbeddedId
private PmKey pmKey;
@Column(name = "vNum", insertable = false, updatable = false)
private String vNum;
@Column(name = "vSiteC", insertable = false, updatable = false)
private String vSiteC;
public PmKey getPmKey() {
return pmKey ;
}
public void setPmKey (PmKey pmKey ) {
this.pmKey = pmKey ;
}
public String getVNum() {
return vNum;
}
public void setVNum(String vNum) {
this.vNum = vNum;
}
public String getVSiteC() {
return vSiteC;
}
public void setVSiteC(String vSiteC) {
this.vSiteC = vSiteC;
}
}
Now to define one-one relationship, I have to write @OneToOne in JPA, but what should be written for @JoinColumn, where I have mentioned ???.
To identify a unique entry from both tables, we should have vNum and vSite of both tables should match with each other.
select * from vanTb v, vSiteTb site where
v.vNum = site.vNum and v.SiteC = site.SiteC
and v.vId = 1234
The above query will return a unique row from both tables.
Please let me know the solution.
Upvotes: 0
Views: 1089
Reputation: 21113
To specify the @OneToOne
join columns inside vanTb
to vanSiteTb
, it would be:
@JoinColumns({
@JoinColumn(name = "vNum", referencedColumnName = "vNum"),
@JoinColumn(name = "vSiteC", referencedColumnName = "vSiteC")
})
If you had a column for the vanTab
's primary key vId
in vanSiteTb
, you would map the inverse of that back to the vanTab
like this:
@JoinColumn(name = "vId", referencedColumnName = "vId")
Just remember that name
represents the column name in the current table of the entity where you're adding the join column annotation and that referencedColumnName
represents the name of the column in the associated entity's table.
Upvotes: 1