-->
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.  [ 13 posts ] 
Author Message
 Post subject: Mapping dilemma
PostPosted: Tue Aug 09, 2005 4:03 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Hello. I have one parent table, one child table and 3 reference/lookup tables (see diagram below).

What is a good to set the Parent object in Hibernate (i.e. mapping)? I'll be doing basic CRUD operations on the Parent object.

Also, would you use createQuery/HQL at all like it is demonstrated in the Weblog example in the Hibernate 3 reference guide?

Image


Top
 Profile  
 
 Post subject: Re: Mapping dilemma
PostPosted: Tue Aug 09, 2005 4:13 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
biguniverse wrote:
Hello. I have one parent table, one child table and 3 reference/lookup tables (see diagram below).

What is a good to set the Parent object in Hibernate (i.e. mapping)? I'll be doing basic CRUD operations on the Parent object.

Also, would you use createQuery/HQL at all like it is demonstrated in the Weblog example in the Hibernate 3 reference guide?

Image


I would map the reference objects as <many-to-one>'s and use the Criteria API since this is a very simple/straightforward model.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:23 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thanks.

How would you map the Parent/Child (1...0.*) relationship?

Perhaps, the parent having a <bag> for it's children and the Child having <many-to-one> mapping back to the parent (i.e. bi-directional)?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:26 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
biguniverse wrote:
Thanks.

How would you map the Parent/Child (1...0.*) relationship?

Perhaps, the parent having a <bag> for it's children and the Child having <many-to-one> mapping back to the parent (i.e. bi-directional)?


The documentation indicates that Bags give good performance and do mention it in several examples so I don't think you could go wrong with that choice.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:31 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Actually, the documentation says that <bag> is a pretty bad choice.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:35 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
christian wrote:
Actually, the documentation says that <bag> is a pretty bad choice.


Christian,

I'm certainly not going to disagree with you but I gave that advice based on Section 20.5.3 Bags and Lists are the most efficient inverse collections.

The example provided in this section seems to exactly mimic his example.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 4:58 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thanks, Preston.

1. Christian, how would you map this, if <bag> is a bad choice?

I too had the impression that <bag> wasn't a bad choice based on the hibernate reference.


2. Also, is using createQuery with HQL as demonstrated in the Weblog sample application (also, in hibernate 3 reference), a good design? If you just need all rows back, won't the mapping (with lazy=false) automatically load the Child collection in my Parent class?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 5:21 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I said <bag>, not Bag. Please read the "Performance" chapter again.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 8:13 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
I would do that with Set it performs pretty well

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="model">

    <class name="Parent" table="PARENT">
        <id name="parentId" column="PARENTID" type="java.lang.Integer">
            <generator class="native"/>
        </id>

        <property name=".." column="...." type="java.lang.String" />
       
      <set
         name="children"          cascade="all-delete-orphan" inverse="true" >
          <key column="PARENT_ID"/>
          <one-to-many class="Parent"/>
      </set> 
      
    </class>
   
</hibernate-mapping>


on child maps define many-to-one relationship to parent

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 9:35 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thanks.

And, for the reference objects, as Preston (above) suggested, would you also go with as <many-to-one>'s with use the Criteria API?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 12:42 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
biguniverse wrote:
Thanks.

And, for the reference objects, as Preston (above) suggested, would you also go with as <many-to-one>'s with use the Criteria API?


I will and I do. In our application we have only few places where we are using HQL and probably we will convert them to Criteria later. All of the rest is built with Criteria API. Suggested mapping actually works perfectly for us. Your case is kind of standard in our app. Approx 80 % of our classes have exactly the same model like yours: parent/child plus lots of refereces mapped with many-to-one.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 1:44 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thanks. By the way, if you have a sample XML file that shows "parent/child plus lots of refereces mapped with many-to-one", that would be awesome :-)

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 2:54 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
biguniverse wrote:
Thanks. By the way, if you have a sample XML file that shows "parent/child plus lots of refereces mapped with many-to-one", that would be awesome :-)

Thanks again.


You are welcome

It is a simplified mapping:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="model.data">

   <class name="Customer" table="CUSTOMER">
      <id name="customerId" column="CUSTOMER_ID"
         type="java.lang.Integer">
         <generator class="native" />
      </id>

      <property name="firstName" column="FIRST_NAME" type="java.lang.String" />
      <property name="lastName" column="LAST_NAME" type="java.lang.String" />
      <property name="middleName" column="MIDDLE_NAME" type="java.lang.String" />
            
      <many-to-one name="nameSuffix" column="NM_SFX_SCODE_ID" class="model.Systemcode" />
      <many-to-one name="namePrefix" column="NM_PFX_SCODE_ID" class="model.Systemcode" />
      <many-to-one name="maritalStatus" column="MRTL_STS_SCODE_ID" class="model.Systemcode" />
      <many-to-one name="language" column="LANGUAGE_ID" class="model.Language" />
      <many-to-one name="customerStatus" column="STATUS_CODE_ID" class="model.Systemcode" />

      <set name="phones" lazy="true"
         cascade="all-delete-orphan" inverse="true">
         <key column="CUSTOMER_ID" />
         <one-to-many class="Phone" />
      </set>

      <set name="addresses" lazy="true"
         cascade="all-delete-orphan" inverse="true">
         <key column="CUSTOMER_ID" />
         <one-to-many class="Address" />
      </set>

   </class>

</hibernate-mapping>



All many-to-one to referencial classes are unidirectional

_________________
Vasyl Zhabko


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