These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Design Advice?
PostPosted: Fri Mar 29, 2013 4:06 pm 
Newbie

Joined: Fri Mar 29, 2013 4:00 pm
Posts: 1
Hey. I'm trying to understand how to best design a set of tables in hibernate. For simplicity, I have two classes:

Code:
public class Security {
   @Id
   @Column
   public String symbol;


   public List<Earnings> earnings;
    ...


And
Code:
public class Earnings {
   public Security security;
   public Date date;
   ...


The Security has a OneToMany relationship with the Earnings Table. The Security only needs the symbol as the uniqueid, while the Earnings table would need the Symbol of the security plus a date to ensure uniqueness.

Now the question is how best to setup the relationship with hibernate and annotations.

Is it best to create a Composite Key in the Earnings and then use a OneToMany mapping using the composite key? If so, I get circular references.

How would you map the two entities?

Thanks in advance


Top
 Profile  
 
 Post subject: Re: Design Advice?
PostPosted: Wed Apr 10, 2013 9:26 am 
Newbie

Joined: Wed Mar 13, 2013 11:32 am
Posts: 16
Hi

You can map it something like this:

Code:
public class Security {
   @Id
   @Column
   public String symbol;

   @OneToMany(mappedBy="security")
   public List<Earnings> earnings;
    ...




Code:
public class EarningsPK {
   public Security security;
   public Date date;
}


Code:
@IdClass(EarningsPK.class)
public class Earnings {

   @Id
   @OneToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="symbol", referenceColumnName="symbol")
   public Security security;

   @Id
   public Date date;
   ...



Br

Jesper


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.