-->
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.  [ 10 posts ] 
Author Message
 Post subject: Newbie : Problem while persisting.
PostPosted: Mon Jun 11, 2007 3:12 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
I am having problem while persisting using the "save()" method. I am using

Hibernate version: 3.2


Mapping documents:User.hbm.xml

Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false" default-cascade="none" default-access="property">
   
   <!-- User Table Mapping -->

   <class name="userprofile.layer.domain.User" table="TB_USER">
      <id name="userID" column="USER_ID">
           <generator class="assigned" />
        </id>
      
      <property name="title" column="TITLE"/>
      <property name="generation" column="GENERATION"/>
      <property name="firstName" column="FIRST_NAME"/>
      <property name="middleInitial" column="MIDDLE_INITIAL"/>   
      <property name="lastName" column="LAST_NAME"/>
      <property name="emailAddress" column="EMAIL_ADDRESS"/>
      <property name="createDateTS" column="CREATE_DATE_TS"/>
      
      <one-to-one name="userAddress" class="userprofile.layer.domain.UserAddress"/>

   </class>

   <!-- User Address Table Mapping -->   

   <class name="userprofile.layer.domain.UserAddress" table="TB_USER_ADDRESS">

      <id name="userID" column="USER_ID">
         <generator class="foreign">
            <param name="property">User</param>
         </generator>
      </id>      
      
      <one-to-one name="userObj" class="userprofile.layer.domain.User" constrained="true"/>

      <property name="addressLine1" column="ADDRESS_LINE_1"/>
      <property name="addressLine2" column="ADDRESS_LINE_2"/>
      <property name="cityName" column="CITY_NAME"/>
      <property name="stateProvince" column="STATE_PROVINCE"/>
      <property name="zipCode" column="ZIP_CODE"/>         
   </class>
</hibernate-mapping>



User object has a property of "UserAddress"
UserAddress object has a property of "User".

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

Code:
session.save(userObj)


The problem is, i can see only the user table has new entry but the address table is not being populated with new row. Can somebody correct me if anything is wrong in the configuration or else in the code.


Name of the database : DB2 [/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 11, 2007 5:04 pm 
Beginner
Beginner

Joined: Mon May 21, 2007 5:22 pm
Posts: 27
Location: Salt Lake City
[code]<!-- User Address Table Mapping -->

<class name="userprofile.layer.domain.UserAddress" table="TB_USER_ADDRESS">

<id name="userID" column="USER_ID">
<generator class="foreign">
<param name="property">[b]userObj[/b]</param>
</generator>
</id>

<one-to-one name="userObj" class="userprofile.layer.domain.User" constrained="true"/>

<property name="addressLine1" column="ADDRESS_LINE_1"/>
<property name="addressLine2" column="ADDRESS_LINE_2"/>
<property name="cityName" column="CITY_NAME"/>
<property name="stateProvince" column="STATE_PROVINCE"/>
<property name="zipCode" column="ZIP_CODE"/>
</class> [/code]

you have to use userObj in the foreign key attribute, not the User.

this might work :)

_________________
Try 'N' Enjoy !!
Rahul

If helpful do rating ....


Top
 Profile  
 
 Post subject: still not working!!!
PostPosted: Tue Jun 12, 2007 2:46 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
I tried changing the mapping file as suggested by rahul dongre, but i still don't see any change.

I have two tables, one is User and one is User_Address, and the mapping file contains like the following,

Code:
    <class name="userprofile.layer.domain.User" table="USER">
     <id name="userID" column="USER_ID">
           <generator class="assigned" />
        </id>
     .....
     ..... other properties.....
     .....
     <one-to-one name="userAddress" class="userprofile.layer.domain.UserAddress"/>
    </class>

    <class name="userprofile.layer.domain.UserAddress" table="USER_ADDRESS">

      <id name="userID" column="USER_ID">
         <generator class="foreign">
            <param name="property">userObj</param>
         </generator>
      </id>

        .......
        ..... other properties......
      <one-to-one name="userObj" class="userprofile.layer.domain.User" constrained="true"/>



and i am calling save() method, passing the populated "User" object, which also contains "UserAddress" as one of the property. My question is, whether do i need to call save() method by passing "UserAddress" object also.

Quote:
Does hibernate takes care of inserting into two tables, if we call save() method only once by passing the User object.?


Can any one help me in this issue, thanks all..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 1:23 am 
Regular
Regular

Joined: Mon Apr 02, 2007 3:54 am
Posts: 67
Location: Hyderabad
As Per me you need to call it for all the classes which you are using for inserting a record and saving it.


Top
 Profile  
 
 Post subject: Re: still not working!!!
PostPosted: Wed Jun 13, 2007 1:32 am 
Newbie

Joined: Wed Jun 13, 2007 1:19 am
Posts: 1
Location: The Netherlands
hib_user wrote:
Does hibernate takes care of inserting into two tables, if we call save() method only once by passing the User object.?


