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.