-->
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.  [ 8 posts ] 
Author Message
 Post subject: Unable to persist to multiple tables.
PostPosted: Wed Oct 03, 2007 3:42 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
I have an object called "User", which has two other objects "UserAddress" and "UserContInfo" objects. And i have 3 tables corresponding to each class.

When i try to save the "User" object, i can see only entry into "User" table and i don't see any entries in other two tables. Do i need to call save on each object? Here is my mapping files.

Hibernate version:3.2

Mapping documents:

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 package="userprofile.layer.domain" auto-import="true" default-lazy="false" default-cascade="persist" default-access="property">
   
   <!-- User Table Mapping -->

   <class name="User" table="USER">
      <id name="userID" column="USER_ID">
           <generator class="assigned"/>
        </id>      

      <property name="firstName" column="FIRST_NAME"/>
      <property name="middleInitial" column="MIDDLE_INITIAL"/>      
      <property name="lastName" column="LAST_NAME"/>
      
      <one-to-one name="userAddress" class="UserAddress"/>
      <one-to-one name="userContInfo" class="UserContInfo"/>

   </class>

   <!-- User Address Table Mapping -->   

   <class name="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="User" constrained="true"/>
         
      
      
      <property name="address1" column="ADDRESS_1"/>
      <property name="address2" column="ADDRESS_2"/>
      <property name="cityName" column="CITY_NAME"/>
      <property name="state" column="STATE"/>
      <property name="country" column="COUNTRY"/>
      <property name="zipCode" column="ZIP_CODE"/>         
   </class>

   <!-- User Contact Info Table Mapping -->   

   <class name="UserContInfo" table="USER_CONT_INFO">      
      <id name="userID" column="USER_ID">
         <generator class="foreign">
            <param name="property">userObj</param>
         </generator>
      </id>      
      
      <one-to-one name="userObj" class="User" constrained="true"/>
         
      <property name="emailID" column="EMAIL_ID"/>
      <property name="cellPhone" column="CELL_PHONE"/>
      <property name="homePhone" column="HOME_PHONE"/>
   </class>
</hibernate-mapping>



Name and version of the database you are using:MySQL 5.0



