-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to make this with hibernate annoations
PostPosted: Wed Aug 05, 2009 9:51 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
Hi,

I need to make two hibernate beans of the following ERD:

Image

Any suggestions?

Thanks,
Pieter


Top
 Profile  
 
 Post subject: Re: How to make this with hibernate annoations
PostPosted: Mon Aug 10, 2009 7:14 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
This simply looks like a mapping of a compound key for each table first, and then a one-to-many association, right?

Mapping Compound Primary Keys Tutorial:
http://jpa.ezhibernate.com/Javacode/lea ... rimarykeys

Mapping One to One Relationships with Hibernate Tutorial:
http://jpa.ezhibernate.com/Javacode/lea ... sociations

Just curious - does effective date need to be part of a compound key?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: How to make this with hibernate annoations
PostPosted: Mon Aug 10, 2009 7:38 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
Hi Cameron,

Unfortunately effective date needs to be in the primary key.
This is how Oracle HCM stores historical data.

For the moment i'm this far:

Code:
@Entity
@IdClass(CompanyEffDatePK.class)
@Table(name = "COMPANY")
public class CompanyEffDate implements EffDateInterface
{
   // ----- CONSTANTS -----
   private static final long serialVersionUID = 6055686640986901623L;

   // ----- MEMBERS -----
   private String companyId;
   private Date effDate;
   
   private String name;

   // ----- GETTER -----
   
   /**
    * @return the companyId
    */
   @Id()
   @Column(name="ID")
   public String getCompanyId()
   {
      return companyId;
   }

   /**
    * @return the effDate
    */
   @Id()
   @Column(name="EFFDT")
   public Date getEffDate()
   {
      return effDate;
   }
...



Code:
@Entity
@IdClass(AddressEffDatePK.class)
@Table(name = "COMPANY_ADDRESS")
public class AddressEffDate implements EffDateInterface
{
   // ----- CONSTANTS -----
   private static final long serialVersionUID = -3827054467502810838L;
   
   // ----- MEMBERS -----
   private String addressId;
   private Date effDate;
   
   private String companyId;
   
   private String name;
   private String street;

   // ----- GETTER -----
   
   /**
    * @return the addressId
    */
   @Id()
   @Column(name="ID")
   public String getAddressId()
   {
      return addressId;
   }

   /**
    * @return the effDate
    */
   @Id()
   @Column(name="EFFDT")
   public Date getEffDate()
   {
      return effDate;
   }

   /**
    * @return the companyId
    */
   @Column(name="COMPANY_ID")
   public String getCompanyId()
   {
      return companyId;
   }
....


You see I can’t navigate between mappings.

Pieter


Top
 Profile  
 
 Post subject: Re: How to make this with hibernate annoations
PostPosted: Mon Aug 10, 2009 9:24 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
You simply need to define a one to many on the company class:
Code:
    @OneToMany(targetEntity=AddressEffDate.class)
    @JoinColumns({
        @JoinColumn(name="COMPANY_ID", referencedColumnName = "ID"),
        @JoinColumn(name="EFFDT", referencedColumnName = "EFFDT")})
   public List<AddressEffDate> getAddresses() {
      return addresses;
   }
   public void setAddresses(List<AddressEffDate> addresses) {
      this.addresses = addresses;
   }


Top
 Profile  
 
 Post subject: Re: How to make this with hibernate annoations
PostPosted: Mon Aug 10, 2009 9:32 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
@JoinColumn(name="EFFDT", referencedColumnName = "EFFDT")})
AddressEffDate hasn't got the company EFFDT, it has a column EFFDT but it is related to the companyAddress


Top
 Profile  
 
 Post subject: Re: How to make this with hibernate annoations
PostPosted: Mon Aug 10, 2009 10:39 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If the date is part of the primary key I assume when company data changes that a new row gets inserted into the company table with the same ID but a different date. If this is the case then the ERD is wrong and the relationship is many to many. Could be why you're having difficulty mapping it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.