3

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?

woyaru
  • 5,544
  • 13
  • 54
  • 92

1 Answers1

4

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">
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • Thank you for answer. According to using Hibernate directly: I have defined session factory in spring configuration file but I am getting error that hibernate.cfg.xml is not found. So I can't probably define session factory either in spring configuration file OR in hibernate.cfg.xml file. – woyaru Sep 05 '12 at 10:54
  • @woyaru: You can define sessionFactory in configuration file. I have added a sample for the same. Check if you have defined sessionFactory correctly. May be some properties are missing and thats why spring is looking for hibernate.cfg.xml – Ajinkya Sep 05 '12 at 10:56
  • I have in my session factory defined the same properties as you have defined in the example. In the example from the link there is also `configLocation` property but inside there is hibernate.cfg.xml. I have added to my question my definition of session factory. – woyaru Sep 05 '12 at 11:17
  • @woyaru: I think you should use `AnnotationSessionFactoryBean` instead of `LocalSessionFactoryBean`. I have updated my answer. – Ajinkya Sep 05 '12 at 11:33
  • When I have replaced `LocalSessionFactoryBean` with `AnnotationSessionFactoryBean` I have got error the same as here: http://stackoverflow.com/a/8566420/845220 `AnnotationSessionFactoryBean` is part of Hibernate 3. I would like to use Hibernate 4. – woyaru Sep 05 '12 at 11:43