Polishing the standalone Spring Hibernate Example

I have modified the post here http://cuppajavamattiz.com/2008/06/14/migrating-to-a-standalone-spring-hibernate-application/ to make it look more professional.

Scripts to run:

drop table authors;
drop database authors;
create database AUTHORS;
use AUTHORS;
CREATE TABLE authors (ISBN_CODE varchar(10) NOT NULL default '0',AUTHOR varchar(30) default NULL,PRIMARY KEY (ISBN_CODE)) ;
insert into authors (ISBN_CODE,AUTHOR )values("1e","matty");

The main class:

package com.mattiz.web.managedbeans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mattiz.persistence.beans.AuthorBean;
import com.mattiz.service.spring.IMattizDelegate;

public class MattizMain {

private ApplicationContext applicationContext;

private IMattizDelegate mattizDelegate;

public IMattizDelegate getMattizDelegate() {
 mattizDelegate = (IMattizDelegate) applicationContext
 .getBean("mattiz.service.delegate");
 return mattizDelegate;
 }

public void setMattizDelegate(IMattizDelegate mattizDelegate) {
 this.mattizDelegate = mattizDelegate;
 }

public ApplicationContext getApplicationContext() {
 return applicationContext;
 }

public void setApplicationContext(ApplicationContext applicationContext) {
 this.applicationContext = applicationContext;
 }

public static void main(String[] args) {
 MattizMain mattizMain = new MattizMain();
 mattizMain.applicationContext = new ClassPathXmlApplicationContext(
 "resources/mattiz.xml");
 mattizMain.getMattizDelegate().addAuthor("007", "Bond");
 AuthorBean authorBean = mattizMain.getMattizDelegate().findAuthor("007");
 System.out.println("AUTHOR IS " + authorBean.getAuthor());
 }

}

The Delegate class:

package com.mattiz.service.spring;

import com.mattiz.persistence.beans.AuthorBean;
import com.mattiz.persistence.data.IMattizDao;

public class MattizDelegate implements IMattizDelegate {

private IMattizDao mattizDao;

public void addAuthor(String isbnCode, String author) {
 getMattizDao().insertAuthor(isbnCode, author);
 }

public AuthorBean findAuthor(String isbnCode) {
 AuthorBean authorsBean = getMattizDao().lookupIsbn(isbnCode);
 return authorsBean;
 }

 public IMattizDao getMattizDao() {
 return mattizDao;
 }

public void setMattizDao(IMattizDao mattizDao) {
 this.mattizDao = mattizDao;
 }

}

The delegate interface:

package com.mattiz.service.spring;

import com.mattiz.persistence.beans.AuthorBean;
import com.mattiz.persistence.data.IMattizDao;

public interface IMattizDelegate {

 public void addAuthor(String isbnCode, String author);

public AuthorBean findAuthor(String isbnCode);

 public IMattizDao getMattizDao();

public void setMattizDao(IMattizDao mattizDao);

}

The Domain class:

package com.mattiz.persistence.beans;

public class AuthorBean {
 String author;
 String isbnCode;

public String getAuthor() {
 return author;
 }

public void setAuthor(String author) {
 this.author = author;
 }

public String getIsbnCode() {
 return isbnCode;
 }

public void setIsbnCode(String isbnCode) {
 this.isbnCode = isbnCode;
 }

}

The Dao class:

package com.mattiz.persistence.data;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.mattiz.persistence.beans.AuthorBean;

