[jboss-user] [JBoss Seam] - Re: EntityManager: For newbies or to close gaps in your know

petemuir do-not-reply at jboss.com
Thu Aug 10 08:30:29 EDT 2006


Very useful - nice work - might be worth mentioning this in the EJB3/Hibernate forums as it far less confusing than the Hibernate EntityManager reference manual.

anonymous wrote : One of the properties must be an Id (mostly a Long) and the class itself must be annotated with @Entity.

Why a long? (Not come across this before)

anonymous wrote :  This needn't happen immediately, he might keep your entity in the cache and write it to the database later.
By default flushes occurs at the method boundary.

anonymous wrote : (Just by the way: Whenever a RuntimeException occurs during a transaction a rollback is performed and all changes to data get revoked).

Just RuntimeException? Not all exceptions? Is it EJB3 or the SeamExceptionFilter that does the rollback?

anonymous wrote : So there's an easy solution: We extend the lifetime of the persistence context so that the entities remain managed during calling multiple transactions/methods. Therefore we change the injection of the EntityManager:
  | @PersistenceContext(type=PersistenceContextType.EXTENDED
  |   | private EntityManager em;
  | Now the managed entities "live" in an Extended Persistence Context

The Seam Managed Persistence Context takes this further and provides a conversation scoped entity manager.  This prevents Lazy Initialisation Exceptions from occuring when accessing entities from earlier requests.

It needs to be set up in components.xml (see the Configuration section of the reference doc), and then can be accessed via (where mySMPC is the name it was configured under)

@In(create=true) private EntityManager mySMPC;

anonymous wrote : ..."from Entity where lastName=".nameToSearchFor." order by firstName"

It is recommended not to do this but to do

em.createQuery("select e from Entity e where e.lastName = :lastname order by e.firstName").setParameter("lastName", lastName).getResultList();

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

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



More information about the jboss-user mailing list