-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parent child problem
PostPosted: Wed Jul 21, 2004 1:34 am 
Newbie

Joined: Wed Jul 21, 2004 1:04 am
Posts: 2
Location: Noida
Hi,

I am trying the parent child one to many relationship since last 2 days. I read manuals and went through the forum but not very useful.

The scenario is I have a UserVO class which can have at the most 2 child AddressVO. The mapping files are as follows

Code:
<hibernate-mapping>
   <class name="UserVO" table="pocuser">
      <id name="id" type="int" column="id">
         <generator class="identity"/>
      </id>
      <set name="address" inverse="true" lazy="true" cascade="all-delete-orphan">
         <key column="pocuser_id" />
         <one-to-many class="AddressVO"/>
      </set>
      <property name="userid" column="userid" not-null="true"/>
   </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
   <class name="AddressVO" table="address">
      <id name="id" type="integer" column="id">
         <generator class="identity"/>
      </id>
      <many-to-one name="user" column="pocuser_id"/>
      <property name="address_type">
         <column name="addressType" sql-type="char(1)" not-null="true"/>
      </property>
      <property name="address">
         <column name="address" not-null="true"/>
      </property>
   </class>
</hibernate-mapping>


The UserVO have
Code:
public Set getAddress() {
        return address;
}
public void setAddress(Set pAddress) {
        this.address = pAddress;
}


The AddressVO have
Code:
public UserVO getUser() {
         return this.user;
}
public void setUser(UserVO pUser) {
         this.user = pUser;
}


Now to test I do
Code:
            Session session = HibernateSessionFactory.currentSession();
            Transaction tx = session.beginTransaction();
            UserVO user = new UserVO();
            user.setUserid("bbhangale");
           
            AddressVO address = new AddressVO();
            address.setAddress("Saket Nagar");
            address.setAddressType('P');
            address.setUser(user);
            user.getAddress().add(address);
           
            address = new AddressVO();
            address.setAddress("Noida");
            address.setAddressType('C');
            address.setUser(user);
            user.getAddress().add(address);

            session.save(user);
            tx.commit();


But it doesn't work. I get following error
Quote:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: AddressVO


Now If I add only single address then the error is
Quote:
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)


I check the log for 2nd scenario and i find update query being fired for address whereas it should fire insert query.

Please help.

_________________
Thanks
Bhushan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 1:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Please read the FAQ and documentation before posting to the forum.


unsaved-value="0"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 2:44 am 
Newbie

Joined: Wed Jul 21, 2004 1:04 am
Posts: 2
Location: Noida
I did read manuals and FAQ but missed the unsaved one.

Thanks it worked!

One need to say unsaved-value="0" for the primary key of the child.

_________________
Thanks
Bhushan


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