public class MattizDao implements IMattizDao {

private HibernateTemplate hibernateTemplate;

public AuthorBean lookupIsbn(String isbn) throws DataAccessException {
 // List l = getHibernateTemplate().find(
 // "from Authors authorBean where authorBean.isbnCode = ?", isbn);//use
 // when
 // isbn is not PK
 AuthorBean authorBean = null;
 authorBean = (AuthorBean) getHibernateTemplate().load(AuthorBean.class,
 isbn);
 return authorBean;
 }

public void insertAuthor(String isbn, String authorName)
 throws DataAccessException {
 AuthorBean authorBean = new AuthorBean();
 authorBean.setAuthor(authorName);
 authorBean.setIsbnCode(isbn);
 HibernateTemplate template = getHibernateTemplate();
 template.saveOrUpdate(authorBean);
 System.out.println("Inserted/Updated AuthorBean, ISBN "
 + authorBean.getAuthor() + " " + authorBean.getIsbnCode());
 }

public HibernateTemplate getHibernateTemplate() {
 return hibernateTemplate;
 }

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
 this.hibernateTemplate = hibernateTemplate;
 }
}

The Dao interface:

package com.mattiz.persistence.data;

import org.springframework.dao.DataAccessException;

import com.mattiz.persistence.beans.AuthorBean;

public interface IMattizDao {

public AuthorBean lookupIsbn(String isbn) throws DataAccessException;

public void insertAuthor(String isbn, String authorName)
 throws DataAccessException;

}

The mapping hbm file(Author.hbml.xml):

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
 <class name="com.mattiz.persistence.beans.AuthorBean" table="AUTHORS"
 lazy="false">
 <id name="isbnCode" type="java.lang.String" column="ISBN_CODE"
 length="10" />
 <property name="author" type="java.lang.String" column="AUTHOR"
 length="50" />
 </class>
</hibernate-mapping>

The spring configuration file(mattiz.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="mattiz.hibernate.dataSource"
 class="com.mchange.v2.c3p0.ComboPooledDataSource"
 destroy-method="close">
 <property name="driverClass" value="com.mysql.jdbc.Driver" />
 <property name="jdbcUrl"
 value="jdbc:mysql://localhost:3306/authors" />
 <property name="user" value="root" />
 <property name="password" value="admin" />
 </bean>

<bean id="mattiz.abstract.hibernate.sessionfactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
 abstract="true" lazy-init="true">
 <property name="dataSource">
 <ref local="mattiz.hibernate.dataSource" />
 </property>
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">
 org.hibernate.dialect.MySQLDialect
 </prop>
 <prop key="hibernate.show_sql">false</prop>
 </props>
 </property>
 </bean>

<bean id="mattiz.pageSize" class="java.lang.Integer">
 <constructor-arg value="5000"></constructor-arg>
 </bean>

<bean id="mattiz.hibernate.sessionfactory"
 parent="mattiz.abstract.hibernate.sessionfactory">
 <property name="mappingResources">
 <list>
 <value>resources/Author.hbm.xml</value>
 </list>
 </property>
 </bean>

 <bean id="mattiz.transactionManager"
 class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 <property name="sessionFactory">
 <ref bean="mattiz.hibernate.sessionfactory" />
 </property>
 </bean>

<bean id="mattiz.hibernateTemplate"
 class="org.springframework.orm.hibernate3.HibernateTemplate">
 <property name="sessionFactory">
 <ref bean="mattiz.hibernate.sessionfactory" />
 </property>
 </bean>

<bean id="mattiz.dao.mattizDAO"
 class="com.mattiz.persistence.data.MattizDao"
 singleton="true">
 <property name="hibernateTemplate">
 <ref bean="mattiz.hibernateTemplate" />
 </property>
 </bean>

<bean id="mattiz.client"
 class="com.mattiz.web.managedbeans.MattizMain"
 singleton="true">
 <property name="mattizDelegate" ref="mattiz.service.delegate" />
 </bean>

<bean id="mattiz.service.delegate"
 class="com.mattiz.service.spring.MattizDelegate"
 singleton="true">
 <property name="mattizDao" ref="mattiz.dao.mattizDAO" />
 </bean>

</beans>

The jars I added are from the springsource with dependencies package. Other than that I used the mysql driver, jta.jar (see screenshot)
Capture

Download source code here StandAloneHibernateUpdated

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: