-->
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.  [ 1 post ] 
Author Message
 Post subject: question about persisting many-to-one objecs
PostPosted: Mon Aug 04, 2008 11:01 pm 
Newbie

Joined: Wed Aug 01, 2007 4:00 am
Posts: 8
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.