-->
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.  [ 3 posts ] 
Author Message
 Post subject: Annotation configuration : separating mappings from props.
PostPosted: Mon Nov 08, 2010 11:18 am 
Newbie

Joined: Mon Nov 08, 2010 11:01 am
Posts: 3
Hi all,

after reading several Hibernate reference documents as well as Googling around, I haven't found an answer to the following problem.
In an annotation based Hibernate configuration, I would like to separate de declaration of which classes are mapped, from other properties more closely coupled to the database driver and connection.

For now, I have a hibernate.cfg.xml in which I declare a hibernate-configuration element, in turn containing a session-factory element. The latter uses a list of <mapping class="..."> elements to specify which classes are persistent. The same session-factory element also describes, through properties, the database dialect, driver class and similar connection related characteristics.

As I'm using the same list of persistent classes across half a dozen different database configurations, I'd like to avoid cutting/pasting the <mapping class="..."> elements and use a kind of import mechanism. The imported file would then only contain this list of elements and would be reusable in all of the database configurations. Unfortunately, the only import mechanism seems to be relying on the <mapping resource="..."> element which needs an hbm.xml file for each persistent class. And I don't see how to build these files when using annotation based configuration.

So I'd be glad to examine any proposals allowing me to cleanly separate the declaration of mapped classes from the database related properties.

Many thanks,

MmM.


Top
 Profile  
 
 Post subject: Re: Annotation configuration : separating mappings from props.
PostPosted: Tue Nov 09, 2010 12:00 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Look at it from a non-Hibernate perspective but from a XML perspective.

Place all your mapping elements in a XML file and then import this file into your various configurations. That way your mappings can be shared across all session factory definitions and you'd have to look at only one place to manage your mappings.

Code:
Common-Mappings.xml (do NOT have the standard XML headers):
<mapping resource="app/model/Users.hbm.xml"/>
<mapping resource="app/model/Accounts.hbm.xml"/>



In hibernate.cfg.xml:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
[
<!ENTITY contentMappings SYSTEM "classpath://app/model/hbm/Common-Mappings.xml">
]>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
     … other properties …
    &commonMappings;
   </session-factory>



I have related question to you: If you are using annotation based configuration, why do you need the mappings? Do you have multiple entity mappings for any class?

HTH.


Top
 Profile  
 
 Post subject: Re: Annotation configuration : separating mappings from props.
PostPosted: Wed Nov 10, 2010 4:08 am 
Newbie

Joined: Mon Nov 08, 2010 11:01 am
Posts: 3
Thanks krishy for this insight.

And indeed, I have split the hibernate configuration in two files, but not exactly in the way you suggest. Primarily, because, as I'm using annotation based persistence, there are no class specific mapping files. But also thanks to the fact I'm using Spring.

Thus, in a first bean configuration file, which is database agnostic, I declare a sessionfactory listing the persistent classes, and a set of DAO service implementations handling these classes as follows :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="myDataSource" />
      <property name="hibernateProperties">
         <ref bean="hibernateProperties"/>
      </property>
      <property name="annotatedClasses">
         <list>
            <value>com.example.dao.entities.MyFirstEntity</value>
            <value>com.example.dao.entities.MySecondEntity</value>
                                ...
         </list>
      </property>
   </bean>

   <bean id="txManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <tx:annotation-driven transaction-manager="txManager" />

   <bean id="myFirstEntityService" class="com.example.dao.services.MyFirstEntityServiceImpl">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

   <bean id="mySecondEntityService" class="com.example.daos.ervices.MySecondEntityServiceImpl">
      <property name="sessionFactory" ref="sessionFactory" />
   </bean>

</beans>


As you'll notice, this file contains references to two external beans : myDataSource and hibernateProperties. These beans are defined in a separate XML file, and, their content depends on the actual configuration of the database access. As an example, here's how they are defined when running unit tests, in one of the project modules :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="hibernateProperties" class="java.util.Properties">
      <constructor-arg>
      <props>
          <prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
          <prop key="hibernate.connection.url">jdbc:hsqldb:file:/tmp/daotest</prop>
          <prop key="hibernate.connection.username">sa</prop>
          <prop key="hibernate.connection.password"></prop>
          <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
          <prop key="hibernate.hbm2ddl.auto">create</prop>
          <prop key="show_sql">true</prop>
       </props>   
      </constructor-arg>
   </bean>

   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="properties">
          <ref bean="hibernateProperties"/>
       </property>
   </bean>

   <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
      destroy-method="close">
      <property name="driverClassName" value="${hibernate.connection.driver_class}" />
      <property name="url" value="${hibernate.connection.url}" />
      <property name="username" value="${hibernate.connection.username}" />
      <property name="password" value="${hibernate.connection.password}" />
   </bean>
</beans>


I tried using a Spring <import resource="..."> statement to include the second file in the first one, but as the arguments of these statements are relative to the location of the importing file this wouldn't work in all scenarios (such as when the filename of the XML file with the connection characteristics is given with a command-line argument). So I adapted my applications to make them load the two files, either in the same Spring XmlApplicationContext if both are located in the classpath, or in two different XmlApplicationContexts if they come from different sources.

As you can see, it was quite an investment (and, granted, it has become more of a Spring topic rather that a Hibernate one) but I've got the feeling that it's starting to pay off.

Cheers,

MmM


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