使用Spring Session Factory时如何配置Hibernate [英] How to configure Hibernate when you are using a Spring Session Factory

查看:115
本文介绍了使用Spring Session Factory时如何配置Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Eclipse中设置Hibernate Tools.问题在于它找不到任何映射文件.

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

我创建了一个控制台配置,该配置指向我的environment.properties文件和hibernate.cfg.xml.问题在于hibernate.cfg.xml中没有映射.

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>

似乎它正在使用myproject-persistence.xml(如下)中的spring bean sessionFactory查找所需的映射文件.我看不到可以在eclipse中将该文件添加到控制台配置的任何地方.

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>

我该如何工作?

更新

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

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.

推荐答案

如果您要使用注释来创建持久性实体,那么您可以执行以下操作:

if you are using annotations to create your persistence entities than you can do like:

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


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

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

where entityclasses is the package which contains all your entity classes. Let me know if it helps!!

这篇关于使用Spring Session Factory时如何配置Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