That depends on how you model your class relations. In your case both classes are modeled as stand-alone entities. Entities that are referenced by other entities are usualy not "taken care" of automatically unless you take some extra measures.

I see in your hibernate-mapping root element that your default-cascade="none" . This could probably be one cause of your problem. The cascade option should at least be "save-update" to ensure that one "save()" call for your User object also stores the associated address.
Another option, of course, is to save the User object and its address both explicitly.


Top
 Profile  
 
 Post subject: Still not successful!!!!!
PostPosted: Wed Jun 13, 2007 11:10 am 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
I tried adding the cascade option. I tried giving different values each time and nothing worked, i gave cascade="persist, delete" did not worked, so i tried cascade="persist" did not worked, so i tried cascade="save-update" and this one gave me an exception, if i give this option, hibernate is generating "insert" sql for user object and an "update" sql for UserAddress object, so it is giving an exception. I tried giving cascade="all", this too giving exception.

Here is my mapping file,

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-lazy="false" default-cascade="persist" default-access="property">
   
   <!-- User Table Mapping -->

   <class name="userprofile.layer.domain.User" table="USER">
      <id name="userID" column="USER_ID">
           <generator class="assigned" />
        </id>   
      
      <property name="title" column="TITLE"/>
      <property name="firstName" column="FIRST_NAME"/>
      <property name="middleInitial" column="MIDDLE_INITIAL"/>      
      <property name="lastName" column="LAST_NAME"/>
      <property name="emailAddress" column="EMAIL_ADDRESS"/>
      <property name="createDateTS" column="CREATE_DATE_TS"/>      
      
      <one-to-one name="userAddress" class="userprofile.layer.domain.UserAddress" cascade="persist"/>

   </class>

   <!-- User Address Table Mapping -->   

   <class name="userprofile.layer.domain.UserAddress" table="USER_ADDRESS">

      <id name="userID" column="USER_ID">
         <generator class="foreign">
            <param name="property">userObj</param>
         </generator>
      </id>      
      
      <!-- <one-to-one name="userObj" class="userprofile.layer.domain.User" constrained="true"/>   -->

      
      <property name="addressLine1" column="ADDRESS_LINE_1"/>
      <property name="addressLine2" column="ADDRESS_LINE_2"/>
      <property name="cityName" column="CITY_NAME"/>
      <property name="stateProvince" column="STATE_PROVINCE"/>
      <property name="countryCode" column="COUNTRY_CODE"/>
      <property name="zipCode" column="ZIP_CODE"/>         
   </class>
</hibernate-mapping>


I commented out <one-to-one mapping for the class "UserAddress", even that too didn't worked...

Quote:
So my only option is to call "save()" twice for "User" and "UserAddress" separately?


Can somebody help me........what is wrong in my mapping file....


Top
 Profile  
 
 Post subject: Re:
PostPosted: Wed Jun 13, 2007 4:25 pm 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Hi hib_user,

You will need to put a many-to-one with unique=true on one side for this to work.

I have answered your similar query here:

http://forum.hibernate.org/viewtopic.php?t=976194&highlight=

*Please don't forget to rate in both places*

Regards,
Jitendra


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 4:36 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
Sorry, it didn't worked...i checked the log files...it is not generating insert sql for the address table..


Top
 Profile  
 
 Post subject: You have it almost...
PostPosted: Wed Jun 13, 2007 11:17 pm 
Newbie

Joined: Thu May 31, 2007 3:27 am
Posts: 11
It is ok to use two "one-to-one" elements in your case because your using the special id generator "foreign" and your association is made through primary keys.

For the cascade-style, you should use "save-update" or "all". "persist" won't work if your using the session.save(), you'll have to use session.persist().

To correctly persist both sides of your bidirectional association with only one save, you'll have to do something like this:
Code:
.....//Transaction begin

User user=new User();
...
UserAddress userAddress=new UserAddress();
...

user.setUserAddress(userAddress);
userAddress.setUserObj(user);

session.save(user);

.....//transaction commit


Now, to clean this a little bit, you could add this in the User's setUserAddress method:

Code:
public void setUserAddress(UserAddress userAddress) {
    this.userAddress = userAddress;
    userAddress.setUserObj(this);
  }


And you can then get ride of the "userAddress.setUserObj(user);" in the first exemple and do like this:

Code:

User user=new User();
user.setUserID(1000);

UserAddress userAddress=new UserAddress();
user.setUserAddress(userAddress);

try{
session.save(user);
tx.commit();
}catch(HibernateException he){
tx.rollback();
throw he;
}finally{
session.close();
}


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jun 14, 2007 2:55 am 
Senior
Senior

Joined: Tue Jun 12, 2007 4:49 pm
Posts: 127
Location: India
Quote:
It is ok to use two "one-to-one" elements in your case because your using the special id generator "foreign" and your association is made through primary keys.


This will mean having same primary keys, which is not the case here. So we need to have many-to-one.

hib_user, I missed that you are using save. If you want to use save u will have to use save-update or all as cascade, else as letrait said, use persist.

Regards,
Jitendra


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