hibernate 3.6.10版 4.3.8版 demo

今天抽空看了hibernate官方文档,拿到目前最新版本4.3.8,按照官方文档调试,遇到一些问题。

在网上找答案的时候,资料很少,搜了好久,一直没找到原因。

 

然后,我就试着将版本降到3.6.10版本,这样一来资料多了很多,解决后,将版本升级到4.3.8,OK!

 

用 hibernate 3.6.10版 实现简单的demo

 

结构图如下:

hibernate 3.6.10版 4.3.8版 demo

 

1.JAR包

 

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.6.10.Final</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.34</version>
		</dependency>

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
	</dependencies>

 2.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.url">jdbc:mysql://ip:端口/数据库</property>
		<property name="connection.username">用户名</property>
		<property name="connection.password">密码</property>
		<property name="connection.driver_class"> com.mysql.jdbc.Driver </property>

		<property name="dialect"> org.hibernate.dialect.MySQLDialect </property>
		<property name="hibernate.show_sql">true</property>

		<mapping resource="org/hibernate/po/Dept.hbm.xml" />

	</session-factory>
</hibernate-configuration> 

 3.*.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="org.hibernate.po.Dept" table="ROMS_SYS_DEPT">
		<id name="deptId" type="java.lang.String" column="DEPT_ID"/>
		<property name="deptName" type="java.lang.String" column="DEPT_NAME"/>
		<property name="deptPid" type="java.lang.String" column="PDEPT_ID"/>
	</class>
</hibernate-mapping>

 4.PO类

public class Dept {

	private String deptId;
	private String deptName;
	private String deptPid;

	// getter,setter 省略 
}

 5.HibernateUtil 类

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		SessionFactory sessionFactory = null;
		try {
			Configuration conf = new Configuration();
			conf.configure();
			// 装载指定的配置文件
			// conf.configure(new File("abc.xml"));
			// 创建 SessionFactory
			sessionFactory = conf.buildSessionFactory();
			} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed:" + ex);
			throw new ExceptionInInitializerError(ex);
		}
		return sessionFactory;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

}

 6.Manager处理类

public class DeptManager {

	public static void main(String[] args) {
		DeptManager mgr = new DeptManager();
		mgr.createAndStoreEvent("My Event", new Date());
//		HibernateUtil.getSessionFactory().close();
	}

	private void createAndStoreEvent(String title, Date theDate) {
		Dept dept = new Dept();
		dept.setDeptId("1111");
		dept.setDeptName("测试部门");
		dept.setDeptPid("000");

		Session session = HibernateUtil.getSessionFactory().openSession();	
		Transaction tx = session.getTransaction();
		// 开启事务
		tx.begin();
		session.save(dept);
		tx.commit();
		session.close();
	}
}

 7.sql

 

DROP TABLE IF EXISTS `ROMS_SYS_DEPT`;
CREATE TABLE `ROMS_SYS_DEPT` (
  `DEPT_ID` varchar(32) CHARACTER SET utf8 NOT NULL DEFAULT '',
  `DEPT_NAME` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
  `PDEPT_ID` varchar(32) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`DEPT_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

4.3.8 版本调整的文件

 

1.HibernateUtil

public class HibernateUtil {

	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory() {
		SessionFactory sessionFactory = null;
		try {

			Configuration configuration = new Configuration()
					.configure("hibernate.cfg.xml");

			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
					.applySettings(configuration.getProperties()).build();

			sessionFactory = configuration.buildSessionFactory(serviceRegistry);

		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed:" + ex);
			throw new ExceptionInInitializerError(ex);
		}
		return sessionFactory;
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}

 

2.DeptManager

 

public class DeptManager {

	public static void main(String[] args) {
		DeptManager mgr = new DeptManager();

		// if (args[0].equals("store")) {
		// mgr.createAndStoreEvent("My Event", new Date());
		// }

		mgr.createAndStoreEvent("My Event", new Date());
		HibernateUtil.getSessionFactory().close();
	}

	private void createAndStoreEvent(String title, Date theDate) {
		Dept dept = new Dept();
		dept.setDeptId("1111");
		dept.setDeptName("测试部门");
		dept.setDeptPid("000");

		Session session = HibernateUtil.getSessionFactory().openSession();

		session.beginTransaction();
		session.save(dept);
		session.getTransaction().commit();
	}
}

 

你可能感兴趣的:(Hibernate)