首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate 的一个小事例(demo)

2012-10-06 
hibernate 的一个小例子(demo)实体类:Users.javapackage com.liufei.hibernate.domainimport java.util.D

hibernate 的一个小例子(demo)
实体类:Users.java
package com.liufei.hibernate.domain;

import java.util.Date;

public class Users {
private String id;
private String name;
private Date birthday;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date date) {
this.birthday = date;
}

}

对应的映射文件:Users.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package = "com.liufei.hibernate.domain">
<class name = "Users">
<id name = "id">
   <generator class = "native"/>
  </id>
  <property name = "name"/>
  <property name = "birthday"/>
  </class>
</hibernate-mapping>
hibernate配置文件:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="foo">
<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">liufei</property>

<property name="hibernate.hbm2ddl.auto">create</property>

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

<mapping resource="com/liufei/hibernate/domain/Users.hbm.xml"/>

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

由实体类映射表结构:
package com.liufei.hibernate.domain;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
*
* @author 刘飞
*
*/
public class ExportDB {
/**
*
* @param  cfg
* @return  boolean
*/
public boolean exportDB(Configuration cfg) {
boolean flag = false;

SchemaExport export = new SchemaExport(cfg);
if (export != null) {
export.create(true, true);
flag = true;
}

return flag;
}

/*
* public static void main(String[] args) { Configuration cfg=new
* Configuration().configure(); SchemaExport export=new SchemaExport(cfg);
* export.create(true, true); }
*/
}



测试类:
package com.liufei.test;

import java.util.Date;

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

import com.liufei.hibernate.domain.ExportDB;
import com.liufei.hibernate.domain.Users;

/**
*
* @author 刘飞
*
*/
public class Base {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cfg = new Configuration().configure();

ExportDB edb = new ExportDB() ;

if(edb.exportDB(cfg)){
try{
SessionFactory sf = cfg.buildSessionFactory();

Session s = sf.openSession();

Transaction tx = s.beginTransaction();

Users user = new Users();

user.setId("3");
user.setBirthday(new Date());
user.setName("liufei");

s.save(user);
tx.commit();
s.close();
}catch(Exception e){
e.printStackTrace() ;
}
}

System.out.println("end");

}

}
以上仅供入门者参考!
建议使用如下的方法优化:
package com.liufei.hibernate.domain;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
* Hibernate初始化以及一些基本操作的工具类
* @author 刘飞
*
*/
public class HibernateUtil {
private static SessionFactory sessionFactory ;
private static Configuration configuration ;

/**
* 指定该类不可以被继承
*/
private HibernateUtil(){

}
/**
* configure(Document document);
* configure(File configFile);
* configure(String resource);
* configure(URL url);
*/
static {
configuration = new Configuration().configure() ;
try{
sessionFactory = configuration.buildSessionFactory() ;
}catch(HibernateException e){
e.printStackTrace() ;
}

}

/**
* 由实体类自动创建数据库表结构
*/
public static void exportDB(){
SchemaExport export=new SchemaExport(configuration);
export.create(true, true);
}

/**
* 向表中插入一个元组的数据
* @param table
*/
public static void addTable(Object table){
Session s = null ;
Transaction tx = null ;
try{
s = getSession() ;
tx = s.beginTransaction() ;
s.save(table) ;
tx.commit() ;

}catch(HibernateException e){
if(tx != null){
tx.rollback() ;
}
throw e ;
}finally{
if(s != null){
s.close() ;
}
}
}

/**
*  获取Session对象
* @return Session
*/
public static Session getSession(){
return sessionFactory.openSession() ;
}

/**
* 返回 SessionFactory 对象
* @return SessionFactory
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 设置由外部传入的 SessionFactory 对象
* @param sessionFactory
*/
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtil.sessionFactory = sessionFactory;
}

}

热点排行