суббота, 1 ноября 2008 г.

Seam 2.1.0GA and JBoss 5.0.0CR2

As seam and jboss are developed by the same team, I would expect them to be compatible. But in reality they are not =)

So if you download the versions I mentioned in the subject and try to generate a default project by seam-gen (including db access) then there wil be a surprise for you. Deploying this project to jboss won't actually work. When it wants to connect to the database it will suddenly go:
 EntityManagerFactory not found in JNDI 


Googling that string was quite painful, but after couple of hours I finally got a result!
Someone commented out something from jboss codebase and seam doesn't know about it =)

So if you're looking for a solution, you should be using another way of binding your entity manager, not jndi. To do that in j2ee enviroment (which i suppose you would have running jboss) you need to create a small helper class that will translate the entitymanager from jboss world to seam world:

@Stateless
@Name("entityManagerFactory")
public class EntityManagerFactoryHackBean implements EntityManagerFactoryHackLocal {

@PersistenceUnit(name="main")
EntityManagerFactory factory;

@Unwrap
public EntityManagerFactory getEntityMangagerFactory()
{
return factory;
}

}

see, having both stateless and name annotations it serves like a bridge between two worlds.

then you have to use it in your components.xml instead of jndi name:
<persistence:managed-persistence-context name="entityManager"
entity-manager-factory="#{entityManagerFactory}"
scope="conversation" auto-create="true" />

Thanks for this thread on jboss forum

There is another thread which has a solution without ugly hacks, but it doesn't work in j2ee env and a small amendment that should make it work even in j2ee env. Btw this second thread tells us that this should be fixed in a consequent release of jboss.