1 Reply Latest reply on Jan 25, 2006 11:50 AM by John Blackmore

    hibernate.default_schema setting in persistence.xml

    John Blackmore Newbie

      Now that I've deployed a collection of entity beans, reverse engineered from the database using Hibernate Tools 3.1, I'm having trouble doing a basic find operation from a stateless session bean. When I set the hibernate.show_sql option to "true" in my persistence.xml file, I noticed the SQL produced omitted my default_schema setting, however it does put a '.' before the table name. If I leave out the default_schema setting, it defaults to whatever is in the ejb3.deployer/META-INF/persistence.properties, if specified. I can't find a way to persuade Hibernate to use the default_schema setting in persistence.xml.

      Environment:

      JBoss 4.0.2
      EJB3.0 RC3patch1
      Java 1.5.0_06
      Hibernate 3.1.1
      JBoss IDE 1.5
      Hibernate Tools 3.1
      Oracle 9i Datasource
      Fedora Core 3 Linux

      Packaging:

      myApp.ear
       +-- META-INF
       MANIFEST.MF
       application.xml
       +-- lib
       myApp.ejb3
       myApp.par
       myApp.war
      

      application.xml
      <application>
       <display-name>My Application</display-name>
       <module>
       <java>lib/mylib.jar</java>
       </module>
       <module>
       <ejb>myApp.par</ejb>
       </module>
       <module>
       <ejb>myApp.ejb3</ejb>
       </module>
       <module>
       <web>
       <web-uri>myApp.war</web-uri>
       <context-root>/my-app</context-root>
       </web>
       </module>
       <library-directory>/lib</library-directory>
      </application>
      


      ejb3.deployer/META-INF/persistence.properties
      hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
      hibernate.connection.release_mode=after_statement
      hibernate.transaction.flush_before_completion=false
      hibernate.transaction.auto_close_session=false
      hibernate.query.factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory
      hibernate.hbm2ddl.auto=none
      hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
      # Clustered cache with TreeCache
      #hibernate.cache.provider_class=org.jboss.ejb3.entity.TreeCacheProviderHook
      #hibernate.treecache.mbean.object_name=jboss.cache:service=EJB3EntityTreeCache
      #hibernate.connection.datasource=java:/DefaultDS
      hibernate.connection.datasource=java:/LocalOracleDS
      hibernate.default_schema=XYZ
      hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
      hibernate.jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      hibernate.jndi.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
      


      MyClientStub.java:

      package org.ets.myapp.client;
      
      import java.rmi.RemoteException;
      
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;
      
      import org.ets.crater.interfaces.ItemSession;
      
      /**
       * File: MyClientStub.java
       * Package: alchemist
       *
       * @author jblackmore
       */
      public class MyClientStub {
      
       private static Context context;
      
       /**
       *
       */
       public MyClientStub(Context ctx) {
       super();
       context = ctx;
      
       }
      
       public static ItemSession getItemSessionBean() {
       ItemSession item = null;
       try {
       item = (ItemSession) context.lookup(ItemSession.class.getName());
       } catch (Exception e) {
       e.printStackTrace();
       }
       return item;
       }
      
      
       /**
       * @param context The context to set.
       */
       public static void setContext(Context context) {
       MyClientStub.context = context;
       }
      
       /**
       * @return Returns the context.
       */
       public static Context getContext() {
       return context;
       }
      
       public static void main(String[] args) throws NamingException {
       Context context = new InitialContext();
       context.addToEnvironment("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
       context.addToEnvironment("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
       context.addToEnvironment(Context.PROVIDER_URL, "jnp://localhost:1099");
       MyClientStub.setContext(context);
       ItemSession itemBean = MyClientStub.getItemSessionBean();
       try {
       itemBean.getItemData(args[0]);
       } catch (RemoteException e) {
       e.printStackTrace();
       } catch (Exception e) {
       e.printStackTrace();
       }
       }
      }
      


      Finally, the stateless bean, ItemSessionBean.java, savagely snipped to reveal just what I'm using...
      /*
       * Created on Jan 20, 2006
       */
      package org.ets.myapp.ejb3;
      
      
      public @Stateless class ItemSessionBean implements ItemSession {
      
       @PersistenceContext (unitName="MyEntityManager")
       private EntityManager entityManager; //=emf.createEntityManager();
      
       public ItemData getItemData(String itemCode) throws Exception {
       Itm itm = entityManager.find(Itm.class, itemCode);
       ItemData item = new ItemData(itemCode);
       // item.setField1(itm.getField1());
       // ...details snipped...
       return item;
       }
      
      }
      


      Now, given this configuration, I tried deploying my application twice, once with a default_schema setting in persistence.xml, once without.

      persistence.xml, with default_schema
      <entity-manager>
       <name>MyEntityManager</name>
       <jta-data-source>java:/LocalOracleDS</jta-data-source>
       <properties>
       <property name="hibernate.default_schema">ABC</property>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
       <property name="hibernate.hbm2ddl.auto" value="none"/>
       <property name="hibernate.show_sql" value="true"/>
       </properties>
      </entity-manager>
      


      Log after running client:
      2006-01-24 15:00:47,867 INFO [STDOUT] Hibernate: select itm0_.ITM_CDE as ITM1_20_0_, itm0_.STMLS_ID as STMLS2_20_0_, itm0_.ITM_PRMPT_TXT as ITM3_20_0_, itm0_.ITM_RBRC_TXT as ITM4_20_0_, itm0_.ITM_ACSN_CDE as ITM5_20_0_, itm0_.ITM_PRDCTN_AVLBL_FLG as ITM6_20_0_ from .ITM itm0_ where itm0_.ITM_CDE=?
      


      Then, after removing the default_schema setting from persistence.xml...
      persistence.xml, without default_schema
      <entity-manager>
       <name>MyEntityManager</name>
       <jta-data-source>java:/LocalOracleDS</jta-data-source>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
       <property name="hibernate.hbm2ddl.auto" value="none"/>
       <property name="hibernate.show_sql" value="true"/>
       </properties>
      </entity-manager>
      


      rebuild, redeploy, rerun client...
      Log after running client:
      2006-01-24 15:01:56,538 INFO [STDOUT] Hibernate: select itm0_.ITM_CDE as ITM1_115_0_, itm0_.STMLS_ID as STMLS2_115_0_, itm0_.ITM_PRMPT_TXT as ITM3_115_0_, itm0_.ITM_RBRC_TXT as ITM4_115_0_, itm0_.ITM_ACSN_CDE as ITM5_115_0_, itm0_.ITM_PRDCTN_AVLBL_FLG as ITM6_115_0_ from XYZ.ITM itm0_ where itm0_.ITM_CDE=?
      



      Is this a bug, or am I doing something wrong?

      Thanks,

        • 1. Re: hibernate.default_schema setting in persistence.xml
          John Blackmore Newbie

          I just answered my own question, or better put, I found the answer to a different question was the same...

          I just commented out a couple of lines in persistence.properties:

          hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
          hibernate.connection.release_mode=after_statement
          hibernate.transaction.flush_before_completion=false
          hibernate.transaction.auto_close_session=false
          hibernate.query.factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory
          hibernate.hbm2ddl.auto=none
          hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider
          # Clustered cache with TreeCache
          #hibernate.cache.provider_class=org.jboss.ejb3.entity.TreeCacheProviderHook
          #hibernate.treecache.mbean.object_name=jboss.cache:service=EJB3EntityTreeCache
          #hibernate.connection.datasource=java:/DefaultDS
          #hibernate.connection.datasource=java:/LocalOracleDS
           #hibernate.default_schema=XYZ
           #hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
          hibernate.jndi.java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
          hibernate.jndi.java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
          


          Now all the properties for this datasource are read from persistence.xml, as desired.