-->
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-one : identifyer of an instance altered problem
PostPosted: Sat Jan 22, 2005 3:21 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
I have created a unidirectional many-to-one mapping for a member address relationship. In this case we have a family unit made up of one or more family members who can share the same contact address. The member class has an contactinformation attribute of the ContactInformation class. The database has a member table with a fk field to store the primary id of the contact information stored in the Contact_information table.

I create and instance of a Member (M1) and new contact information (C1) stored in the contactiformation attribute and persist this to the database. When I create a new family member (M2) i want them to share the same contact informaiton so i use the the following method :
M2.setContact(M1.getContact());
Then save M2 to the database. Now i want to view a list of all family members so i generate the query that returns a list of the family members when i do so i get the following message in my console:
identifier of an instance of registration.ContactInformation altered from 3 to 7
And a new entry is created for M2 contact information in the database so that I have two contact information for the family and each family member point to a different one. This is not what I want as I want all family members to point to the same contact information so that if one is updated it is updated for all family members who share that contact information.
I have tried many different mappings and none seem to work.

Hibernate version: 2.1.6

Mapping documents:
<hibernate-mapping>
<class name="registration.Member" table="MEMBER">
<id name="memberID" type="int" unsaved-value="any">
<column name="member_id" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>
<many-to-one
name="contact"
column="contact_id"
cascade="save-update"
class="registration.ContactInformation"/>
<property name="guardianID">
<column name="guardian_id"/>
</property>
<property name="memberType">
<column name="member_type" />
</property>
<property name="firstName">
<column name="first_name" />
</property>
<property name="lastName">
<column name="last_name" />
</property>
<property name="email">
<column name="email" />
</property>
<property name="cellPhone">
<column name="cell_phone" />
</property>
<property name="workPhone">
<column name="work_phone" />
</property>
<property name="emergPhone">
<column name="emergency_phone" />
</property>
<property name="userName">
<column name="username" />
</property>
<property name="passWord">
<column name="psword" />
</property>
<property name="notify">
<column name="notify" />
</property>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="registration.ContactInformation" table="CONTACT_INFORMATION">
<id name="contactID" type="int" unsaved-value="any">
<column name="contact_id" sql-type="int" not-null="true"/>
<generator class="increment"/>
</id>
<property name="address">
<column name="address"/>
</property>
<property name="city">
<column name="city"/>
</property>
<property name="province">
<column name="province"/>
</property>
<property name="email">
<column name="email" />
</property>
<property name="phone">
<column name="phone_num" />
</property>
<property name="postalCode">
<column name="postal_code" />
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Possible soulution
PostPosted: Fri Mar 11, 2005 12:24 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
OK, I guess I'll have to answer my own post. It seems on doing a seach that this is somewhat of a problem. Unfortunately there are never any soulutions posted or even any further probing for information.

My guess is (and it is a gues because I have come up with a kluge to the problem and have not tested this theory yet) is that there is a problem with the equalsto and hashcode. Without these methods properly implemented there will be issues with database idenity with objects.

What I beleive is happening is that this is a web based application. In some instances the sessions is being closed causing a dereferencing between the object in memory and the object as it is stored in the database. Therefore Hibernate thinks it is a new object that has the same primary key as an object stored in the database. Therefore it assignes the next available key (Hibernate is manageing my keys) to the object and stores it in the database.

So what needs to be done is to implements the equalsto and hashcode methods so that Hibernate knows that we are talking about the same object in the database and it can reassociate these in my new transaction. (I will be testing this at a later date)

My kluge was to add an attribute to the many object in my many-to-one association that will be the forign key(FK) to the one. The object itself is mapped but I have set the update and insert to false on the many to one mapping and just mapped the FK as a regular attrubute and set it in my Data Access Object (DAO). Hibernate can then recreate the object easily and does not mess things up when i persist the new objects. (its ugly but it works)


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.