使用Spring会话工厂时如何配置Hibernate

使用Spring会话工厂时如何配置Hibernate,spring,hibernate-tools,Spring,Hibernate Tools,我正在尝试在eclipse中设置Hibernate工具。问题是它找不到任何映射文件 我创建了一个控制台配置,它指向我的environment.properties文件和hibernate.cfg.xml。问题是hibernate.cfg.xml中没有映射 <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configura

我正在尝试在eclipse中设置Hibernate工具。问题是它找不到任何映射文件

我创建了一个控制台配置,它指向我的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(如下)中的Springbean 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>

${hibernate.dial}
org.hibernate.transaction.jdbc事务工厂
classpath*:com/mybusiness/myproject/platform/api/**.hbm.xml
我怎样才能让它工作


更新


通过将单个映射添加到“编辑配置”中的“映射”选项卡,我成功地使其工作。但是,我不能在这里使用通配符,必须手动添加每个映射。

Hibernate 4.x版本下没有Hibernate工具。它在3.2版中提供。如果您使用的是maven,则依赖关系如下所示:

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

org.hibernate
休眠工具
3.2.4.GA
运行时
现在,工具的hibernate配置与spring无关。xml示例如下(本例中的值适用于sql server):


net.sourceforge.jtds.jdbc.Driver
jdbc:jtds:sqlserver://myinstance:port/mydb
数据库用户
dbpass
mydb
org.hibernate.dialogue.sqlserverdialogue
现在,为了在eclipse中配置hibernate配置xml,您应该选择hibernate透视-->编辑配置-->转到配置文件设置

下面是一个示例反向工程xml(提供了对代码生成的粒度控制)



HTH

如果您正在使用注释创建持久性实体,您可以执行以下操作:

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


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

实体类

其中EntityClass是包含所有实体类的包。如果有帮助,请告诉我

那么,上面的hibernate配置会自动检测所有带注释和hibernate XML映射吗,还是需要手动将它们添加到上面的文件中?另外,如果该文件与实际的项目hibernate设置无关,那么我将把上述文件存储在哪里?不,数据库配置xml是关于数据库连接到数据库的反向工程(生成hibernate实体POJO)。如果要控制生成的类并管理要考虑的表,则需要提供逆向工程XML。编辑我的答案以包含一个示例反向工程xml。我不想对现有数据库进行反向工程。我已经设置了表和xml映射。我只想对它们运行HQL查询。我遇到的问题是,除非我显式地将表添加到Hibernate工具配置面板中的“映射”选项卡,否则Hibernate工具无法找到这些表。这将自动将所有用@Entity注释的类映射到相应的表。不过,我无法从eclipse Hibernate工具插件中引用spring bean。
<?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>
<bean id = "mySessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">


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