我可以使用在DispatcherServlet Context中声明的Hibernate Session Factory而不是hibernate.cfg.xml吗? [英] Can I use Hibernate Session Factory declared in DispatcherServlet Context instead of hibernate.cfg.xml?

查看:105
本文介绍了我可以使用在DispatcherServlet Context中声明的Hibernate Session Factory而不是hibernate.cfg.xml吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我之前的Spring MVC项目中,我使用Hibernate作为JPA的提供者。我没有必要创建 hibernate.cfg.xml 文件,因为我在Spring DispatcherServlet上下文文件中声明了Hibernate Session Factory,并且声明了 persistence .xml file。



在我的新项目中,我基本上使用Hibernate。我从我的数据库结构中生成了实体类。但是,在IDEA中DAO类尚未生成,为什么?我能以某种方式在IDEA中生成DAO类吗?在创建POJO的过程中,实体类也创建了Hibernate Session Factory在DispatcherSerlvet Context文件中。



我在自己的简单DAO类上创建了检查数据库中的持久化类。但是发生这个错误:

 创建SessionFactory对象时发生错误./hibernate.cfg.xml未找到
code>

所以我想我必须创建 hibernate.cfg.xml 。如果是的话,我必须在我的DispatcherServlet上下文文件中保留Hibernate Session Factory声明?
$ b 编辑

 <! -  Hibernate session factory  - > 
< beans:bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>

< beans:property name =dataSource>
< beans:ref bean =dataSource/>
< / beans:property>

< beans:property name =hibernateProperties>
< beans:道具>
< beans:prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / beans:prop>
< beans:prop key =hibernate.show_sql> true< / beans:prop>
< beans:prop key =hibernate.hbm2ddl.auto> update< / beans:prop>
< beans:prop key =hibernate.connection.url> jdbc:mysql:// localhost /finances< / beans:prop>
< beans:prop key =hibernate.connection.driver_class> com.mysql.jdbc.Driver< / beans:prop>
< beans:prop key =hibernate.connection.username> root< / beans:prop>
< beans:prop key =hibernate.connection.password> root< / beans:prop>
< / beans:道具>
< / beans:property>

< beans:property name =annotatedClasses>
< beans:list>
< beans:value> my.package.FirstClass< / beans:value>
< beans:value> my.package.SecondClass< / beans:value>
< / beans:list>
< / beans:property>
< / beans:bean>
<! - Hibernate会话工厂结束 - >

< beans:bean id =transactionManagerclass =org.springframework.orm.hibernate4.HibernateTransactionManager>
< beans:property name =sessionFactoryref =sessionFactory/>
< / beans:bean>

编辑#2



我已将注释类和连接定义移动到 hibernate.cfg.xml 文件中。我从Spring配置文件中删除了会话工厂定义以及 Transaction Manager 定义。我的数据库中的简单持久对象正常工作。所以也许这是使用Spring MVC和Hibernate的最短路线?但是关于交易管理器呢?这是另一个操作或操作所需要的吗?

解决方案


我没有创建hibernate。 cfg.xml文件,因为我有
在我的Spring DispatcherServlet中声明了Hibernate Session Factory
上下文文件,并且我声明了persistence.xml文件。

AFAIK在使用JPA时,我们需要在Spring配置文件中定义 entityManagerFactory ,JPA实现由 jpaVendorAdapter 属性。 persistence.xml 用于定义持久性单元。 hibernate.cfg.xml JPA不需要。


在我的新项目中,我希望基本上使用Hibernate。

如果你想直接使用hibernate,你需要在spring配置文件或hibernate.cfg.xml文件中定义会话工厂,比如

 < bean id =mySessionFactoryclass =org.springframework.orm.hibernate3。
annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =myDataSource/>
< property name =annotatedClasses>
< list>
<值> com.foo.Bar< /值>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.HSQLDialect< /丙>
< prop key =hibernate.show_sql> true< / prop>
< prop key =hibernate.hbm2ddl.auto>建立< / prop>
< /道具>
< / property>
< / bean>

您正在混合使用JPA和Hibernate配置。
以下链接可能对您有所帮助避免一些混淆

Spring + Hibernate

Spring + JPA(使用Hibernate实现) a>






编辑:使用 AnnotationSessionFactoryBean ,因为您使用注释来定义映射

 < bean id =sessionFactoryclass =org.springframework.orm .hibernate3.annotation.AnnotationSessionFactoryBean> 


In my previous Spring MVC project I have used Hibernate as a provider for JPA. I didn't have to create hibernate.cfg.xml file because I have declared Hibernate Session Factory in my Spring DispatcherServlet Context file and I have declared persistence.xml file.

In my new project I would like to use Hibernate basically. I have generated entities classes from my database structure. However in IDEA DAO classes haven't been generated, why? Can I in some way generate DAO classes in IDEA? And during generating this POJO, entities classes I have create also Hibernate Session Factory in DispatcherSerlvet Context file.

I have created on my own simple DAO classes to check persisting class in database. But this error has occured:

Error in creating SessionFactory object./hibernate.cfg.xml not found

So I am supposing that I have to create hibernate.cfg.xml. And if yes have I to keep Hibernate Session Factory declaration in my DispatcherServlet Context file at all?

EDIT

<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <beans:property name="dataSource">
        <beans:ref bean="dataSource" />
    </beans:property>

    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
            <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
            <beans:prop key="hibernate.connection.url">jdbc:mysql://localhost/finances</beans:prop>
            <beans:prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</beans:prop>
            <beans:prop key="hibernate.connection.username">root</beans:prop>
            <beans:prop key="hibernate.connection.password">root</beans:prop>
        </beans:props>
    </beans:property>

    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>my.package.FirstClass</beans:value>
            <beans:value>my.package.SecondClass</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>
<!-- Hibernate session factory end -->

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

EDIT #2

I have moved annotated classes and connection definitions to the hibernate.cfg.xml file. I have deleted session factory definition and also Transaction Manager definition from spring configuration file. And my simple persisting object in my database works properly. So maybe this is the shortest way to work with Spring MVC and Hibernate? But what about Transaction Manager? Is this required by another operations or actions?

解决方案

I didn't have to create hibernate.cfg.xml file because I have declared Hibernate Session Factory in my Spring DispatcherServlet Context file and I have declared persistence.xml file.

AFAIK while using JPA we need to define entityManagerFactory in Spring configuration file and JPA implementation is decided by jpaVendorAdapter property. persistence.xml is used to define persistence-units. hibernate.cfg.xml not required with JPA.

In my new project I would like to use Hibernate basically.

If you want to use hibernate directly, you need to define session factory either in spring configuration file or in hibernate.cfg.xml file like

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.
    annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.foo.Bar</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

You are mixing up JPA and Hibernate configuration.
Following links might help you to avoid some confusion
Spring + Hibernate
Spring + JPA (with Hibernate implementation)


EDIT: Use AnnotationSessionFactoryBean as you are using annotation to define mapping

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

这篇关于我可以使用在DispatcherServlet Context中声明的Hibernate Session Factory而不是hibernate.cfg.xml吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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