users@glassfish.java.net

Cannot get id of managed object instance in session bean method

From: <glassfish_at_javadesktop.org>
Date: Thu, 19 Jul 2007 13:36:58 PDT

I do not understand the following behavior of Glassfish Beta 3 (b50g-beta3). The code is reduced as much as possible.

The strange thing is that inside the session bean method "testSaveParent" I cannot get the id of the child with "child.getId()", but in the test client where the session bean method is invoked, the id can be retrieved from the same object with getId().

As far as I had understood the "child" object should be managed after the "em.persist(child);" call in the session bean method, and it should be possible to get the id inside the session bean method. But the statement "System.out.println("Session bean get new id " + child.getId());" will always print "Session bean get new id null".

Maybe someone can point out what is wrong with my code, or whether this might be a defect in Glassfish.

Thanks
Stephan

------------------- Child class -----------------
@Entity
public class Child implements Serializable {
        private Long id;
        private Parent parent;
        
        @ManyToOne
        public Parent getParent() {
                return parent;
        }

        public void setParent(Parent parent) {
                this.parent = parent;
        }

        @Id
        @GeneratedValue
        public Long getId() {
                return id;
        }

        public void setId(Long id) {
                this.id = id;
        }
}
--------------- Parent class -------------
@Entity
public class Parent implements Serializable {
        private Long id;
        private List<Child> children;
        
        @OneToMany(mappedBy="parent")
        public List<Child> getChildren() {
                return children;
        }

        public void setChildren(List<Child> children) {
                this.children = children;
        }

        @Id
        @GeneratedValue
        public Long getId() {
                return id;
        }

        public void setId(Long id) {
                this.id = id;
        }
}

---------------- Session bean method -------------------
@Stateless
@TransactionManagement(value=TransactionManagementType.CONTAINER)
public class CustomerSession implements CustomerSessionRemote, CustomerSessionLocal {
    
    @javax.persistence.PersistenceContext(unitName="backoffice")
    private EntityManager em;
         
        public Child testSaveParent(Parent parent) {
                em.persist(parent);
                
                Child child = new Child();
                child.setParent(parent);
                
                parent.getChildren().add(child);
                
                em.merge(parent);
                em.persist(child);
                
                System.out.println("Session bean get new id " + child.getId());
                
                return child;
        }
}
--------- invoking the session bean method ------------------
                Parent parent = new Parent();
                Child child = session.testSaveParent(parent);
                
                System.out.println("id is " + child.getId());
[Message sent by forum member 'smuehlst' (smuehlst)]

http://forums.java.net/jive/thread.jspa?messageID=227343