-->
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.  [ 4 posts ] 
Author Message
 Post subject: parent/child association with property-ref and where clause
PostPosted: Wed Sep 20, 2006 3:56 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
Hi,

I'm working with a content management system that is limited in the sense that it does not create proper relationships between tables, i.e. foreign key constraints to primary/unique keys.

Each table has a database id, a content id and a status field which describes if a particular row (i.e. the content) has been deleted. Relationships between tables can be built using the content id and by checking the status of a particular row.

I'm currently trying to build a parent/child relationship using the "where" attribute on the class and set elements. I'm also using the "property-ref" attribute to build the key for my set of children.

Can anyone advise me if I am heading along the right track?

Also when i set the fetch="join" i seem to get duplicate elements (equal to the number of children that exist) when i query for all parents. Setting fetch="select" works but causes two selects to be executed. Is there a way to get the join to work as it seems more efficient?

Hibernate mappings are as follows.

Code:
<hibernate-mapping package="test.hibernate" >
   <class name="Parent" table="parent" where="status!='DELETED'">
      <id name="id" />
      <natural-id>
         <property name="cid" />
      </natural-id>
        <property name="name" />
       
        <set name="children" where="status!='DELETED'"
           fetch="join" lazy="false" >
            <key column="parent_id" property-ref="cid" />
            <one-to-many class="Child"/>
        </set>       
       
   </class>
</hibernate-mapping>

<hibernate-mapping package="test.hibernate" >
   <class name="Child" table="child" where="status!='DELETED'">
      <id name="id" />
      <natural-id>
         <property name="cid" />
      </natural-id>
        <property name="name" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Not sure if this helps...
PostPosted: Wed Sep 27, 2006 11:56 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
I am a newbie so this post is ment to be an exploratory step rather an answer. It seems to me that this is one of those cases where all the intelligence of parent-child mapping gets in the way. Why not just use regular queries that retrieve into a hibernate populate entity?

SQLQuery query = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery("select {c.*} from Children as c where c.parent_id=:id and c.status!='DELETED'");
query.addEntity("c", Children.class);
query.setInteger("id", cid);

List results = query.list();

Let me know what you think :)

Marius Seritan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 12:16 am 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:23 am
Posts: 27
Because its not as cool. Haha.

I guess I'd just prefer it if hibernate could just do everything for me for the following reason:
1. less work for me
2. ability to retrieve everything in a single call

Thanks for the suggestion though. I will probably end up doing it that way anyhow.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 9:10 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
I hear you, the cool factor is what attracted me to Hibernate. But sometimes it seems to get in the way. Even the approach above is MUCH nicer than jdbc.

Good luck!

Marius


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