-->
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.  [ 3 posts ] 
Author Message
 Post subject: About adding entities with null id in Sets
PostPosted: Wed Mar 24, 2004 11:47 am 
Newbie

Joined: Wed Mar 24, 2004 11:41 am
Posts: 3
Hi, i have some problem with hibernate implementing a one-to-many relationship, when i want to populate the Sep in the class that hold the set of another objects, the problem is that the object are created with id value "null" and when are added to the Set only the first object is inserted the others not, one solution that i founded is modify the equals "method" of the entity adding other propertys plus the id to make the object diferent from the already inserted in the Set (i think that this is the problem), here we attach an excerpt of the hibernate's xml definition and a fragment of the source code. Perhaps there is something wrong in definition's xml or source code, or another way to do that, and not to modify the "equals" method,

I'm using hibernate version 2.1

Best Regards and Thanks for a very good job in hibernate, Rodney

xml definition:

<class name="com.rdk.infbank.bus.UserRole" table="user_roles">
<id name="id" column="ur_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<many-to-one name="user" class="com.rdk.infbank.bus.User" column="user_id"/>
<many-to-one name="role" class="com.rdk.infbank.bus.Role" column="role_id"/>
</class>

<class name="com.rdk.infbank.bus.User" table="users">
<id name="id" column="user_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="user_name" type="string" length="15" not-null="true"/>
<property name="password" column="user_pass" type="string" length="15" not-null="true"/>
<property name="active" column="user_active" type="boolean" />
<set name="roles" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="user_id"/>
<one-to-many class="com.rdk.infbank.bus.UserRole"/>
</set>
</class>

source code:

User usr = (User) command;
usr.getRoles().clear();

for(int i=0; i<ids.length; i++) {
Role rol = (Role) rDao.loadObj(ids[i]);
UserRole ur = new UserRole();
ur.setRole(rol);
ur.setUser(usr);
ur.setRolename(rol.getName());
ur.setUsername(usr.getName());
usr.getRoles().add(ur); -> solamente se adiciona el primer elemento
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 24, 2004 12:34 pm 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
See http://www.hibernate.org/109.html.

I understand that you're using id for equals(). Then if you put multiple new objects into a set only one of them will be found in the set (because java uses equals() to distinguish between elements in the set). Instead, use some other properties for equals(). Preferably ones that won't change, e.g. the timestamp when the object was created etc.

A good idea is to use the equals() and hashCode() builders provided in the jakarta commons-lang package, they are good.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Thu Mar 25, 2004 10:47 am 
Newbie

Joined: Wed Mar 24, 2004 11:41 am
Posts: 3
The article on the hibernate web site help me understand the problem, the best solution in my opinion is rewrite the Equals and HashCode of the class, but the problem is that in several case we don't have fields in table that help us to build a reallly good Equals and HashCode method


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