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.  [ 3 posts ] 
Author Message
 Post subject: merge does not populate id?
PostPosted: Wed Apr 26, 2006 6:20 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
I am sure this has been asked before, but the keywords I used for the search did not help.

Why does the below test fail? Specifically, I expected the id to be non-null, but it was null.

Code:
   public void testIdAfterMergeNotNull() {
      EntityManager mgr = openSession();
      mgr.getTransaction().begin();      
      
      ParentDBO parent = new ParentDBO();
      parent.setName("notnullID");
      mgr.merge(parent);
      
      //NOTE: I think this should be non-null but the test fails
                //because it is null
      assertNotNull(parent.getId());
      
      mgr.getTransaction().commit();
      mgr.close();
   }


Just in case it helps, here is the bean that goes with it....

Code:
@Entity
@Table( name = "Parents")
@NamedQueries(
   {
      @NamedQuery(name="getParents", query="SELECT c FROM ParentDBO c")
   })
public class ParentDBO {

   private Long id;
   private int version;
   private String name;
   private Collection<ChildDBO> children;
   
   @Column(name="Id")
   @Id()
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   @SuppressWarnings("all")
   private void setId(Long id) {
      this.id = id;
   }
   
   @Version
   @Column(name="Version", nullable=false)
   public int getVersion() {
      return version;
   }
   @SuppressWarnings("all")
   private void setVersion(int version) {
      this.version = version;
   }
   
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   
   @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);
      
      //NOTE: testing
      setChildren(getChildren());
   }
   
   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);
   }
}

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 8:40 am 
Senior
Senior

Joined: Mon Aug 22, 2005 5:45 am
Posts: 146
i guess the identifier of your class is id, not name.
so if you want to merge, you need to set the identifier.

_________________
Please don't forget to give credit, if my posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 5:29 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
actually no. I finally figured it out today a few days later. The EJB3 works different. for hibernate to set the id, you have to do this.....

bean = mgr.merge(bean)

In hibernate apis, the method would set the id of the bean. In EJB3, it doesn't but it returns a new bean where the id is set.
dean


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