-->
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: Collections being deleted on save.
PostPosted: Wed Jul 13, 2005 6:33 pm 
Newbie

Joined: Tue May 31, 2005 6:37 pm
Posts: 2
Hi,

I have an method that tries to update a hibernate mapped object. The object that I am trying to update is a Product object which has a many-to-many relationship with the supplier objects.

1. public ProductDataObject updateProduct(ASObject asProduct) {
2. Product product = (Product) new ASTranslator().fromActionScript(asProduct);
3. return (ProductDataObject) new ProductDAO().makePersistent(product).getDataObject();
4. }

On line 2. the product object is fully populated except for the "supplier" field which is a Set of Supplier objects. The "supplier" field is null at this stage. When I try to save this Product object on line 3 there is a query executed that deletes the existing supplier relationship. Other than trying to select the supplier collection from the database is there another way that I can stop this relationship deletion from happening?

Is there a way to tell hibernate to only delete collections when I specify it? I essentially only want hibernate to update relationships when the code tells it to. (or the mapping files tell it to).

Any pointers would be appreciated.

Thanks

Jordan

[b]Hibernate version:3.05

[b]Name and version of the database you are using:MySQL

[b]The generated SQL (show_sql=true):


Top
 Profile  
 
 Post subject: Re: Collections being deleted on save.
PostPosted: Wed Jul 13, 2005 7:47 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
jordan wrote:
Hi,

I have an method that tries to update a hibernate mapped object. The object that I am trying to update is a Product object which has a many-to-many relationship with the supplier objects.

1. public ProductDataObject updateProduct(ASObject asProduct) {
2. Product product = (Product) new ASTranslator().fromActionScript(asProduct);
3. return (ProductDataObject) new ProductDAO().makePersistent(product).getDataObject();
4. }

On line 2. the product object is fully populated except for the "supplier" field which is a Set of Supplier objects. The "supplier" field is null at this stage. When I try to save this Product object on line 3 there is a query executed that deletes the existing supplier relationship. Other than trying to select the supplier collection from the database is there another way that I can stop this relationship deletion from happening?

Is there a way to tell hibernate to only delete collections when I specify it? I essentially only want hibernate to update relationships when the code tells it to. (or the mapping files tell it to).

Any pointers would be appreciated.

Thanks

Jordan

[b]Hibernate version:3.05

[b]Name and version of the database you are using:MySQL

[b]The generated SQL (show_sql=true):


Can you include your mapping files.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 3:47 am 
Newbie

Joined: Tue May 31, 2005 6:37 pm
Posts: 2
Thanks for the reply. Here are two mapping files which specify the many-to-many relationship between a Product and an Outlet. In my original post I should have said Outlet and not Supplier. It's the same problem though.

It seems that if I set the collections to 'null' then I get the deletion. If I set them to the populated collections then it is ok.

I suspect that I must be missing an attribute on one of the mappings. Again, any help would be most appreciated.

Thanks again

Jordan

////////////////////////////
Product mapping file
///////////////////////////

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="uk.co.caley.vo.Product"
table="PRODUCT"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true"
>

<id
name="id"
column="ID"
type="java.lang.Long"
length="10"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Product.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="productName"
type="java.lang.String"
update="true"
insert="true"
>
<column
name="PRODUCT_NAME"
length="250"
not-null="true"
unique="false"
sql-type="VARCHAR (250)"
/>
</property>

<set
name="discounts"
lazy="true"
inverse="false"
cascade="delete-orphan"
sort="unsorted"
order-by="DATE_ADDED"
>

<key
column="PRODUCT_ID"
>
</key>

<one-to-many
class="uk.co.caley.vo.Discount"
/>

</set>

<set
name="outlets"
table="PRODUCT_OUTLET"
lazy="true"
inverse="true"
cascade="none"
sort="unsorted"
>

<key
column="PRODUCT_ID"
>
</key>

<many-to-many
class="uk.co.caley.vo.Outlet"
column="OUTLET_ID"
outer-join="auto"
/>

</set>

</class>

</hibernate-mapping>


////////////////////////////
Outlet mapping file
///////////////////////////

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="uk.co.caley.vo.Outlet"
table="OUTLET"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true"
>

<id
name="id"
column="ID"
type="java.lang.Long"
length="10"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Outlet.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="outletName"
type="java.lang.String"
update="true"
insert="true"
>
<column
name="OUTLET_NAME"
length="250"
not-null="true"
unique="false"
sql-type="VARCHAR (250)"
/>
</property>

<set
name="discounts"
lazy="true"
inverse="true"
cascade="delete-orphan"
sort="unsorted"
>

<key
column="OUTLET_ID"
>
</key>

<one-to-many
class="uk.co.caley.vo.Discount"
/>

</set>

<set
name="products"
table="PRODUCT_OUTLET"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
>

<key
column="OUTLET_ID"
>
</key>

<many-to-many
class="uk.co.caley.vo.Product"
column="PRODUCT_ID"
outer-join="auto"
/>

</set>

</class>

</hibernate-mapping>


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.