-->
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: 2 Classes generated per table
PostPosted: Fri May 05, 2006 10:41 am 
Newbie

Joined: Fri May 05, 2006 10:28 am
Posts: 3
Hey list,

I am generating mapping files and POJOs from an Oracle 10g database.
Some tables result in 2 classes, one has "Id" appended to it. What is the significance of this ? which class do I use as my domain object ?
I probably generates the 2 files when it comes across a table with a composite PK, but I want to understand the significance of this.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 05, 2006 12:17 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I rarely generated mapping and pojo files using the respective tools available.

But generating an ID class for composite key makes sense and I always have classes implemented in this way. And if you observe the ID class would implement Serializable interface as this is requirement for any ID class which contains elements for composite key.

For example, with the below example mapping file, the classes are given further down.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="learn.hibernate">
   <class name="MyComposite" table="My_Composite_Table">
      <composite-id name="myCompositeId" class="MyCompositeId">
         <key-property name="propertyA" column="prop_a"/>
         <key-property name="propertyB" column="prop_a"/>
         <key-property name="propertyC" column="prop_a"/>
      <composite-id>

      <property name="propertyD" column="prop_d"/>
   </class>
</hibernate-mapping>


Code:
public class MpComposite {
   private MyCompositeId myCompositeId;
   private String propertyD;

   // getters and setters
}

public class MyCompositeId implements Serializable {

   private String propertyA;
   private String propertyB;
   private String propertyC;

   //getters and setters for above properties...

}


The second approach, I sometimes follow, is to have one class extend other class with composite key fields.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping  package="learn.hibernate">
   <class name="MyComposite" table="My_Composite_Table">
      <composite-id>
         <key-property name="propertyA" column="prop_a"/>
         <key-property name="propertyB" column="prop_a"/>
         <key-property name="propertyC" column="prop_a"/>
      <composite-id>

      <property name="propertyD" column="prop_d"/>
   </class>
</hibernate-mapping>


Code:
public class MpComposite extends MyCompositeId {

   private String propertyD;

   // getters and setters
}

public class MyCompositeId implements Serializable {

   private String propertyA;
   private String propertyB;
   private String propertyC;

   //getters and setters for above properties...

}


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.