[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 3:47 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Try to use cascade option ? I don't know what is default for cascade.

http://www.hibernate.org/hib_docs/v3/re ... ild-update
http://www.hibernate.org/hib_docs/v3/re ... n-onetoone

Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 8:39 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
<one-to-one name="userAddress" class="UserAddress"/>
<one-to-one name="userContInfo" class="UserContInfo"/>

change to

<one-to-one name="userAddress" cascade="all"
class="UserAddress"/>
<one-to-one name="userContInfo" cascade="all"
class="UserContInfo"/>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 04, 2007 8:20 pm 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
Hi,

I tried using the cascase="all" option, and now i am getting the following exception:

Code:
org.springframework.orm.hibernate3.HibernateSystemException: attempted to assign id from null one-to-one property: userObj


This exception is coming because, the UserAddress class has a property of "User" class with get and setter methods, but the object is not initialized.

I tried making the property to be initiatlized, but when i tried to deploy it on the server, it is giving error.

Can somebody tell me how a one-to-one mapping can be done.

here are my classes:

Code:
public class User {
   private int userID;
   
   private String firstName;
   private String middleInitial;
   private String lastName;
   private UserAddress userAddress = new UserAddress();
   private UserContInfo userContInfo = new UserContInfo();
   
   public User(){      
   }
   
   /**
    * @return Returns the firstName.
    */
   public String getFirstName() {
      return firstName;
   }
   /**
    * @param firstName The firstName to set.
    */
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   /**
    * @return Returns the lastName.
    */
   public String getLastName() {
      return lastName;
   }
   /**
    * @param lastName The lastName to set.
    */
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   /**
    * @return Returns the middleInitial.
    */
   public String getMiddleInitial() {
      return middleInitial;
   }
   /**
    * @param middleInitial The middleInitial to set.
    */
   public void setMiddleInitial(String middleInitial) {
      this.middleInitial = middleInitial;
   }
   /**
    * @return Returns the userID.
    */
   public int getUserID() {
      return userID;
   }
   /**
    * @param userID The userID to set.
    */
   public void setUserID(int userID) {
      this.userID = userID;
   }   
   
   /**
    * @return Returns the userAddress.
    */
   public UserAddress getUserAddress() {
      return userAddress;
   }
   /**
    * @param userAddress The userAddress to set.
    */
   public void setUserAddress(UserAddress userAddress) {
      this.userAddress = userAddress;
   }

   /**
    * @return the userContInfo
    */
   public UserContInfo getUserContInfo() {
      return userContInfo;
   }

   /**
    * @param userContInfo the userContInfo to set
    */
   public void setUserContInfo(UserContInfo userContInfo) {
      this.userContInfo = userContInfo;
   }
   
   
}


Code:
public class UserAddress {
   private int userID;   
   private String address1;
   private String address2;
   private String cityName;
   private String state;
   private String country;
   private String zipCode;
   private User userObj;
   //private UserAddressCompositeKey userAddressCompKey;   
   
   public UserAddress(){      
   }

   /**
    * @return Returns the addressLine1.
    */
   public String getAddress1() {
      return address1;
   }
   /**
    * @param addressLine1 The addressLine1 to set.
    */
   public void setAddress1(String address1) {
      this.address1 = address1;
   }
   /**
    * @return Returns the addressLine2.
    */
   public String getAddress2() {
      return address2;
   }
   /**
    * @param addressLine2 The addressLine2 to set.
    */
   public void setAddress2(String address2) {
      this.address2 = address2;
   }
   /**
    * @return Returns the cityName.
    */
   public String getCityName() {
      return cityName;
   }
   /**
    * @param cityName The cityName to set.
    */
   public void setCityName(String cityName) {
      this.cityName = cityName;
   }

   /**
    * @return Returns the country
    */
   public String getCountry() {
      return country;
   }
   /**
    * @param country The country to set.
    */
   public void setCountry(String country) {
      this.country = country;
   }
   /**
    * @return Returns the stateProvince.
    */
   public String getState() {
      return state;
   }
   /**
    * @param State The State to set.
    */
   public void setState(String state) {
      this.state = state;
   }
   /**
    * @return Returns the userID.
    */
   public int getUserID() {
      return userID;
   }
   /**
    * @param userID The userID to set.
    */
   public void setUserID(int userID) {
      this.userID = userID;
   }
   /**
    * @return Returns the zipCode.
    */
   public String getZipCode() {
      return zipCode;
   }
   /**
    * @param zipCode The zipCode to set.
    */
   public void setZipCode(String zipCode) {
      this.zipCode = zipCode;
   }

   /**
    * @return the userObj
    */
   public User getUserObj() {
      return userObj;
   }

   /**
    * @param userObj the userObj to set
    */
   public void setUserObj(User userObj) {
      this.userObj = userObj;
   }

   
      
}



Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 1:42 am 
Newbie

Joined: Thu Oct 04, 2007 5:00 am
Posts: 13
I think you should send your code:
Aren't you doing something like follow?

UserAddress userAddress = new UserAddress();
userAddress.set...
userAddress.set...
...

ContactInfo contactInfo = new ContactInfo();
contactInfo.set...
contactInfo.set..
...

Users user = new User();
user.setContactInfo(contactInfo);
user.setUseAddress(userAddres);

session.save(userAddress);
session.save(contactInfo);
session.save(user);

.....

This code is working fine in my project.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 10:48 am 
Newbie

Joined: Mon Jun 11, 2007 2:58 pm
Posts: 11
Yes, you can do that. But, when you have defined an association in the mapping, then hibernate will persist different associated objects to their respective tables, if you "cascade" option in the mapping file. You don't need to call separate "save" operations on each object.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 12:21 am 
Newbie

Joined: Thu Oct 04, 2007 5:00 am
Posts: 13
Thanks hib_user.
But there are various options for cascade ie cascade-delete,cascade-update etc. In which condition we can use the particular option? or should we use "cascade-all" always.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 2:38 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Hi!

It's on you how to use the cascade option. You can use all cascades, or only some.

Generally I would say, you can use all options from parent to child, and just exclude delete option from child to parent. It can also differ from association to association.

You can also think about all-delete-oprhan for child associations.

http://www.hibernate.org/hib_docs/v3/re ... d-cascades
http://ideoplex.com/id/881/cascading-ac ... -hibernate

Maybe this helps,
Pavol


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