Results 1 to 4 of 4

Thread: Spring DAO + Hibernate; Am I doing this right?

  1. #1
    Join Date
    Mar 2005
    Posts
    29

    Default Spring DAO + Hibernate; Am I doing this right?

    I have just started adding Spring's DAO support into my JSF+Hibernate application. Since I am really new to all this DAO stuff I'd like to post my code and get feedback on what I have done right and/or what I should do differently. I will not be including my hibernate mapping files as they work fine and this is more about implementing Spring's DAO. Thanks for any tips/advice you can give me.

    The following code is all for my User class and getting a user via a username and password for logging in.

    applicationContext.xml
    Code:
    <beans>
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                <value>java&#58;comp/env/jdbc/anykey</value>
            </property>
        </bean>
     
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                </props>
            </property>
            <property name="mappingDirectoryLocations">
                <list>
                    <value>classpath&#58;/com/intrust/anykey/database/hibernate/mappings</value>
                </list>
            </property>
        </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
        </bean>
     
        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory">
                <ref bean="sessionFactory"/>
            </property>
        </bean>
        
        <bean id="userDao" class="com.intrust.anykey.database.dao.hibernate.UserDaoHib">
            <property name="hibernateTemplate">
                <ref bean="hibernateTemplate"/>
            </property>
        </bean>        
     
        <bean id="userServiceTarget" class="com.intrust.anykey.database.dao.services.UserServiceImpl">
            <property name="userDao">
                <ref bean="userDao"/>
            </property>
        </bean>
     
        <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="transactionManager"/>
            </property>
            <property name="target">
                <ref bean="userServiceTarget"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="getUser">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    </beans>
    UserService
    Code:
    public interface UserService &#123;
        public void setUserDao&#40;UserDAO userDao&#41;;
        public User getUser&#40;String username, String password&#41;;
    &#125;
    UserServiceImpl
    Code:
    public class UserServiceImpl implements UserService &#123;
        
        private UserDAO userDao;
     
        public void setUserDao&#40;UserDAO userDAO&#41;&#123;
            this.userDao = userDAO;
        &#125;
        
        public User getUser&#40;String username, String password&#41; &#123;
            return userDao.load&#40;username, password&#41;;
        &#125;
    &#125;
    UserDAO
    Code:
    public interface UserDAO &#123;
    	public User load&#40;String username, String password&#41;;
    &#125;
    UserDaoHib
    Code:
    public class UserDaoHib implements UserDAO &#123;
     
        private HibernateTemplate hibernateTemplate;
     
        public void setHibernateTemplate&#40;HibernateTemplate template&#41;&#123;
            this.hibernateTemplate = template;
        &#125;
     
        public User load&#40;String username, String password&#41; &#123;
            String passHash = MD5.getHash&#40;password&#41;;
            final Map map = new HashMap&#40;&#41;;
                map.put&#40;"username", username&#41;;
                map.put&#40;"password", passHash&#41;;
     
    
           return &#40;User&#41;hibernateTemplate.execute&#40;new HibernateCallback&#40;&#41; &#123;
                public Object doInHibernate&#40;Session session&#41; throws HibernateException&#123;
                    return session.createCriteria&#40;User.class&#41;.add&#40;Restrictions.allEq&#40;map&#41;&#41;.uniqueResult&#40;&#41;;
                &#125;
            &#125;&#41;;
    &#125;
    User Login Snippet
    Code:
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext&#40;getContext&#40;&#41;&#41;;            
    UserService userDAO = &#40;UserService&#41;context.getBean&#40;"userService"&#41;;
    User user = userDAO.getUser&#40;username, password&#41;;
    Gregg Bolinger

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    As far as I can see, the config / code look OK. I have however some questions / remarks:
    - You seem to be using Spring support Hibernate3 from JIRA, Juergen will be comiting some code to sandbox soon.
    - There should be HibernateDaoSupport class that ease the creation of Dao Classes
    - getUser should be marked PROPAGATION_REQUIRED,readOnly I think.
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Mar 2005
    Posts
    29

    Default

    Quote Originally Posted by irbouho
    As far as I can see, the config / code look OK. I have however some questions / remarks:
    - You seem to be using Spring support Hibernate3 from JIRA, Juergen will be comiting some code to sandbox soon.
    - There should be HibernateDaoSupport class that ease the creation of Dao Classes
    - getUser should be marked PROPAGATION_REQUIRED,readOnly I think.
    HTH
    I actually started using the HibernateDaoSupport. It's kind of a catch 22 on some things though. I really like using the Criteria and Expression API rather than writing a bunch of HQL. It would seem that without using the Callback to get access to the actual Hibernate Session, using those API's is impossible. And thanks for the info on the getUser method.

    An add-on question. Right now I am adding a new service declaration

    Code:
    <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref bean="transactionManager"/>
            </property>
            .............................
    for each service class I have. Is there a way to define 1 service bean and just add targets to keep the applicationContext more concise?
    Gregg Bolinger

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can use a template with some inner beans as follows:
    Code:
      <!-- transactional service template -->
      <bean id="txProxyTemplate"
         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
         abstract="true"
         lazy-init="true">
        <property name="transactionManager">
          <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="set*">PROPAGATION_REQUIRED</prop>
            <prop key="save*">PROPAGATION_REQUIRED</prop>
            <prop key="delete*">PROPAGATION_REQUIRED</prop>
          </props>
        </property>
      </bean>
    
      <bean id="userManager" parent="txProxyTemplate">
        <property name="proxyInterfaces">
          <value>org.taha.service.UserManager</value>
        </property>
        <property name="target">
          <bean class="org.taha.service.UserManagerImpl">
            <property name="userDAO">
              <ref bean="userDAO"/>
            </property>
          </bean>
        </property>
      </bean>
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Replies: 5
    Last Post: Feb 3rd, 2009, 05:19 AM
  2. Replies: 3
    Last Post: Aug 16th, 2007, 12:10 PM
  3. A Spring Class Loader?
    By azzoti in forum Architecture
    Replies: 8
    Last Post: May 7th, 2005, 04:02 AM
  4. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM
  5. Replies: 7
    Last Post: Aug 21st, 2004, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •