-->
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: Many-to-many relationship help from a newbie.
PostPosted: Sun Jan 15, 2006 12:49 am 
Newbie

Joined: Sun Jan 15, 2006 12:43 am
Posts: 1
I need help getting many to many relation to work.
my mapper file looks like


<hibernate-mapping>
<class name="com.it.cc.community.domain.ContactsDirectory" table="contactsdirectory">
<id name="directoryID" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="directoryName" column="directoryname" type="string" />
<property name="directoryDescription" column="directorydescription" type="string" />
<property name="isGroupDirectory" column="isgroupdirectory" type="boolean" />

<list name="directoryContactMembers" table="directorytoperson" cascade="all" lazy="true">
<key column="directoryid" not-null="true"/>
<list-index column="dpid" />
<many-to-many column="personid" class="com.it.cc.community.domain.Person" unique="true" fetch="select"/>

</list>

</class>
</hibernate-mapping>

code to pull the the directory class with the person class that i use is as follows


Session session = SessionFactory.currentSession();
Criteria crit = session.createCriteria(getTargetClass());
crit.add(Expression.eq(session.getSessionFactory().getClassMetadata(getTargetClass()).getIdentifierPropertyName(),primaryKey));
//crit.setFetchMode("person", FetchMode.SELECT);
ContactsDirectory directory = (ContactsDirectory) crit.list().get(0);
Hibernate.initialize(directory.getDirectoryContactMembers());
//directory.setDirectoryContactMembers(ValueHandlerFactory.getValueHandlerFactory().getDirectoryValueHandler(this, directory, directory.getDirectoryID()));
SessionFactory.closeSession();
return directory;


my directorytoperson table looks like
directoryid, personid, dpid (primarykey)
all of type mediumint, and this is using mysql.

My problem is that when i call this code, i get like 70 null objects, and 1 which is the actual object to be retrieved. What am i doing wrong? why do i keep pulling up so many null objects?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 15, 2006 8:52 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hi!

The reason for this behaviour is in the use of the 'list' tag.
I bet, that the index property dpid has a value around 70. When you define a mapping as list with a list-index column, the value is stored at the index position specified in the list-index column. If the list-index is 70 for one element, this element is positioned at index 70; all other elements in the list/array are left empty (i.e. 'null').

I tend to use ordered sets with a natural order based on the supposed list-index column.

All the best,

René


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.