2

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

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

我怎样才能得到这个工作?


更新

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

4

2 回答 2

1

Hibernate Tools 在 Hibernate 4.x 版本下不可用。它适用于 3.2 版。如果您使用的是 maven,则依赖项如下:

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

现在,工具的休眠配置与 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 config xml,你应该选择 Hibernate Perspective --> Edit Configuration --> Go for configuration File Setup。

下面是一个示例逆向工程 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>

高温高压

于 2013-04-19T05:06:32.793 回答
1

如果您使用注释来创建持久性实体,那么您可以这样做:

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


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

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

于 2013-04-22T19:33:28.073 回答