• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Composite Key Issues

 
Michael Paik
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following datamodel: (Apologies if the Kleene shorthand isn't in preferred format)

Order 1 => * Credit
Order * => 1 LoanPopulation
LoanPopulation 1 => + Loan
Loan 1 => * Credit

Now, the problem is that for a given object graph (say, I want to fetch an Order and its associated components) the Credit object is uniquely keyed on order_id and loan_id.

The net-net of this is that I can create a DAO that will fetch this object by supplying the two keys explicitly (obviously) but I cannot seem to set up a mapping that will allow Hibernate to automatically load the appropriate one because of the composite dependency (ie, I can load the Order, then the Loan Population, then the Loans, but to get Credit, I need the order_id which isn't a column in Loan that I can join on).

Any help much appreciated.
 
Mohd. Yakub
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael ,

The PK of tabel Credit is composed of both columns order-Id & loan_id.
let credit_id is surrogate primary key.For this we can use <idbag> mapping,

<idbag name="credits" table="Credit">
<collection-id type="long" column="credit_id">
<generator class="sequence"/>
</collection-id >
<key column="order_id"/>
<many-to-many class="Loan" column="load_id"/>
</idbag>
u can use other collections as well
so ,u have now order_id to get credits..
regards,
yakub
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic