[jboss-user] [EJB 3.0] - Programmatic lookup of EM in Java SE way fails, but why?

henk53 do-not-reply at jboss.com
Sun Aug 16 13:50:06 EDT 2009


I'm porting an old Tomcat application that obtains an entity manager the Java SE way. I included the existing .war inside an EAR which defines among others the data source. The PU unit is defined by a persistence.xml inside the war's WEB-INF/classes/META-INF directory.

During deployment of the EAR, it's clear that the PU is getting picked up:


  | 19:21:10,695 INFO  [Version] Hibernate EntityManager 3.4.0.GA
  | 19:21:10,804 INFO  [Ejb3Configuration] Processing PersistenceUnitInfo [
  | 	name: example
  | 	...]
  | 19:21:11,151 WARN  [Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec correctly. PersistenceUnitInfo.getNewTempClassLoader() is null.
  | [...]
  | 19:21:12,187 INFO  [AnnotationBinder] Binding entity from annotated class: com.example.models.User
  | 19:21:12,187 INFO  [EntityBinder] Bind entity com.example.models.User on table users
  | 

However, when I try to obtain the EM using the following Java SE code inside a Servlet, an exception results. 

This is the code (simplified):


  | EntityManagerFactory factory = Persistence.createEntityManagerFactory("example");
  | entityManager = factory.createEntityManager();
  | entityManager.getTransaction().begin();
  | User user = entityManager.find(User.class, userId);
  | 

And this is the exception:


  | org.hibernate.MappingException: Unknown entity: com.example.models.User
  | 	at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
  | 	at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
  | 	at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
  | 	at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
  | 	at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
  | 	at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
  | 

When I take a look with a debugger in the following method where the exception is thrown:


  | public EntityPersister getEntityPersister(String entityName) throws MappingException {
  | 	EntityPersister result = (EntityPersister) entityPersisters.get(entityName);
  | 	if (result==null) {
  | 		throw new MappingException( "Unknown entity: " + entityName );
  | 	}
  | 	return result;
  | }
  | 

Then it seems that the map entityPersisters is always empty.

Now I understand that inside a container I should probably change the existing code to pick up the managed EM via JNDI. I wonder however (pure out of curiosity), why the Java SE way exactly fails. I seems to work, as the factory is returned, the entitymanager is created and the transaction is allowed to be started, but then upon actually requesting an entity it appears that the PU is empty.

Anyone has any inside on this? 


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

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



More information about the jboss-user mailing list