-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate treats Transient object as persisted object?
PostPosted: Sat Apr 14, 2007 10:21 am 
Newbie

Joined: Sat Apr 07, 2007 6:53 am
Posts: 16
Hello All, I have a problem. I have a Customer with a Bank_Address that inherites from Address Class. When I create a new customer and new bank_address transient objects.

Code:
           Customer customer = new Customer("2", ...);
           customer.setBank_Address(new Bank_Address(...));


and save them calling save method of session as follow:

Code:
             session.save(customer);
             session.Flush();


This code crashes because hibernate insert customer, but later, it try update a bank_Address, as if it already was a persisted object.

SQL LOG-->
Quote:
Hibernate: INSERT INTO CUSTOMER (FIRST_NAME, SECOND_NAME, BANK_ADDRESS, ID) VALUES (@p0, @p1, @p2, @p3); @p0 = 'Jordi', @p1 = 'Cabré Roca', @p2 = '2', @p3 = '2'
Hibernate: UPDATE ADDRESS SET STREET = @p0, NUMBER_STREET = @p1, CITY = @p2, STATE = @p3 WHERE ID = @p4; @p0 = 'La Parra', @p1 = '15', @p2 = 'Riudecols', @p3 = 'Tarragona', @p4 = '2'

<-------> In this point program crashes, becaus in database this bank_address doesn't exist.
if code doesn't crashes the follow sentence is UPDATE BANK_ADDRESS ...

the correct SQL sentences should be:
INSERT INTO CUSTOMER(...)
INSERT INTO ADDRESS(...) instead of UPDATE ADDRESS SET(...)
INSERT INTO BANK_ADDRESS(...)

What Do I do wrong?

thanks in advanced.

Mapping files -->
Code:
   <class name="Customer" table="CUSTOMER">
      <id name="Id" column="ID" type="String">
         <generator class="assigned" />
      </id>
                ...
      <many-to-one name="Bank_address" class="Model.BankAddress, ComponentsBroker" unique="true" cascade="all" column="BANK_ADDRESS" />
                 ...
   </class>

   <class name="Address" table="ADDRESS">
      <id name="Id" column="ID" type="String">
         <generator class="assigned" />
      </id>
      <property name="Street" type="String" column="STREET"/>
      <property name="Number_street" type="String" column="NUMBER_STREET"/>
      <property name="City" type="String" column="CITY"/>
      <property name="State" type="String" column="STATE"/>
      
      <joined-subclass name="BankAddress" table="BANK_ADDRESS">
         <key column="ADDRESS_ID"/>
         <property name="BankName" type="String" column="BANK_NAME"/>
         <property name="Account" type="String" column="ACCOUNT"/>
      </joined-subclass>
      
      <joined-subclass name="Model.ContactAddress, ComponentsBroker" table="CONTACT_ADDRESS">
         <key column="ADDRESS_ID"/>
         
         <component class="Name" name="ContactName">
            <property name="First_name" column="FIRST_NAME" type="String"/>
            <property name="Second_name" column="SECOND_NAME" type="String"/>
         </component>
         <property name="Phone" type="String" column="PHONE"/>
      </joined-subclass>
   </class>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 10:05 am 
Newbie

Joined: Sat Apr 07, 2007 6:53 am
Posts: 16
I've found other forum item in 2005 that talked about the same problem.

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=000071

In these thread they told the problem was "assigned" id generator. they said Hibernate assumes that if an object has an id (different from 'null' or from the 'unsaved-value'), then it's an existing DB record.

And here appear two problems:

* What about entyties that id are natural keys (social security number, ...)
* Why my customer is saved so? its id is assigned = '2' and id is diferent of null.

Can you help me to see some light? Any help would be great appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 15, 2007 3:26 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
In fact, Hibernate needs a way to know if an entity is already persisted or still only in memory (for example, to know if an insert or an update is needed).

To manager this, it's recommended to use surrogate keys, not business/natural one.

Apart from this, I guess your mapping maybe just misses an unsaved-value="null" attribute (although this is the default, imo...). This would tell hibernate that if getId() returns null, then it's not been persisted yet, see ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 16, 2007 6:32 am 
Newbie

Joined: Sat Apr 07, 2007 6:53 am
Posts: 16
Ok thanks for your help, however, why to save customer generates a INSERT instead of UPDATE? If I follow your words, customer object should be updated because your id= '2'. Why the customer is inserted and bank_address is updated?

When I execute session.save(customer) hibernate generates a INSERT CUSTOMER. According to your words hibernate should generate a UPDATE CUSTOMER.

Thanks for all.


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