-->
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.  [ 1 post ] 
Author Message
 Post subject: Passing EntityManager reference between methods doesn't work
PostPosted: Thu Aug 19, 2010 11:47 pm 
Newbie

Joined: Sun Jul 05, 2009 9:59 pm
Posts: 6
I apologize in advance for a lengthy post but I guess I need to support my query with code.

I am coding for a system which has 12 database instances. All databases have the same number and name of tables but the data is different (of course). Depending on a URL parameter passed from an external application, I need to choose one of the 12 databases at runtime.
I have coded a EntityFactory wrapper which takes in the URL parameter and returns an appropriate EntityManager instance using Reflection.
Problem is that the returned EntityManager, when used in find, is not loading the Entity attributes as it should. Not surprisingly, find works inside the EntityFactory wrapper.
Code:
public class ApplicationSpecificEntityManagerFactoryImpl implements
   ApplicationSpecificEntityManagerFactory {

    @Override
    public EntityManager getEntityManager(String persistenceUnitname) {

   Field[] declaredFields = getClass().getDeclaredFields();
   PersistenceContext annotation = null;
   for (Field field : declaredFields) {
       if (field.getType() == EntityManager.class) {
      annotation = field.getAnnotation(PersistenceContext.class);

      if (annotation != null
         && persistenceUnitname.equals(annotation.unitName())) {
          try {
         EntityManagerFactory emf = Persistence
            .createEntityManagerFactory(persistenceUnitname); // I don't do this in a Container, rely on DI
         EntityManager em = null;

         ApplicationParameter applicationParameter = null;
         ApplicationParameterId id = new ApplicationParameterId(
            "GCO", (short) 4, "LDAP", (short) 1);

         field.set(this, emf.createEntityManager());
         
//         em = emf.createEntityManager();
         applicationParameter = ((EntityManager) field.get(this)).find(
            ApplicationParameter.class, id);

         System.out.println("value in ApplicationSpecificEntityManagerFactoryImpl: " + applicationParameter.getParamValue()); // prints LDAP
         
         return (EntityManager) field.get(this);
//         return em;
          } catch (IllegalArgumentException ex) {
         ex.printStackTrace();
          } catch (IllegalAccessException ex) {
         ex.printStackTrace();
          }
      }
       } else {
      continue;
       }
   }

   return null;
    }

    @PersistenceContext(unitName = "myPU")
    private EntityManager myEm;
    @PersistenceContext(unitName = "hisPU")
    private EntityManager hisEm;
}

Main class:
Code:
public class JpaTest {

    public static void main(String[] args) {
   ApplicationSpecificEntityManagerFactory entityManagerFactory = new ApplicationSpecificEntityManagerFactoryImpl();
   ApplicationParameterManager applicationParameterManager = new JpaApplicationParameterManagerImpl(
      entityManagerFactory);
   applicationParameterManager.setPlantCode("myPlant");

   String value = applicationParameterManager
      .getApplicationParameterValue(ApplicationConstants.AUTHENTICATION_PARAMETER);

   System.out.println("value in JpaTest: " + value); // prints null
    }
}

Persistence.xml snippet:

Code:
<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.x.y.dashboard.persistence.ApplicationParameter</class>
        <class>com.x.y.dashboard.persistence.ApplicationParameterId</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test"/>
            <property name="javax.persistence.jdbc.user" value="appadmin"/>
            <property name="javax.persistence.jdbc.password" value="xxx"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>


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

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.