[jboss-user] [EJB 3.0] - Re: EntityManager not auto-flushing at end of SLSB method

mazz@jboss.com do-not-reply at jboss.com
Wed Jan 2 23:43:03 EST 2008


For sure you need to do the merge there (or a em.persist - em.merge is for overwriting data on a possibly existing entity -I suspect you really want to do em.persist here).

Just "new MyEntity()" doesn't really do anything - there is no magic here for your entity manager to know that object exists.  You need to put it in the JPA session - the only way for that to happen is to load it into the entity manager, either by asking the entity manager to persist/merge it (if it isn't in the DB yet or you want to overwrite it) or to find/load it (if it already exists in the DB)  Once the object is "attached" to the entity manager, THEN the magic happens (since its at that point the entity manager knows about your object and can manage it).

I don't know much about SEAM, but it sounds like the injected entity POJO isn't attached yet.  It sounds like its the same as if you passed in the entity from a remote client (which, it too, would not be attached).

If you get passed an entity that you know already exists, to load it into your entity manager:

public mySLSBMethod( MyEntity myentity ) {
   // attach myentity to the entity manager
   myentity = entityManager.find(MyEntity.class, myentity.getId());

I tend to only pass primary keys around, especially for APIs that I want my remote clients to use:

public mySLSBMethod( int myentityId ) {
   MyEntity me = entityManager.find(MyEntity.class, myentityId);

That's one way to load it - another way is to load it is via a named query.  Once loaded, now anything you do to myentity will be tracked and when the entity manager is flushed, those changes make its way to the DB.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4116602#4116602

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4116602



More information about the jboss-user mailing list