I am working on a project based amongst others on Weblogic AS / JPA-Hibernate + Hibernate Validator. Unit tests occur by means of embedded glassfish
In order to create database-driven Constraint Validators, I made an 'EntityManagerAwareConstraintValidatorFactory' similar to your SessionAwareConstraintValidatorFactory mentioned at 'http://community.jboss.org/wiki/AccessingTheHibernateSessionWithinAConstraintValidator'.
A getInstance() method is used because at one point I noticed that there were 2 ConstraintValidatorFactories created (when inside GFEmbedded):
Code:
private EntityManager entityManager;
private static EntityManagerAwareConstraintValidatorFactory instance;
public static EntityManagerAwareConstraintValidatorFactory getInstance()
{
return instance;
}
public EntityManagerAwareConstraintValidatorFactory() {
instance = this;
}
I need to 'inject' the EntityManager into this factory.
For Unit tests outside of the container this is quite easy:
Code:
entityManagerFactory = Persistence.createEntityManagerFactory( "MasterDataLookupPULocal" );
entityManager = entityManagerFactory.createEntityManager();
EntityManagerAwareConstraintValidatorFactory.getInstance().setEntityManager(entityManager);
For Unit tests in the Embedded Glassfish, it is more complex. One cannot lookup the EM through JNDI lookup, as embedded-GF does not store it overthere.
So in the Unit test we need to write something like:
Code:
Map<String, Object> props = new HashMap<String, Object>();
props.put( EJBContainer.APP_NAME, MODULE_NAME );
ejbContainer = EJBContainer.createEJBContainer( props );
context = ejbContainer.getContext();
EntityManagerAwareConstraintValidatorFactory.getInstance().setEntityManager( ????em???? );
But I have no idea how to determine an entitymanager in a unit test where I just created the EJB container...
Can you help me further?
Regards,
Luc