简体   繁体   中英

Can I use Hibernate Session Factory declared in DispatcherServlet Context instead of hibernate.cfg.xml?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM