A Techno Blog, mainly about Java

spring & hibernate

spring 2.x: Alex – Main – discussing with Rod and Rob

Tutorial with Hibernate: Hibernate

Hibernate + Spring : this looked like a decent example,  follow on as I do not know German and translated to English below.

Jump, Hibernate and HSQLDB



After that the first example the interaction of jump and HSQLDB showed, is the second
example instead of the JdbcTemplate the HibernateTemplate to use, so
that Hibernate takes over the OR illustration. For the
preparation (and extension) of the first example first is extended by a line:

log4j.rootCategory=ERROR, A1, A1


For Hibernate just as further Jar files are
necessary, so that itself altogether
in the path to rule must:

  • hsqldb.jar
  • log4j-*.jar
  • spring.jar
  • common logging.jar
  • hibernate3.jar
  • dom4j-*.jar
  • jta.jar
  • common collections *.jar
  • ehcache *.jar
  • cglib *.jar
  • asm.jar
  • antlr *.jar

As the first the DAO implementation shows the use
of HibernateTemplate.

package com.javatutor.dao.hibernate;

import java.util.Collection;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.javatutor.dao.CustomerDao;
import more com.javatutor.domain.Customer;

public class CustomerDaoHibernateImpl extends HibernateTemplate
implements CustomerDao
public Collection<Customer > getCustomers()
  return loadAll(Customer.class);

public Customer findCustomerById(int ID)
  return (Customer) load(Customer.class, ID);

public void save(Customer more customer)
  saveOrUpdate(more customer);

To briefcases of the POJOs on the data base a
Mapping file is necessary, it is, one works with Java 5 lists of new
publications. Into the class path the file
Customer.hbm.xml comes.

<?xml version="1.0"? >

<!DOCTYPE hibernate mapping PUBLIC
"-// Hibernate/Hibernate Mapping DTD 3.0//EN"
"" >

< hibernate mapping package="com.javatutor.domain" >

< class more name="Customer" table="Customers"
lazy="false" >

< ID name="id" column="Id" >
  < generator class="native"/>
</ID >

< property name="name" column="Name"/>

</class >

</hibernate mapping >

In jump XML file must we the
CustomerDaoHibernateImpl announce and
supply with a SessionFactory.

< bean id="CustomerDaoHibernate" 
class="com.javatutor.dao.hibernate.CustomerDaoHibernateImpl" >
< property name="sessionFactory" >
< ref local="SessionFactory"/>
</property >
</bean >

The SessionFactory is necessary for Hibernate and
contains the reference to the OR Mappings and the DATA SOURCE.
We already used the DATA SOURCE, but so that everything remains
persistent, HSQLDB is to write this time in the file system.

< bean id="DataSource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
< property name="driverClassName" >
< value>org.hsqldb.jdbcDriver</value >
</property >
< property name="url" >
value>jdbc:hsqldb:file:/c/:Customers;shutdown=true</value >
</property >
< property name="username" >
< value>sa</value >
</property >
< property name="password" >
< value></value >
</property >
</bean >

It is very important to set the
parameter;shutdown=true to the connecting URL. The
LocalSessionFactoryBean is a producer for our SessionFactory:

< bean id="SessionFactory" 
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property name="dataSource" >
< ref local="DataSource"/>
</property >
< property name="mappingResources" >
< cunning >
  < value>Customer.hbm.xml</value >
</cunning >
</property >
< property name="hibernateProperties" >
< props >
  < prop. key="hibernate.dialect" >
  </prop. >
<! - - < prop. key="hibernate.show_sql">true</prop > -
- >
  < prop. key="">create</prop >
</props >
</property >
</bean >

With the then the CREATE
TABLE is unnecessary also in the application. And war’s then


December 23, 2005 - Posted by | IoC

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: