How do I establish table association in JPA / Hibernate with existing database?

Posted by Paperino on Stack Overflow See other posts from Stack Overflow or by Paperino
Published on 2010-05-20T21:58:39Z Indexed on 2010/05/20 22:00 UTC
Read the original article Hit count: 268

Filed under:
|
|

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 implements 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 hope so. Thanks in advanced!

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about jpa