1 Hibernate 第一例

一个简单的CURD的例子. 源码中不包含所依赖的库, 要不然打的包太大了. Hibernate 版本3.2

PO对象注意:

1 要有默认的构造方法

2 类是非final修饰的.否则 懒加载无用.

UserDAOImpl类的代码, 其它配置文件,请看附件中的源码包

package dao;

import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dao.po.User;

/**
 * 这里做CURD工作
 * 
 */
public class UserDAOImpl
{
	/**
	 * @param args
	 */
	public static void main(final String[] args)
	{
		//test1();
		test2();
		//test3();
	}

	/**
	 * 保存查询测试
	 */
	public static void test1()
	{
		final User u = new User();
		u.setBirthday(new Date());
		u.setName("北京市");
		save(u);
		get(1);
	}

	/**
	 * 修改一个 脱管 的对象, 这是没有先查询.
	 */
	public static void test2()
	{
		final int id = 6;
		get(id);
		final User u = new User();
		u.setBirthday(new Date());
		u.setId(id); //这里指定ID , 好修改
		u.setName("重庆市");
		update(u);
		get(id);
	}

	/**
	 * 测试 load 强制加载
	 */
	public static void test3()
	{
		final User user = load(1);
		System.out.println("查询完成:" + user.getName());
	}

	public static void save(final Object obj)
	{
		Session s = null;
		Transaction t = null;
		try
		{
			s = HibernateUtil.getSeesion();
			t = s.beginTransaction();
			s.save(obj);
			t.commit();
		}
		finally
		{
			if (s != null)
			{
				s.close();
			}
		}
		System.out.println("保存完成");
	}

	public static User get(final int id)
	{
		final Session s = HibernateUtil.getSeesion();
		final User user = (User) s.get(User.class, id);
		s.close();
		System.out.println("查询完成:" + user.getName());
		return user;
	}

	public static User load(final int id)
	{
		final Session s = HibernateUtil.getSeesion();
		final User user = (User) s.load(User.class, id);

		//强制加载
		Hibernate.initialize(user);
		s.close();

		return user;
	}

	public static void update(final User u)
	{
		Session s = null;
		Transaction t = null;
		try
		{
			s = HibernateUtil.getSeesion();
			t = s.beginTransaction();
			s.update(u);
			t.commit();
		}
		finally
		{
			if (s != null)
			{
				s.close();
			}
		}
		System.out.println("修改完成");
	}
}

package dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 这是一个工具类, 快速取得session
 * 
 */
public class HibernateUtil
{
	static SessionFactory	sessionFactory	= null;
	static
	{
		final Configuration cfg = new Configuration();
		cfg.configure(); //路径可以改变
		sessionFactory = cfg.buildSessionFactory();
	}

	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	/**
	 * 取得session
	 * 
	 * @return session
	 */
	public static Session getSeesion()
	{
		return sessionFactory.openSession();
	}
}

hibernate.cfg.xml

<!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="show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
	
	
	<mapping resource="dao/po/User.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

你可能感兴趣的:(Hibernate)