Can I use a Hibernate Factory session declared in the DispatcherServlet context instead of hibernate.cfg.xml?

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

 <!-- 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 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?

+3
source share
1 answer

I did not need to create a hibernate.cfg.xml file because I have a Hibernate Factory session announced in my Spring DispatcherServlet Context File, and I declared a persistence.xml file.

AFAIK, when using JPA, we need to define the entityManagerFactory in the Spring configuration file, and the JPA implementation is determined by the jpaVendorAdapter property. persistence.xml used to define duration units. hibernate.cfg.xml not required with JPA.

In my new project, I would like to use Hibernate mainly.

If you want to use hibernate directly, you need to define the Factory session either in the Spring configuration file or in the hibernate.cfg.xml file, for example

 <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 JPA and Hibernate. The following links can help you avoid confusion.
Spring + Hibernate
Spring + JPA (with Hibernate implementation)


EDIT: Use AnnotationSessionFactoryBean as you use annotation to define the mapping

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

Source: https://habr.com/ru/post/1205400/


All Articles