Spring Framework : A Framework from SpringSource :: Spring ORM Example
Spring using Hibernate Example |
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
item.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!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.javaskool.Item" table="ItemTable" catalog="javaskoolDB" lazy="false">
<id name="itemId" column="ItemID" length="10">
<generator class="native"></generator> <!-- for auto number genetation -->
</id>
<property name="itemName">
<column name="ItemName" length="100" />
</property>
<property name="qty">
<column name="Qty"></column>
</property>
</class>
</hibernate-mapping>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iobj" class="com.javaskool.Item">
<property name="itemId" value="1001"/>
<property name="itemName" value="Logitech Mouse"/>
<property name="qty" value="500"/>
</bean>
<!-- If you are having hibfernate.cfg.xml , below two line is enough to get sessionFactory-->
<!--
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
-->
<!-- OR -->
<!-- You can use below code to configure data source here itself -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/javaskoolDB" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<!--
Now, suppose you have a data source defined in the Spring IoC container.
If you want to use this data source for your session factory, you can inject it into the
dataSource property of LocalSessionFactoryBean.
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>item.hbm.xml</value> <!-- <value>com/javaskool/item.hbm.xml</value> -->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
</beans>
<!--
The mappingResources propertys type is String[], so you can specify a set of mapping
files in the classpath. LocalSessionFactoryBean also allows you take advantage of Springs
resource-loading support to load mapping files from various types of locations. You can specify
the resource paths of the mapping files in the mappingLocations property, whose type is
Resource[].
-->
hibernate.cfg.xml
<?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>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/javaskoolDB
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">
isacMySQLDriver
</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping resource="item.hbm.xml" />
</session-factory>
</hibernate-configuration>
TestDrive.java
package com.javaskool;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class TestDrive {
public static void main(String[] args)
{
//Resource res = new ClassPathResource("applicationContext.xml");
//BeanFactory beans=new XmlBeanFactory(res);
//or
BeanFactory beans=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
Item p=(Item)beans.getBean("iobj"); //getting Object from context
System.out.println("Item Details : ");
System.out.println(p.getItemId()+ " "+p.getItemName()+" "+p.getQty());
SessionFactory sf=(SessionFactory)beans.getBean("sessionFactory");
HibernateTemplate ht=new HibernateTemplate(sf);
//ht.save(p);
p.setItemId(2);
p.setQty(99999);
ht.update(p);
//p.setItemId(1);
//ht.delete(p);
/*
Session session=sf.openSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
session.flush();
session.close();
*/
}
}
Output
Item Details :
1001 Logitech Mouse 500
Hibernate: update javaskoolDB.ItemTable set ItemName=?, Qty=? where ItemID=?
mysql>select * from ItemTable;
+--------+----------------+-------+
| ItemID | ItemName | Qty |
+--------+----------------+-------+
| 2 | Logitech Mouse | 99999 |
+--------+----------------+-------+
1 row in set (0.00 sec)
mysql>
Click Below to download the Examples
Recent Comments