Tutorial66.com
@Share Application Development Skill.



Tutorial is tutorial...66 is My Country Code Up to 10 Years EXP for Application Development Role in Thailand.
collapse

Tutorial Category



Topic: Spring framework Hibernate tutorial How to User Hibernate as Data Access  (Read 318 times)


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
One highlight of the Spring Framework  is compatible with many data Access eg JDBC, Hibernate, iBatis
This tutorial show you for How User Hibernate as Data Access for Spring Framework
Requirement Tools
 1. JDK and Eclipse Editor (it 's common that we have) ;D
 2. Hibernate  .. Go to Google and Search with "Hibernate Download"  to get it.
 3. Spring Framework ..I believe you know.but if it does not goto Google and Search "Spring Framework Download" 
 
Remark :Step Over ..is Common Reply this topic If you do not.  :'(
Remark :Step Into ..I Will Show you in detail and Example.  8)

Step 1 : Use Eclipse IDE to create Dynamic Web Application (Step Over)
Step 2 : Import Hibernate Library and Spring Spring Framework  to Project  (Step Over)
Step 3 : Build Hibernate Mapping Class with annotation (Step Into)
Class Name Example.java
Code: [Select]
package com.en.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * @author Somsuksri Kasemsuk
 * @since  13-08-2011
 */

@Entity
@Table(name = "example")
public class Example implements java.io.Serializable {
private static final long serialVersionUID = -169364292963401081L;

@Id
@Column(name = "code_id")
private Integer codeId;

@Column(name = "tdesc")
private String tdesc;

@Column(name = "edesc")
private String edesc;

public Integer getCodeId() {
return codeId;
}

public void setCodeId(Integer codeId) {
this.codeId = codeId;
}

public String getTdesc() {
return tdesc;
}

public void setTdesc(String tdesc) {
this.tdesc = tdesc;
}

public String getEdesc() {
return edesc;
}

public void setEdesc(String edesc) {
this.edesc = edesc;
}
}

Step 4 : Create XML Configuration File (Step Into)
hibernate.cfg.xml add it to classpath
Code: [Select]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <mapping class="com.en.hibernate.Example"/>
    </session-factory>
</hibernate-configuration>

Configuration Spring Context
dataAccesssContext-Hibernate.xml
Code: [Select]
<?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="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${db.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.default_batch_fetch_size">0</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
                <prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="lobHandler">
<bean class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
</property>
</bean>

<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

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

</beans>
Load it to Spring Context
Web.xml
Code: [Select]
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/appcontext/datasource-service.xml
/WEB-INF/appcontext/dataAccesssContext-Hibernate.xml
</param-value>
</context-param>
« Last Edit: October 21, 2011, 05:28:51 AM by Admin »




SimplePortal 2.3.3 © 2008-2010, SimplePortal