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