-->
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.  [ 7 posts ] 
Author Message
 Post subject: session.update question? does this even make sense
PostPosted: Fri Apr 23, 2004 12:58 am 
Newbie

Joined: Tue Oct 14, 2003 6:49 pm
Posts: 7
Finally! dove into hibernate.
Purchase order applicatoin.
Order that contains line items.
I can save, delete, and find Orders just fine.

What I would like to do
find and existing order O1 (i can do this)
delete some line items from O1 (client side, no db calls yet)
update O1 with hibernate and have the items removed from the order also removed from the database

What happens
hibernate (my code) updates the order, but the order that were deleted client side still remain in the database.

Code:
class Order {
    private String orderId;
    Set lineItems = new HashSet();
    public Set getLineItems() {...}
    public void setLineItems(Set lineItems) {...}
    public void addLineItem(LineItem lineItem) {...}
    public void deleteLineItem(LineItem lineItem {...}
    ...
}

class LineItem {
    private String lineItemId;
    private String orderId;
    private Order order;
    ...
}

<class name="Order" table="orders">
    <!-- <id> and <property> elements, nothing special -->
    <set name="lineItems" table="line_items" inverse="true" cascade="all">
        <key column="orderid" />     
        <one-to-many class="LineItem" />
    </set>
</class>

<class name="LineItem" table="lineitems">
    <!-- <id> and <property> elements, nothing special -->
    <many-to-one name="order" class="Order" column="orderid" />
</class>

// order object with some line items
Order order = nwe Order(...);
order.addLineItem(new LineItem(...));
order.addLineItem(new LineItem(...));

// create the order and get the new order with orderid
// order and line items are saved correctly, etc.
order = orderService.createOrder(order);

// delete a lineitem
// no call to hibernate just remove the line item from the order objects lineItems Set
order.deleteLineItem(lineItemToDelete);

// update the order through hibernate
// i want lineItemToDelete now deleted from line_items table
order = orderService.updateOrder(order)


I dont think deleting all the line items of O1 from the line_items table will work since its an update, nor i do think this is good practice, nor am i even sure my question makes since considering an update implies something to update. Maybe Ill have to keep a list of the lineItemId's deleted and delete them from the line_items table before updating O1 through hibernate.?

Thanks in Advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
use cascade="all-delete-orphan"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:19 am 
Newbie

Joined: Tue Oct 14, 2003 6:49 pm
Posts: 7
thats one quick ass response.

changed Order.hbm.xml <set> tag to:

<set name="lineItems" table="line_items" inverse="true" cascade="all-delete-orphan">
<key column="orderid" />
<one-to-many class="com.jacobs.itss.tracker.model.LineItem" />
</set>

Unfortanatley that throws an exception:
dao.DAOException: HibernateOrderDAO.updateOrder(): You may not dereference a collection with cascade="all-delete-orphan"
at HibernateOrderDAO.updateOrder(HibernateOrderDAO.java:43)
at OrderService.updateOrder(OrderService.java:25)
at OrderServiceTest.testUpdateOrder(OrderServiceTest.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:397)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:281)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:173)

Forgive the junit and eclipse trace above. I am sure this string is easy enough to find in the hibernate source but I have yet to set the hibernate project up yet, plus... its only my second day working with hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Like it says. You can't dereference that collection from your object model.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 1:54 am 
Newbie

Joined: Tue Oct 14, 2003 6:49 pm
Posts: 7
hmmm.... i gethere that. ;]
question is why cant hibernate dereference the collection?

figure hibernate is expecting the object reference returned by hibernate to use cascade="all-delete-orphan"

wrote a test that creates an order with line items an returns the order to ways.

order = session.saveOrUpdate(order);
and
order= session.find(query);

then immediately updated the order
session.saveOrUpdate(order);
and
session.update(order);

i believe the state and reference of the object to be unchanged.
am i wrong?

Thanks in advance again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 5:01 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
It depends on the code that is in your setLineItems method.

For example, if you have anything like the following:

Code:
public void setLineItems(Set lineItems) {
    getLineItems().addAll(lineItems);
}


Then that will produce the error you are seeing.

You need to directly set the set as so:

Code:
public void setLineItems(Set lineItems) {
    this.lineItems = lineItems;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 10:17 am 
Newbie

Joined: Tue Oct 14, 2003 6:49 pm
Posts: 7
thanks for the reply.

i should have mentioned before, that i made sure i was not creating new Set and setting lineItems to that set. well, i was doing it, but removed it, then wrote the test abaove. i see no place where i am chaning the reference of lineItems, wierd.

thanks again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.