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.  [ 2 posts ] 
Author Message
 Post subject: merge(parent). 2 inserts and one update
PostPosted: Sun Apr 23, 2006 7:12 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I have the following code that according to hibernate logging sql results in
1. insert parent
2. insert child
3. update parent(why is this happening?)

Here is the transaction code....

Code:
      mgr.getTransaction().begin();
      
      ParentDBO parent = new ParentDBO();
      parent.setName("parent");
      ChildDBO child = new ChildDBO();
      child.setName("child");
      parent.addChild(child);
      
      mgr.merge(parent);

      mgr.getTransaction().commit();


Here is the relevant ParentDBO code
Code:
   @Column(name="parent_id")
   @OneToMany(mappedBy="parent", cascade = CascadeType.ALL, fetch=FetchType.EAGER)   
   public Collection<ChildDBO> getChildren() {
      if(children == null) {
         children = new ArrayList<ChildDBO>();
      }
      return children;
   }
   public void setChildren(Collection<ChildDBO> children) {
      this.children = children;
   }
   public void addChild(ChildDBO child) {
      if(child == null)
         throw new IllegalArgumentException("child may not be null and was");
      else if(child.getParent() != null)
         child.getParent().getChildren().remove(child);
      
      child.setParent(this);
      getChildren().add(child);
   }
   
   public void removeChild(ChildDBO child) {
      if(child == null)
         throw new IllegalArgumentException("child may not be null and was");
      else if(!getChildren().contains(child))
         throw new IllegalArgumentException("Parent does not contain this child");
      
      child.setParent(null);
      getChildren().remove(child);
   }


Here is the relevant ChildDBO code....
Code:
   @ManyToOne
   @JoinColumn(name="parent_id")
   public ParentDBO getParent() {
      return parent;
   }
   public void setParent(ParentDBO parent) {
      this.parent = parent;
   }   


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 9:26 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
turns out it is because I have a @version field. This seems not right? anyone from the hibernate team can answer this?
thanks,
dean


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