views:

45

answers:

1

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!

+3  A: 

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;

}
Affe
Sorry for the confusion (my mind is absolute moosh right now). Yes a Referral has many encounters. Let me give this a try.
Paperino
you may also need a mappedBy="referral" on the @OneToMany, but hibernate can usually find what it needs by type
Affe
Absolute gold. Worked. Thanks for making my day slightly less hair pulling!
Paperino