Intro SpringORM

 


ProductDao required HibernateTemplate object

HibernateTemplate need SessionFactor object through LocalSessionFactorBean

LocalSessionFactoryBean need dataSource, HibernateProperties, AnnotatedClass
ProductDAO required HibernateTemplate
HibernateTemplate has all the method required for CRUD operation.
HibernateTemplate require SessionFactory. Since SessionFactory interface we have to give LocalSessionFactoryBean. 
LocalSessionFactorBean require DataSource, HibernateProperties, AnnotatedClass.
Dependencies are springorm, mysql-connector-java, hibernate

@Transcational should be used before a method with has write operation and configure HibernateTranscationManager in xml like below.

This is how it is set

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<tx:jta-transaction-manager/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springjdbc?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>

<bean class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" name="sessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>com.mountech.entities.Student</value>
</list>
</property>
</bean>

<bean class="org.springframework.orm.hibernate5.HibernateTemplate" name="hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean class="com.mountech.dao.StudentDAOImpl" name="studentDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>

<bean class="org.springframework.orm.hibernate5.HibernateTransactionManager" name="transactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

Comments

Popular posts from this blog