Code:
Code:
Hello,
I have 2 tables Company and Address with One Many relation
Company(1) --> Address(*)
When I insert a new Company, the Addresses associated with are also inserted into the Address Table.
But when I update an existing Company, the changes in the Addresses are not updated.
I used the cascade="all" attribute in the One - MAny Relationship set.
Please find below my code and tell me what I am missing.
MemberCompany.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">
<!-- Generated Jun 9, 2009 12:49:11 PM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="org.suiteaccess.domain.MemberCompany" table="MemberCompany" schema="dbo" catalog="suiteaccess">
<id name="companyId" type="int">
<column name="companyId" />
<generator class="identity" />
</id>
<property name="companyName" type="string">
<column name="companyName" not-null="true" unique="true" />
</property>
[b]<set name="addresses" inverse="true" lazy="true" table="Address" fetch="select" cascade="all">
<key>
<column name="memberCompanyId" />
</key>
<one-to-many class="org.suiteaccess.domain.Address" />
</set>[/b]
</class>
</hibernate-mapping>
Address.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">
<!-- Generated Jun 6, 2009 7:40:18 PM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="org.suiteaccess.domain.Address" table="Address" schema="dbo" catalog="suiteaccess">
<id name="addressId" type="int">
<column name="addressId" />
<generator class="identity" />
</id>
[b] <many-to-one name="memberCompany" class="org.suiteaccess.domain.MemberCompany" fetch="select">
<column name="memberCompanyId" />
</many-to-one>[/b]
<property name="address1" type="string">
<column name="address1" length="50" not-null="true" />
</property>
.
.
</class>
</hibernate-mapping>
Hibernate.cfg.xml:Code:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/suiteaccess;instance=SQLEXPRESS</property>
<property name="connection.username">sa</property>
<property name="connection.password">9184leela</property>
<property name="connection.pool_size">2</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hbm2ddl.auto">false</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="mappings/Users.hbm.xml" />
<mapping resource="mappings/Address.hbm.xml" />
<mapping resource="mappings/MemberCompany.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code:
Address.javaCode:
package org.suiteaccess.domain;
import java.util.Date;
/**
* Address generated by hbm2java
*/
public class Address implements java.io.Serializable {
private int addressId;
private MemberCompany memberCompany;
private String address1;
.
.
public Address() {
}
public Address(int addressId, String address1) {
this.addressId = addressId;
this.address1 = address1;
.
.
}
public Address(int addressId, MemberCompany memberCompany,
String address1) {
this.addressId = addressId;
this.memberCompany = memberCompany;
this.address1 = address1;
.
.
}
public int getAddressId() {
return this.addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public MemberCompany getMemberCompany() {
return this.memberCompany;
}
public void setMemberCompany(MemberCompany memberCompany) {
this.memberCompany = memberCompany;
}
}
MemberCompany.java:Code:
package org.suiteaccess.domain;
// Generated Jun 9, 2009 12:49:10 PM by Hibernate Tools 3.2.4.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* MemberCompany generated by hbm2java
*/
public class MemberCompany implements java.io.Serializable {
private int companyId;
private String companyName;
private Set addresses = new HashSet(0);
public MemberCompany() {
}
public MemberCompany(int companyId, String companyName) {
this.companyId = companyId;
this.companyName = companyName;
}
public MemberCompany(int companyId, String companyName,
Set addresses,) {
this.companyId = companyId;
this.companyName = companyName;
this.addresses = addresses;
}
public int getCompanyId() {
return this.companyId;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public String getCompanyName() {
return this.companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Set getAddresses() {
return this.addresses;
}
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
}
MemberCompanyDoa:Code:
public class MemberCompanyDao {
private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
public void persist(MemberCompany transientInstance) {
log.debug("persisting MemberCompany instance");
try {
Session session= sessionFactory.openSession();
Transaction tx=session.getTransaction();
tx.begin();
session.persist(transientInstance);
tx.commit();
session.close();
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void delete(MemberCompany persistentInstance) {
log.debug("deleting MemberCompany instance");
Session session=null;
try {
session= sessionFactory.openSession();
Transaction tx=session.getTransaction();
tx.begin();
session.delete(persistentInstance);
tx.commit();
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
finally{
session.close();
}
}
[b] public MemberCompany update(MemberCompany detachedInstance) {
log.debug("merging MemberCompany instance");
try {
Session session= sessionFactory.openSession();
Transaction tx=session.getTransaction();
tx.begin();
session.update(detachedInstance);
tx.commit();
session.close();
log.debug("update successful");
return null;
} catch (RuntimeException re) {
log.error("update failed", re);
throw re;
}
}[/b]
}
The main class:
Code:
address=(Address)addresses.get("addedAddress"+i);
address.setMemberCompany(memberCompany);
addressSet.add(address);
memberCompany.setAddresses(addressSet);
System.out.println("Updating....");
memberCompany.setCompanyId(id);
memberCompanyDao.update(memberCompany);