I am working on a web application which needs hibernate for DB integration. So i decided to use hibernate with spring. As we all know that Spring is very lightweight and supports hibernate in an easy way. Also i am used to spring
While working this time i decided to use
Steps to make
- Extend
- Define your methods which provides different Database operations (CREATE, DELETE, FETCH, UPDATE, INSERT etc...)
- Define required beans as below,
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/testdb</value>
</property>
<property name="username" value="username" />
<property name="password" value="passwordforit" />
<property name="defaultAutoCommit" value="true" />
<property name="defaultTransactionIsolation" value="2" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="50" />
<property name="minIdle" value="50" />
<property name="maxWait" value="100" />
<property name="initialSize" value="20" />
</bean>
<!-- Database Property -->
<bean id="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider</prop>
</props>
</property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="hibernateProperties">
<ref bean="hibernateProperties" />
</property>
<!-- OR mapping files. -->
<property name="mappingResources">
<list>
<value>myMapping.hbm.xml</value>
</list>
</property>
</bean>
<bean id="MyDAOTarget" class="com.dao.MyDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
- Define transactionManager for transaction managment as below,
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
- Define
<bean id="MyDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="MyDAOTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
<prop key="update*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
<prop key="delete*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED</prop>
<prop key="find*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, readOnly</prop>
</props>
</property>
</bean>
- Use
- Use
- Use
- Use
It seems so easy right? But no, wait! All these works fine when you have
I have tried using
public void updateMyObj(MyObj myObj) {
Session session = getSession(true);// true means allow create new session if required.
Transaction trans = session.beginTransaction();
session.saveOrUpdate(myObj);
trans.commit();
}
Same thing works for any update operation. This is really strange, i tried for one whole day using
0 Comments:
Post a Comment