-->
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.  [ 9 posts ] 
Author Message
 Post subject: Programmatic configuration.
PostPosted: Wed Mar 22, 2006 9:13 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
I'm trying to use the org.hibernate.ejb.Ejb3Configuration class to configure Hibernate and create an EntityManagerFactory.

Are the properties which you set in that the same as the ones in org.hibernate.cfg.Configuration?

The problem is, Hibernate is not finding my annotated entity beans.

Before, when I used persistence.xml with

Code:
<persistence>
   <persistence-unit name="gf">
      <description>The base set of green fields' tables</description>
      <jta-data-source>java:/GreenFieldsDS</jta-data-source>
      <properties>
         <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" />
         <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.ejb.naming_strategy" value="com.fcl.gf.naming.FCLNamingStrategy" />
         <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
   </persistence-unit>
</persistence>

...

   emf = Persistence.createEntityManagerFactory("gf");


it eagerly looked for them on application deployment, and I saw the logging output

Code:
15:12:36,359 INFO  [AnnotationBinder] Binding entity from annotated class: com.fcl.greenfield.Language
15:12:36,359 INFO  [EntityBinder] Bind entity com.fcl.greenfield.Language on table Language


for each entity binding them to their tables.

Now, application deployment causes verry little logging. And when I try to get an EntityManagerFactory, I do

Code:
   String name="GreenFieldsDS";

...
   Ejb3Conf = new Ejb3Configuration();
   Ejb3Conf.setProperty("hibernate.connection.datasource", "java:/" + name);
   Ejb3Conf.setProperty("hibernate.cache.provider_class", "org.jboss.ejb3.entity.TreeCacheProviderHook");
         Ejb3Conf.setProperty("hibernate.treecache.mbean.object_name", "jboss.cache:service=EJB3EntityTreeCache");
   Ejb3Conf.setProperty("hibernate.show_sql", "true");
   Ejb3Conf.setProperty("hibernate.ejb.naming_strategy", "com.fcl.gf.naming.FCLNamingStrategy");
   Ejb3Conf.setProperty("hibernate.hbm2ddl.auto", "update");
         Logger.info("Ejb3Configuration.createEntityManagerFactory()");
   emf = Ejb3Conf.createEntityManagerFactory();


Which I'm hoping has the same effect, but I don't see the binding of annotated entities on deployment, and when I issue a query, it says

Code:
11:18:38,468 WARN  [QuerySplitter] no persistent classes found for query class: select c from com.fcl.greenfield.UserGroup as c


How do I configure with org.hibernate.ejb.Ejb3Configuration and get it to scan for annotated entities?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 7:52 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
There is no information to be had anywhere on this!

If I use the pure Hibernate startup classes, org.hibernate.cfg.Configuration, it doesn't scan the jars to find EJB3 entities.

When I put a persistence.xml into my .par file, it scans on deployment. Does it in fact create an EntityManagerFactory for each persistence unit at this time?

How do I access these rather than create my own EntityManagerFactory instances with an org.hibernate.ejb.Ejb3Configuration?

Basically, I don't want to have EntityManagerFactory instances created willy-nilly and hanging around, I want to have one for each persistence unit, and always access that.

Anybody have any information? I've been trying loads of things, but it's all complete guesswork because the javadocs are minimal.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 23, 2006 11:24 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
OK, figured out how to do what I want.

The thing is, we're using JBoss, and we can't use injected EntityManagers because we need to be flexible about which PersistenceUnit we access.

Anyway, the EntityManagerFactory instances created by JBoss on deployment can be made available as I have done in my persistence.cml below{

Code:
<persistence>
   <persistence-unit name="Core">
      <description>The base set of green fields' tables</description>
      <jta-data-source>java:/CoreDS</jta-data-source>
      <properties>
      <property name="jboss.entity.manager.factory.jndi.name" value="java:/CoreFactory"/>
         <property name="hibernate.hbm2ddl.auto" value="update" />
         <!--property name="hibernate.ejb.cfgfile" value="hibernate_config.xml"/-->
      </properties>
   </persistence-unit>
   <persistence-unit name="GreenFields">
      <description>The base set of green fields' tables</description>
      <jta-data-source>java:/GreenFieldsDS</jta-data-source>
      <properties>
      <property name="jboss.entity.manager.factory.jndi.name" value="java:/GreenFieldsDSFactory"/>
         <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" />
         <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.ejb.naming_strategy" value="com.fcl.gf.naming.FCLNamingStrategy" />
         <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
   </persistence-unit>
   <persistence-unit name="GreenFieldsAlt">
      <description>The base set of green fields' tables</description>
      <jta-data-source>java:/GreenFieldsAltDS</jta-data-source>
      <properties>
      <property name="jboss.entity.manager.factory.jndi.name" value="java:/GreenFieldsAltDSFactory"/>
         <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" />
         <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.ejb.naming_strategy" value="com.fcl.gf.naming.FCLNamingStrategy" />
         <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
   </persistence-unit>
</persistence>


You see the property jboss.entity.manager.factory.jndi.name. That is the JNDI name under which the EntityManagerFactory for that PersistenceUnit is bound. So I can get EntityManagers for whichever PersistenceUnit is indicated at run time.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 25, 2006 9:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you detail why PU injection does not work for you?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 5:44 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
emmanuel wrote:
Can you detail why PU injection does not work for you?


Because it would mean hardcoding the PU name:

Code:
@PersistenceUnit(name="Greenfields");


In fact, the PU is decided by who the logged on user is, so we have

Code:
...

public @Stateless
class EntityMgrBean implements EntityMgr
{
   @Resource private SessionContext ctx;

   private EntityManager getUserEntityManager()
   {
      GreenfieldsUser user = (GreenfieldsUser)ctx.getCallerPrincipal();
      Logger.info("Finding EntityManager for " + user);
      EntityManagerFactory hibernateEMF = null;
      try
      {
         hibernateEMF = (EntityManagerFactory)new InitialContext().lookup("java:/" + user.getDatabaseResource()+ "Factory");
      }
      catch (Exception e)
      {
         throw new IllegalStateException("Unable to obtain entity manager factory", e);
      }
      EntityManager em = hibernateEMF.createEntityManager();
      em.getTransaction().begin();
      return em;
   }

...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 7:01 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
then you should probably do

@PersistenceUnits(
@PersistenceUnit(name="emf/pus", unitName="tempdb")
) //to get the EMF
@PersistenceContexts(
@PersistenceContext(name="em/pc", unitName="tempdb")
) to get the EM
@Stateless
public Class MyClass {
...
emf = (EntityManagerFactory)ctx.lookup("emf/pu");

em = (EntityManager)ctx.lookup("em/pc");


Since the number of PU is probably fixed

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 8:49 am 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
emmanuel wrote:
then you should probably do

@PersistenceUnits(
@PersistenceUnit(name="emf/pus", unitName="tempdb")
) //to get the EMF
@PersistenceContexts(
@PersistenceContext(name="em/pc", unitName="tempdb")
) to get the EM
@Stateless
public Class MyClass {
...
emf = (EntityManagerFactory)ctx.lookup("emf/pu");

em = (EntityManager)ctx.lookup("em/pc");


Since the number of PU is probably fixed


It's dependent upon runtime conditions. The persistence unit (which schema in the database we want to get our data from) might be changed at any time.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 12:13 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 9:38 am
Posts: 61
Location: Notts, UK
HOw can I get access to the EJB3Configuration object that JBoss uses to do its startup?

I could use

Code:
setInterceptor(org.hibernate.Interceptor interceptor)


in that to set my interceptor up. Cuold I? If you change a configuration that has already been "used" to set the EJB3 system up, do any subsequent changes "take"?

It does not seem possible to customize the configuration, or use our own EntityManagerFactories from within JBoss.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 4:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You can't.
But you can define hibernate.ejb.interceptor in your persistence.xml, so that you don't need to creat an Ejb3configuration

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.