-->
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: Annotation help for parent-child relationship
PostPosted: Thu Dec 22, 2005 1:23 pm 
Newbie

Joined: Sat Apr 10, 2004 12:14 am
Posts: 7
I have a parent-child relationship (Order-OrderLine) modeled as a one-to-many. The Order and OrderLine objects looks as follows:


Code:
@Entity
@Table(name = "order_header")
public class Order extends PersistentObject {
    private int version;
    private String name;
    private Set<OrderLine> orders;

    @Version
    public int getVersion() { return version;  }

    public void setVersion(int version) { this.version = version; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "order")
    public Set<OrderLine> getOrders() { return orders; }

    public void setOrders(Set<OrderLine> orders) { this.orders = orders; }
}

@Entity
@Table(name = "order_line")
public class OrderLine extends PersistentObject {
    private int quantity;
    private String description;
    private Order order;

    public int getQuantity() { return quantity; }

    public void setQuantity(int quantity) { this.quantity = quantity; }

    public String getDescription() { return description; }

    public void setDescription(String description) { this.description = description; }

    @ManyToOne
    public Order getOrder() { return order; }

    public void setOrder(Order order) { this.order = order; }
}



Note that "PersistentObject" is simply an embedded, serializable super class with an "id" attribute (@Id) and which overrides equals and hashcode.

Now to the problem. Whenever I call entityManager.merge(order), a database update (to the header) is ocurring, even if none of the Order or OrderLines changed! Here is the sql:

Code:
Hibernate: select order0_.id as id2_1_, order0_.name as name2_1_, order0_.version as version2_1_, orders1_.order_id as order4_3_, orders1_.id as id3_, orders1_.id as id0_0_, orders1_.order_id as order4_0_0_, orders1_.quantity as quantity0_0_, orders1_.description as descript3_0_0_ from order_header order0_ left outer join order_line orders1_ on order0_.id=orders1_.order_id where order0_.id=?
Hibernate: update order_header set name=?, version=? where id=? and version=?



Do I have the mappings set up correctly for this type of situation. It seems to work okay if I remove the version column but then I loose the optimistic locking semantics.

I'm using the latest Hibernate 3.1, Annotations 3.1 Beta 7, Entity Manager 3.1 Beta 5 along with MySql 5.0. I should also note that I am opening the entity manager in the servlet filter (EXTENDED) and using it for the duration of the thread so all object retrieval is occuring in the same session.

Any help is greatly appreaciated.

Dan.


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.