How to configure Hibernate when you are using a Spring Session Factory

jax picture jax · Apr 19, 2013 · Viewed 14.6k times · Source

I am trying to setup Hibernate Tools in eclipse. The trouble is that it can't find any mapping files.

I have created a console configuration that points to my environment.properties file and hibernate.cfg.xml. The trouble is that there are no mappings in hibernate.cfg.xml.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
  </session-factory>
</hibernate-configuration>

it seems that it is using the spring bean sessionFactory in myproject-persistence.xml (below) to find the required mapping files. I can't see anywhere that this file can be added to the the Console Configuration in eclipse.

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
      <property name="driverClass" value="${hibernate.connection.driver_class}" />
      <property name="jdbcUrl" value="${hibernate.connection.url}" />
      <property name="user" value="${hibernate.connection.username}" />
      <property name="password" value="${hibernate.connection.password}" />
      <property name="initialPoolSize" value="5" />
      <property name="minPoolSize" value="5" />
      <property name="maxPoolSize" value="25" />
      <property name="acquireIncrement" value="5" />
      <property name="maxIdleTime" value="1800" />
      <property name="numHelperThreads" value="5" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">${hibernate.dialect}</prop>
          <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
        </props>
      </property>
      <property name="configLocation" value="classpath:hibernate.cfg.xml" />
      <property name="mappingLocations">
        <list><value>classpath*:com/mybusiness/myproject/platform/api/**/*.hbm.xml</value></list>
      </property>
    </bean>

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

    <tx:annotation-driven />
</beans>

How can I get this working?


UPDATE

I managed to get a single Mapping working by adding it to the 'Mappings' tab in 'Edit Configuration'. However, I can't use wildcards here and would have to add every mapping manually.

Answer

maggu picture maggu · Apr 19, 2013

Hibernate Tools is not available under Hibernate 4.x version. It is available with version 3.2. If you are using maven, the dependency goes as:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-tools</artifactId>
    <version>3.2.4.GA</version>
    <scope>runtime</scope>
</dependency>

Now, the hibernate configuration for the tools got nothing to do with spring. An example xml will be (values in this example is for sql server):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="reversengineeringfactory">
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://myinstance:port/mydb</property>
        <property name="hibernate.connection.username">dbuser</property>
        <property name="hibernate.connection.password">dbpass</property>
        <property name="hibernate.default_catalog">mydb</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    </session-factory>
</hibernate-configuration>

Now, For configuring the hibernate config xml in eclipse, you should choose Hibernate Perspective --> Edit Configuration --> Go for configuration File Setup.

A sample reverse engineering xml is below (gives granular control over code generation)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
    <table-filter match-schema="dbo" match-name="Employee"
        package="com.maggu.domain.model" />
    <table-filter match-schema="dbo" match-name="Company"
        package="com.maggu.domain.model" />

    <table schema="dbo" name="Employee">
        <primary-key>
            <generator class="identity" />
        </primary-key>
    </table>
    <table schema="dbo" name="Company">
        <primary-key>
            <generator class="identity" />
        </primary-key>
    </table>
</hibernate-reverse-engineering>

HTH