Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Hibernate environment in Spring

    • 0
    • 9
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 950
    Comment on it

    Spring comes with a family of data access frameworks that integrate with a variety of data access technologies. Whether youre persisting your data via direct JDBC, iBATIS, or an object relational mapping (ORM) framework such as Hibernate, Spring removes the tedium of data access from your persistence code. We could use JDBC, Hibernate, the Java Persistence API (JPA), or any of a number of persistence. Spring supports all of those persistence mechanisms.

    Hibernate is an open source persistence framework that has gained significant popularity in the developer community. It provides not only basic object-relational mapping but also all the other sophisticated features youd expect from a full-featured ORM tool, such as caching, lazy loading, eager fetching, and distributed caching. This section will cover many issues in detail and show different variations of DAO implementations.

    "DAOs exist to provide a means to read and write data to the database. They should expose this functionality through an interface by which the rest of the application will access them."

    Declaring a Hibernate session factory

    Spring allows you to define resources such as a JDBC DataSource or a Hibernate SessionFactory as beans in the Spring container. Application objects that need to access resources just receive references to such pre-defined instances via bean references.

    <beans>
    
      <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/databaseName"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
      </bean>
    
      <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="mappingResources"
          <list>
            <value>product.hbm.xml</value>
          </list>
        </property>
        <property name="hibernateProperties">
          <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
          </value>
        </property>
      </bean>
    
    </beans>
    

    As with LocalSessionFactoryBean, the dataSource and hibernateProperties properties tell where to find a database connection and what kind of database well be dealing with.

    The HibernateTemplate

    The following snippets show a DAO definition in a Spring container, referencing the above defined SessionFactory, and an example for a DAO method implementation.

    <beans>
    
      <bean id="myProductDao" class="product.ProductDaoImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
     </bean>
    
    </beans>
    
    public class ProductDaoImpl implements ProductDao {
    
        private HibernateTemplate hibernateTemplate;
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
        }
    
        public Collection loadProductsByCategory(String category) throws DataAccessException {
            return this.hibernateTemplate.find("from test.Product product where product.category=?", category);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: