google Analytics

Friday, August 13, 2010

Spring+HibernateDaoSupport Class Example using JNDI as Datasource Lookup

===========================================================================================================================
applicationContext.java
===========================================================================================================================
<?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:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/testing"/>
</bean>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<list>
<value>classpath*:/com/hibernate/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
</props>
</property>
</bean>

<bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
<property name="nestedTransactionAllowed" value="false"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="hibernateDaoSupport" abstract="true"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>
<bean id="userDao" class="com.dataaccess.hibernate.HibernateUserDao"
parent="hibernateDaoSupport"/>

</beans>

===========================================================================================================================
HibernateUserDao.java
===========================================================================================================================
public class HibernateUserDao extends HibernateDaoSupport implements UserDao {
public void save(User user) {
getHibernateTemplate().save(user);
}
public void delete(User user) {
getHibernateTemplate().delete(user);
}
public User getById(Long id) {
return (User) getHibernateTemplate().get(User.class, id);
}
public List<User> getAll() {
return getHibernateTemplate().find("from User");
}
public User getByUsername(String username) {
List objects = getHibernateTemplate().find("from User where username = ?", username);
if (objects.size() == 1) return (User) objects.get(0);
if (objects.size() > 1) {
throw new InvalidResultException("Found more than one results");
}
return null;
}
}

3 comments:

  1. Thanks a lot for your help ...

    ReplyDelete
  2. A nice tutorial on
    JVM Architecture Specification Basic The Heap Area Introduction, teach about the JVM Heap Area in details
    http://www.youtube.com/watch?v=c-A7ZzxjWUI

    JVM Architecture Specification Basic The Method Area explained, teach about the JVM method area
    http://www.youtube.com/watch?v=a5GzF2fSSCE

    ReplyDelete