-->
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: Showing one-to-one relationship in annotated entity class
PostPosted: Sun Feb 08, 2009 5:02 pm 
Newbie

Joined: Sat Jan 24, 2009 1:32 pm
Posts: 9
Hello,

I have inherited a database with a composite key, each table has the following:
int id
int classId

These two columns are combined as the key of almost every table in this db. I have discovered the way to correctly write the entity classes with the corresponding <class name>Id class that contains the component key. Here's an example of this that is based on the rp.batch table:
Code:
@Entity
@Table(name = "rp.batch")
public class HBatch  {
   private HBatchId key;
   public HBatch() {

   }

   // Property accessors
   @EmbeddedId

   @AttributeOverrides( {
      @AttributeOverride(name="id", column=@Column(name="id", unique=false, nullable=false, insertable=true, updatable=true) ),
      @AttributeOverride(name="classId", column=@Column(name="classId", unique=false, nullable=false, insertable=true, updatable=true) ) } )
      public HBatchId getKey() {
      return this.key;
   }
   public void setKey(HBatchId key) {
      this.key = key;
   }
...


With of course, the requisite key class:
Code:
@Embeddable
public class HBatchId implements java.io.Serializable {
   // Fields   
   private int id;
   private int classId;
...


Now in another table, rp.transfer, I have two columns that refer to the primary key of the batch table: batchId and batchClassId. I have an entity class HTransfer that refelects this (probably wrong). The data members of the HTransfer entity class with these values are below:
Code:
   @Column(name = "batchId")
   private Long batchId;

   @Column(name = "batchClassId")
   private Long batchClassId;


What I need to do know is figure out how to show the one-to-one relationship between HTransfer and HBatch. I've read the section one advanced entity association mappings in chapter 7 of Java Persistence with Hibernate but it doesn't tell me how to map a composite key. I believe that if I can do this then one of the earlier queries I've used to attempt so solve my earlier question: (http://forum.hibernate.org/viewtopic.php?p=2405460#2405460) will probably work.

Does anyone out there know how to do this?

Setup Info:
Hibernate version: Core 3.3.1 GA, Annotations 3.4.0

Mapping documents: hibernate.cfg.xml


Regards,

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2009 7:44 am 
Regular
Regular

Joined: Thu Sep 06, 2007 2:22 am
Posts: 108
Location: Noida,India
Hi,

Please refer the chapter 8 of "Java Persistence with Hibernate" for this.
Or you can use following mapping for that -


Unidirectional One-To-One association from HTransfer to HBatch

Code:
@Entity
   @Table(name="Table_Name")
   public class HTransfer{

      
      private HBatch hBatch;
      
      @ManyToOne
      @JoinColumns({@JoinColumn(name="batchId", referencedColumnName="id")
                  @JoinColumn(name="batchClassId",referencedColumnName="classId"})
      public HBatch getHBatch(){
         return hBatch;
      }

      /**
      REMOVE THE FOLLOWING MAPPING

      @Column(name = "batchId")
      private Long batchId;

      @Column(name = "batchClassId")
      private Long batchClassId;
             */
      .............
   }




Unidirectional One-To-One association from HBatch to HTransfer

Code:
@Entity
@Table(name = "rp.batch")
public class HBatch  {
   private HBatchId key;
   public HBatch() {

   }

   // Property accessors
   @EmbeddedId

   @AttributeOverrides( {
      @AttributeOverride(name="id", column=@Column(name="id", unique=false, nullable=false, insertable=true, updatable=true) ),
      @AttributeOverride(name="classId", column=@Column(name="classId", unique=false, nullable=false, insertable=true, updatable=true) ) } )
      public HBatchId getKey() {
      return this.key;
   }
   public void setKey(HBatchId key) {
      this.key = key;
   }

   private HTransfer hTransfer;

   @OneToOne
   @JoinColumns({@JoinColumn(name="batchId", referencedColumnName="id")
                  @JoinColumn(name="batchClassId",referencedColumnName="classId"})
   public HTransfer getHTransfer(){
   }
...



and if you want bidirectional association, put one side association as "mappedBy= ...."


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.