-->
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: Unable to persist associated transient objects
PostPosted: Tue Jul 03, 2007 1:02 pm 
Newbie

Joined: Tue Jul 03, 2007 12:05 pm
Posts: 2
I'm having trouble to persist two transient 1:n-associated objects when using cascade "delete-orphan" without "save-update". Not using "delete-orphan" or using "save-update" works, but is not what is intended.

Hibernate version: 3.2.4.sp1 (though other version showed same behaviour)

A very small (artificial) sample looks like this


Code:
public class TestHibernate {
    public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Item item = new Item();
        item.setName( "Painting IX" );
       
        Bid bid = new Bid();
        bid.setAmount( 23 );
        item.addBid( bid ); // sets both "sides"
       
        session.save( item );
        session.save( bid );
        session.getTransaction().commit();

        HibernateUtil.getSessionFactory().close();
    }


Here
Code:
HibernateUtil
resembles the class found in the hibernate tutorial, bid and item are two entities with the usual getters / setters for the mentioned fields with independent lifecycles.

The relevant parts of the two mappings are as follows:
Code:
<hibernate-mapping>
    <class name="tests.Item" table="ITEMS">
        <id name="id" column="ITEM_ID">
            <generator class="native"/>
        </id>
        <property name="name"/>
       
        <set name="bids" inverse="true" cascade="delete">
              <key column="ITEM_ID" />
              <one-to-many class="tests.Bid" />
        </set>
    </class>
</hibernate-mapping>


and

Code:
<hibernate-mapping>

    <class name="tests.Bid" table="BIDS">
        <id name="id" column="BID_ID">
            <generator class="native"/>
        </id>
        <property name="amount"/>
        <many-to-one name="item" column="ITEM_ID" class="tests.Item" not-null="true" />
    </class>
</hibernate-mapping>


All of the above works as long as I do not use
Code:
delete-orphan
as an additional cascading option; I may write
Code:
delete-orphan
, if I additionally use
Code:
save-update
. Alas, all I really want, is just using
Code:
delete,delete-orphan
which throws an exception on
Code:
session.save( item );
namely a
Code:
TransientObjectException
saying

Code:
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: tests.Bid



Is there a way to persist two transient associated objects using a cascading "delete,delete-orphan"? Changing to a different (working) order like creating "item", persisting "item", creating "bid", persisting "bid" is not an option as the creation of the transient objects is not at my hand.

Many thanks for any helpful comments,

Alde.[/list]


Top
 Profile  
 
 Post subject: In order to save transient you need a cascase = save
PostPosted: Tue Jul 03, 2007 2:38 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
and save only the item


Top
 Profile  
 
 Post subject: Re: Unable to persist associated transient objects
PostPosted: Tue Jul 03, 2007 6:38 pm 
Newbie

Joined: Tue Jul 03, 2007 12:05 pm
Posts: 2
avihaimar77 wrote:
[In order to save transient you need a cascase = save] and save only the item


Well, that was known; to quote an excerpt from my original posting:
alde wrote:
Not using "delete-orphan" or using "save-update" works, but is not what is intended.


In my situation the above suggestion is not an option because the actual project is using the Open-Session-In-View pattern (apart from this problem very successfully). This sometimes leads --- when combined with "cascade=save-update" --- to lots of unnecessary updates.

Regarding the initial question the last paragraph is already kind of offtopic; but who knows, maybe it serves as a motivation to participate in the thread ... ;-)

Alde.


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.