-->
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: Extra update after insert for one to many relationship
PostPosted: Mon Jul 16, 2012 5:15 pm 
Newbie

Joined: Mon Jul 16, 2012 5:05 pm
Posts: 1
I have a parent child relationship with automatic versioning (optimistic locking). We have noticed when we create a new parent and child and then save it, Hibernate performs an insert on each table (parent and child) and then does an additional update on the parent.

If I remove the version number the update does not happen, so I presume it is updating the version number because of the child entity. This will be doing high volume so the additional update places a large overhead on the insertion, so is there a way of stopping this from happening?

We are using Hibernate 3.6.10. I have created simple example of the classes below.

Code:
@Entity
public class Parent {
    @EntityField(display=false)
   protected Long id;

   private Set<Child> children = new HashSet<>();
   private String name;

   @EntityField(display=false)
   private int version = 0;

      
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   
   public void setId(Long id) {
      this.id = id;
   }

   @OneToMany(mappedBy = "parent",orphanRemoval=true,cascade = {CascadeType.ALL}, fetch=FetchType.EAGER)
   public Set<Child> getChildren() {
      return children;
   }
   public void setChildren(Set<Child> children) {
      this.children = children;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   @Version
   public int getVersion() {
      return version;
   }

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


}

@Entity
public class Child {
    @EntityField(display=false)
   protected Long id;

   private Parent parent;
   private String name;
   
    @EntityField(display=false)
   private int version = 0;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   
   public void setId(Long id) {
      this.id = id;
   }

    @ManyToOne(cascade=CascadeType.REFRESH)
    @JoinColumn(name = "parentId")
   public Parent getParent() {
      return parent;
   }
   public void setParent(Parent parent) {
      this.parent = parent;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   @Version
   public int getVersion() {
      return version;
   }

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



}



and this is what I see in the logs

Hibernate: insert into Parent (name, version) values (?, ?)
Hibernate: insert into Child (name, parentId, version) values (?, ?, ?)
Hibernate: update Parent set name=?, version=? where id=? and version=?


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.