In my previous Spring MVC project, I used Hibernate as a JPA provider. I did not need to create a hibernate.cfg.xml file because I declared a Hibernate Session Factory in my Spring DispatcherServlet Context file and I declared a persistence.xml file.
In my new project, I would like to use Hibernate mainly. I created object classes from my database structure. However, no IDEA DAO classes were created, why? Can I somehow generate DAO classes in IDEA? And during the creation of this POJO, the object classes I created are also the Hibernate Session Factory in the DispatcherSerlvet Context file.
I created my own simple DAO classes to test for a persistent class in a database. But this error occurred:
Error in creating SessionFactory object./hibernate.cfg.xml not found
Therefore, I assume that I need to create hibernate.cfg.xml . And if so, should I save the Hibernate Session Factory declaration in my DispatcherServlet context file?
EDIT
<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> <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <beans:property name="sessionFactory" ref="sessionFactory" /> </beans:bean>
EDIT No. 2
I moved the annotated classes and connection definitions to the hibernate.cfg.xml file. I deleted the Factory session definition as well as the Transaction Manager definition from the Spring configuration file. And my simple persistent object in my database is working correctly. Perhaps this is the shortest way to work with Spring MVC and Hibernate? But what about Transaction Manager ? Is this required for other operations or activities?
source share