-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to determine EntityManager in ConstraintValidator
PostPosted: Tue Aug 30, 2011 2:57 am 
Newbie

Joined: Tue Aug 30, 2011 2:48 am
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: How to determine EntityManager in ConstraintValidator
PostPosted: Mon Sep 12, 2011 3:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

this seems to be a Glassfish specific question. Have you tried to ask your question on the Glassfish forum/mailinglist? I am not aware of a way of doing this, but if you find out make sure to update this thread.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.