如何在使用Spring Session Factory时配置Hibernate

时间:2013-04-19 01:24:38

标签: spring hibernate-tools

我正在尝试在eclipse中设置Hibernate Tools。麻烦的是它找不到任何映射文件。

我创建了一个控制台配置,指向我的environment.properties文件和hibernate.cfg.xml。麻烦的是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>

似乎在myproject-persistence.xml(下面)中使用spring bean sessionFactory来查找所需的映射文件。我无法在任何地方看到这个文件可以添加到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>

我怎样才能使这个工作?


更新

我设法通过将其添加到“编辑配置”中的“映射”标签来获得单个映射。但是,我不能在这里使用通配符,而是必须手动添加每个映射。

2 个答案:

答案 0 :(得分:1)

Hibernate 4.x版本不提供Hibernate Tools。它适用于3.2版本。如果您使用的是maven,则依赖关系如下:

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

现在,工具的hibernate配置与spring无关。示例xml将是(此示例中的值是针对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>

现在,为了在eclipse中配置hibernate配置xml,你应该选择Hibernate Perspective - &gt;编辑配置 - &gt;转到配置文件设置。

下面的示例逆向工程xml(对代码生成进行精细控制)

<?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

答案 1 :(得分:1)

如果您使用注释来创建持久性实体,那么:

<bean id = "mySessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">


        <property name = "packagesToScan">
        <list>
            <value>entityclasses</value>
        </list>
        </property>

其中entityclasses是包含所有实体类的包。让我知道它是否有帮助!!