Hi,
I am using Hibernate 3.x with MySql 5.0.
Is the 6th line necessary here? Do we need to execute
manufactureDAO.saveManufacture(manufacture);
here ?
1. @Transactional(propagation = Propagation.REQUIRED)
2. public void saveProduct(final Product product, final Integer manufacturerId)
3. {
4. Manufacturer manufacturer = manufacturerDAO.loadManufacturer(manufacturerId);
5. product.setManufacturedBy(manufacturer);
6. manufacturer.addProduct(product);
7. productDAO.saveProduct(product);
8. }
Mapping Files
=============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="net.sf.hibernate4gwt.sample.domain.Manufacturer" table="manufacturer">
<id name="id" type="integer" column="id">
<generator class="native" />
</id>
<property name="manufacturerName" column="manufacturerName" length="255" not-null="true" />
<set name="products" inverse="true" lazy="true" table="product">
<key column="manufacturerId" not-null="true" />
<one-to-many class="net.sf.hibernate4gwt.sample.domain.Product" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="net.sf.hibernate4gwt.sample.domain.Product" table="product">
<id name="id" type="integer" column="id">
<generator class="native" />
</id>
<property name="productName" column="productName" length="255" not-null="true" />
<many-to-one name="manufacturedBy" class="net.sf.hibernate4gwt.sample.domain.Manufacturer" column="manufacturerId" not-null="true" />
</class>
</hibernate-mapping>
DOMAINS
=======
public class Product extends BaseObject
{
private Integer id;
private String productName;
private Manufacturer manufacturedBy;
..........
..........
lic Manufacturer getManufacturedBy()
{
return manufacturedBy;
}
public void setManufacturedBy(Manufacturer manufacturedBy)
{
this.manufacturedBy = (Manufacturer)manufacturedBy;
}
}
public class Manufacturer extends LazyPojo
{
private Integer id;
private String manufacturerName;
private Set products = new HashSet();
..........
..........
public void addProduct(Product aProduct)
{
if ( products == null )
{
products = new HashSet();
}
products.add(aProduct);
}
public void removeProduct(Product aProduct)
{
products.remove(aProduct);
}
}
Thank you.
Sudheer
|