-->
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: The session.merge(obj) is inserting one extra record
PostPosted: Thu Apr 07, 2005 6:18 pm 
Newbie

Joined: Thu Mar 24, 2005 12:42 pm
Posts: 2
I am using merge to save the data in the database, but I am getting and extra record.
Somebody can give me an idea to fix this problem. I believe that the problem is in the mapping file but I do not know what to do to resolve this problem.


Hibernate version:
3.0.2

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.shsolutions.campaign.model">
<class
name="Person"
table="Persons"
>
<id
name="id"
type="integer"
column="person_id"
>
<generator class="native"/>
</id>

<property
name="firstName"
column="first_name"
type="string"
not-null="true"
length="30"
/>
<property
name="middleName"
column="middle_name"
type="string"
not-null="false"
length="30"
/>
<property
name="lastName"
column="last_name"
type="string"
not-null="true"
length="30"
/>
<property
name="genderId"
column="gender_id"
type="integer"
not-null="false"
length="10"
/>
<property
name="dob"
column="dob"
type="timestamp"
not-null="false"
length="23"
/>
<property
name="email"
column="email"
type="string"
not-null="true"
length="50"
/>
<property
name="languageId"
column="language_id"
type="string"
not-null="true"
length="2"
/>
<many-to-one
cascade="all"
name="address"
column="address_id"
class="Address"
not-null="false"
>
</many-to-one>

<set
cascade="all"
lazy="true"
name="phones"
table="Person_Phones"
>
<key column="person_id" />
<many-to-many class="Phone" column="phone_id" />
</set>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

User u;
User uCurr;

uCurr = ubo.getUser(new Integer(7));

u = new User();
u.setId( uCurr.getId() );
u.setUsername("palfaro");
u.setPassword("palfaro");
u.setFirstName("Gustavo");
u.setLastName("Alfaro");
u.setEmail("palfaro@shsolutions.com");
u.setLanguageId("EN");

Address a = new Address();
a.setId( uCurr.getAddress().getId() );
a.setStreet("McNap");
a.setZip("33363");
a.setDateSaveTime(false);
a.setStateId("FL");
a.setCountryId("US");
a.setCity("Coconut Creek");
a.setTimeZoneId("GMT-05:00");
u.setAddress(a);

Set p = uCurr.getPhones();
//p = new HashSet();
p.add( new Phone(null, "22222"));
u.setPhones(p);

Transaction tx = ubo.s.beginTransaction();

ubo.s.merge(u);

tx.commit();

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Sybase

The generated SQL (show_sql=true):
Hibernate: select user0_.user_id as person1_0_, user0_1_.first_name as first2_25_0_, user0_1_.middle_name as middle3_25_0_, user0_1_.last_name as last4_25_0_, user0_1_.gender_id as gender5_25_0_, user0_1_.dob as dob25_0_, user0_1_.email as email25_0_, user0_1_.language_id as language8_25_0_, user0_1_.address_id as address9_25_0_, user0_.username as username39_0_, user0_.password as password39_0_ from Users user0_ inner join Persons user0_1_ on user0_.user_id=user0_1_.person_id where user0_.user_id=?
Hibernate: select phones0_.person_id as person1___, phones0_.phone_id as phone2___, phone1_.phone_id as phone1_0_, phone1_.number as number26_0_, phone1_.ext as ext26_0_, phone1_.type_id as type4_26_0_, phone1_.note as note26_0_ from Person_Phones phones0_ inner join Phones phone1_ on phones0_.phone_id=phone1_.phone_id where phones0_.person_id=?
Hibernate: select address0_.address_id as address1_0_, address0_.street as street0_0_, address0_.zip as zip0_0_, address0_.zip_ext as zip4_0_0_, address0_.date_save_time as date5_0_0_, address0_.state_id as state6_0_0_, address0_.country_id as country7_0_0_, address0_.city as city0_0_, address0_.time_zone_id as time9_0_0_ from Addresses address0_ where address0_.address_id=?
Hibernate: insert into Phones (number, ext, type_id, note) values (?, ?, ?, ?)
select @@identity
Hibernate: insert into Phones (number, ext, type_id, note) values (?, ?, ?, ?)
select @@identity
Hibernate: insert into Person_Phones (person_id, phone_id) values (?, ?)

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 06, 2005 1:12 pm 
Pro
Pro

Joined: Fri Nov 19, 2004 5:52 pm
Posts: 232
Location: Chicago, IL
I don't know if this is related or not, but, I've been having problems when calling Session.update() on a detached object. For some reason, Session.update() is inserting a new row in the database for the object and then it is updating the row that is supposed to be updated. I'm still trying to figure out what is going on here. I'm using Hibernate 3.0.2 with EJB annotations.


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.