How do I establish table association in JPA / Hibernate with existing database?
Currently I have two tables in my database Encounters and Referrals:
There is a one to many relationship between these two tables. Currently they are linked together with foreign keys. Right now I have
public class Encounter extends JPASupport imple开发者_如何学运维ments java.io.Serializable {
@Column(name="referralid", unique=false, nullable=true, insertable=true, updatable=true)
public Integer referralid;
}
But what I really want is
public class Encounter extends JPASupport implements java.io.Serializable {
..........
@OneToMany(cascade=CascadeType.PERSIST)
public Set<Referrals> referral;
............
}
So that I can eventually do a query like this:
List<Encounter> cases = Encounter.find(
"select distinct p from Encounter p join p.referrals as t where t.caseid =103"
).fetch();
How do I tell JPA that even though I have non-standard column names for my foreign keys and primary keys that its the object models that I want linked, not simply the integer value for the keys?
Does this make sense?
I can't tell from your example, does a referral have many encounters on it, or does an encounter have many referrals on it? You seem to go back and forth. This would be for each Referral has lots of Encounters:
public class Encounter extends JPASupport implements java.io.Serializable {
@ManyToOne
@JoinColumn(name="referralid", referencedColumnName="id",//otherstuff)
private Referral referral;
}
public class Referral extends JPASupport implements java.io.Serializable {
@OneToMany(cascade=CascadeType.PERSIST, mappedBy="referral")
private Set<Encounter> encounters;
}
精彩评论