-->
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.  [ 8 posts ] 
Author Message
 Post subject: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Mon Nov 23, 2009 4:04 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Hi,

I already searched this forum and google, but couldn't find exactly what I was looking for.

I would like to have an extra NamingStrategy, called TestNamingStrategy which concats all my table names with "_TEST".
While in normal mode, I would like to use the DefaultNamingStrategy; But while running the unit tests I would like to use the TestNamingStrategy.

I have the following HibernateUtils class implemented in a separate utils project outside the project I'm working on and I would like to be able to use this single class (which is also unit tested).
I think this HibernateUtil class was practically taken from the Hibernate Community FAQs or something. I'll leave the comments and Javadocs out.
Code:
public class HibernateUtils {

   private static SessionFactory sessionFactory;

   static {
      HibernateUtils.init();
   }

   public static void init() {
      Configuration config = HibernateUtils.getInitializedConfiguration();
      sessionFactory = config.buildSessionFactory();
   }

   public static Configuration getInitializedConfiguration() {
      Configuration config = new Configuration();
      config.configure();
      return config;
   }

   public static Session getSession() {
      Session hibernateSession = sessionFactory.getCurrentSession();
      return hibernateSession;
   }

   public static void flushSession() {
      HibernateUtils.getSession().flush();
   }

   public static void closeSession() {
      HibernateUtils.getSession().close();
   }

   public static void recreateDatabase() {
      Configuration config;
      config = HibernateUtils.getInitializedConfiguration();
      new SchemaExport(config).setDelimiter(";").setFormat(true).setOutputFile("./target/schema-export.sql").create(
            false, true);
   }

   public static Session beginTransaction() {
      Session hibernateSession;
      hibernateSession = HibernateUtils.getSession();
      hibernateSession.beginTransaction();
      return hibernateSession;
   }

   public static void commitTransaction() {
      HibernateUtils.getSession().getTransaction().commit();
   }

   public static void rollbackTransaction() {
      HibernateUtils.getSession().getTransaction().rollback();
   }

   public static void main(String args[]) {
      HibernateUtils.recreateDatabase();
   }

}


Problem is, I can only switch NamingStrategy inside the code where I'm creating the Configuration.
Apparently I cannot use hibernate.cfg.xml to specify which NamingStrategy to use (I have separate hibernate.cfg.xml files for the project and for the unit tests).

I cannot dynamically pass a TEST_MODE parameter of some sorts due to the static character of the class, nor can I override specific methods.

Can anyone give me some advice as how to manage this, except hard-coding my Unit Test mapping files to
Code:
... table="FOO_TEST">...
?

Thanks in advance, and kind regards.

Don Stevo


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 6:13 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
try to set the naming-strategy in your conf:
Code:
<property name="hibernate.naming_strategy" value="com.you.YourNamingStrategy"/>

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 7:49 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
According to the documentation I'm finding (also on this forum) about the possible properties that can be given in the hibernate.cfg.xml or hibernate.properties, this cannot be set in XML or as a property.

But I will try it out this evening. I was trying <property name="namingStrategy">packagename.TestNamingStrategy</property> for the moment, but maybe yours works.


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 7:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I am also not so sure about pure hibernate, but I know for sure that it works when using jpa (hibernate.ejb.naming_strategy).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 8:02 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Yeah, found thatone too but then through Annotation configuration.

At the moment I'm using Hibernate-core with HBM.XML mapping files and hibernate.cfg.xml to try to develop a small PoC on the Oracle HR example schema, because I am (Or at least I USED TO BE) more experienced in core hibernate than I am with the annotations and stuff...
I did not work with entitymanager and JPA yet.

But I write it in core first; whereafter I will try to see how much effort it takes to migrate it to JPA (trying to design it properly to minimize the workload and risk of the migration).


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 2:12 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
The following hibernate.cfg.xml doesn't seem to be working. Hibernate still resolves to the normal tables, and not the _TEST tables.
Code:
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
      <property name="hibernate.connection.username">hr</property>
      <property name="hibernate.connection.password">hr</property>
      <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
      <property name="hibernate.naming_strategy">...utils.hibernate.core.TestNamingStrategy</property>

Any other ideas?
When I map the .hbm.xml files under "test/resources" hard coded to the _TEST tables, obviously it works.
The "main/resources" mapping files are still mapped to the regular tables.

But I would like to have a more dynamic approach.
Ofcourse, problem is ... when I alter one of the regular tables, I have to make sure to alter the _TEST tables as well ... or I could use a separate database schema.
Fact is, I don't want to mess up the Oracle HR example schema so I want to keep the two environments (development / test) separate.


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Tue Nov 24, 2009 6:27 pm 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
I might 've found a better solution.

I'm now using an extended version of the CaveatEmptor HibernateUtil class. I made it more CheckStyle conform and added some convenience methods like beginTransaction, commitTransaction, flushCurrentSession, and stuff like that.

There are getConfiguration() and rebuildSessionFactory(Configuration config) methods in there.

I might write a (generic or, for each separate project, "copied") super class AbstractHibernateBeanTest where I rebuild the SessionFactory with the TestNamingStrategy in the setUp() method, and shutdown the SessionFactory in the tearDown() method.

It's still not configurable through XML (for Hibernate 3 Core) though...


Top
 Profile  
 
 Post subject: Re: Dynamic NamingStrategy (Normal / Test mode)
PostPosted: Thu Nov 26, 2009 10:35 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Does anyone have another solution?


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