在这个研究过程中遇到的一个比较痛苦的问题是Hibernate的版本更新问题,从4.0以前的duplicated,再到4.0以后的一些ServiceRegistry的duplicated过期问题,耗费了很多时间。
下面上demo的流程
步骤一:下载最新版的hibernate,至少4.0以上的,引入hibernate包及sqldb驱动包。
步骤二:配置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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///hibernate_test</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/huang/action/model/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
步骤三:创建model
package com.huang.action.model;
public class Customer {
private Integer cid;
private String username;
private String password;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Customer() {
}
public Customer(Integer cid, String username, String password) {
super();
this.cid = cid;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "Customer [cid=" + cid + ", password=" + password
+ ", username=" + username + "]";
}
}
步骤四:配置对应model文件:如我的model为Customer,Customer.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.huang.action.model.Customer" table="customer" catalog="hibernate_test">
<id name="cid" type="java.lang.Integer">
<column name="CID" />
<generator class="native" />
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="12" not-null="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="12" />
</property>
</class>
</hibernate-mapping>
步骤五:调用实现
package com.huang.action;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import com.huang.action.model.Customer;
public class Test extends TestCase {
public void test(){
SessionFactory sessionFactory=null;
Configuration configuration=new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory= configuration.buildSessionFactory(serviceRegistry);
sessionFactory=configuration.buildSessionFactory(serviceRegistry);
Session session= sessionFactory.openSession();
Transaction transaction= session.beginTransaction();
Customer ct=(Customer)session.get(Customer.class, 1);
transaction.commit();
System.out.println("id:"+ct.getCid()+"\t");
System.out.println("id:"+ct.getUsername()+"\t");
System.out.println("id:"+ct.getPassword()+"\t");
session.close();
sessionFactory.close();
}
}
打印结果如下: