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?