-->
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.  [ 5 posts ] 
Author Message
 Post subject: initializing a list of objects always returns 3 objects.
PostPosted: Tue Feb 06, 2007 12:58 pm 
Newbie

Joined: Thu Mar 24, 2005 10:07 am
Posts: 14
Hi, I'm trying to do a very simple thing here. I have a main object that has many children. Lists and sets. When I get my main object, i initialize my child objects one by one. All lists come back with 3 objects regardless of how many are actually in the database. The only case thats correct is if there are 0 in the DB, 0 are returned.

Sets in the exact same code work fine. I'm stuck. Anything wrong in my mapping docs?



Hibernate Version
3.2

Mapping documents:
Parent
Code:
<list name="myMessages" inverse="true">
            <key>
                <column name="TeenFitID" not-null="true" />
            </key>
            <index column="TeenFitId"/>
            <one-to-many class="com.imediainc.teenfit.datalayer.dto.MyMessages" />
        </list>



Child
Code:
<class name="com.imediainc.teenfit.datalayer.dto.MyThoughts"  table="MyThoughts">
        <id name="thoughtId" type="int">
            <column name="ThoughtID" />
            <generator class="native" />
        </id>
        <many-to-one name="teenFitUser" class="com.imediainc.teenfit.datalayer.dto.TeenFitUser" fetch="select">
            <column name="TeenFitID" not-null="true" />
        </many-to-one>
        <property name="thoughtDesc" type="text">
            <column name="ThoughtDesc" />
        </property>
        <property name="postDate" type="timestamp">
            <column name="PostDate" length="23" />
        </property>
    </class>


Code between sessionFactory.openSession() and session.close():
I have a DAO that retuns the parent object.

Code:
TeenFitUserDAO zTeenFitUserDAO = new TeenFitUserDAO(hsession);
         zTFUser = zTeenFitUserDAO.findByHandle(sHandle_p);
         
         
         log.info("Initializing Profiles - Size: " + zTFUser.getProfiles());
         log.info("Initializing AboutMe - Size: " + zTFUser.getAboutMe().size());
         //zTFUser.getMyFriends());
         log.info("Initializing MyMessages - Size: " + zTFUser.getMyMessages().size());
         log.info("Initializing MyThoughts - Size: " + zTFUser.getMyThoughts().size());



Full stack trace of any exception that occurs:
No exception occurs. It just returns 2 empty child objects and 1 hydrated child object in the list. In the database theres actually only 1 child object

Name and version of the database you are using:
SQL Server 2000

The generated SQL (show_sql=true):
Code:
11:20:04,609 INFO  [STDOUT] Hibernate: select mythoughts0_.TeenFitID as TeenFitID1_, mythoughts0_.ThoughtID as ThoughtID1_, mythoughts0_.TeenFitId as TeenFitId1_, mythoughts0_.ThoughtID as ThoughtID68_0_, mythoughts0_.TeenFitID as TeenFitID68_0_, mythoughts0_.ThoughtDesc as ThoughtD3_68_0_, mythoughts0_.PostDate as PostDate68_0_ from MyThoughts mythoughts0_ where mythoughts0_.TeenFitID=?
11:20:04,609 INFO  [TeenFitBO] Initializing MyThoughts - Size: 3



Debug level Hibernate log excerpt:
<attribute name="ShowSqlEnabled">true</attribute>


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 1:38 pm 
Newbie

Joined: Mon Sep 29, 2003 3:32 am
Posts: 6
A List actually has
- id columns (typically one)
- index columns - an integer storing the position of the element
- data columns

Remember that relations (database tables) don't store position information
so you need an extra column to get list semantics.

I guess your database does not distinguish between case for column names
so you actually mapped the id and the index attributes to the same column.

Nice one :-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 2:31 pm 
Newbie

Joined: Thu Mar 24, 2005 10:07 am
Posts: 14
Shortly after posting this I did notice the same thing, I now have a SortOrder column as the index.

That did the trick - wow can't believe i missed that. But heres why I missed it, and it bubbles up another question. Im using the DAO pattern and saving child objects 1 at a time. Now, each child object has a parent object within it.

So I fill a child obj and say child.setParent(foobar) and I save it. Works fine.

Thats very differnt than saving the parent with a full collection of children. In which case, hibernate would do a delete all children, then insert children, incrementing the index all the time.

So outside of using a set, how can i use a List with a DAO pattern?? Hope i was clear, please reply if not clear.
Thanks


[/img]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 3:39 am 
Newbie

Joined: Mon Sep 29, 2003 3:32 am
Posts: 6
So why don't you build your DAO pattern about the objects you want to model?

If it's the children, o.k..
But if the parent ist the center, so why don't you get the parent via dao, change the children you care about
and save again?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 7:11 pm 
Newbie

Joined: Thu Mar 24, 2005 10:07 am
Posts: 14
What i was asking was unclear. If i use a list within a parent object I think I have to treat that list as a whole list at all times because Hibernate manages the index column. I tried to save a single item in a list through the child DAO and it saved the index column as null.

As i understand it , hibernate saves lists as Delete all first, insert all second, keeping track of the index and incrementing all the way.

I changed my children to sets and its fine, so no worries.

But to answer your question, I could save the parent w/ modified children eveloped witnin, but that would be extra code when i could get at the single child i need through its own DAO.

Thanks


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