Hibernate实例
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?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.driver_class">
com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/funnyweb</property>
<property name="connection.username">root</property>
<property name="connection.password">leung</property>
<property name="connection.pool_size">5</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">
thread</property>
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<mapping resource="funnyweb/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?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="funnyweb.User" table="user">
<id column="name" name="name"/>
<property column="sex" generated="never" lazy="false" name="sex"/>
<property column="birthday" generated="never" lazy="false" name="birthday"/>
<property column="education" generated="never" lazy="false" name="education"/>
<property column="school" generated="never" lazy="false" name="school"/>
<property column="password" generated="never" lazy="false" name="password"/>
<property name="address" type="funnyweb.AddressUserType">
<column name="province"/>
<column name="city"/>
</property>
</class>
</hibernate-mapping>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package funnyweb;
public class Address implements java.io.Serializable{
private String province;
private String city;
public Address(){
}
public Address(String province,String city){
this.province = province;
this.city = city;
}
public String toString(){
return (province+city);
}
public boolean equals(Object arg0){
if(arg0==null)
return false;
if(this==arg0)
return true;
Address address = (Address)arg0;
if((address.getCity()==this.getCity())
&&(address.getProvince()==this.getProvince()))
return true;
else
return false;
}
public int hashCode(){
return (this.getCity().hashCode() +
this.getProvince().hashCode());
}
public void setProvince(String province){
this.province = province;
}
public String getProvince(){
return this.province;
}
public void setCity(String city){
this.city = city;
}
public String getCity(){
return this.city;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package funnyweb;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.hibernate.Hibernate;
public class AddressUserType implements UserType {
private static final int[] TYPES = {Types.VARCHAR,Types.VARCHAR};
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return deepCopy(arg0);
}
public Object deepCopy(Object arg0) throws HibernateException {
if(arg0==null){
return null;
}
Address address = (Address)arg0;
return new Address(address.getProvince(),address.getCity());
}
public Serializable disassemble(Object arg0)
throws HibernateException {
return (Serializable)deepCopy(arg0);
}
public boolean equals(Object arg0, Object arg1)
throws HibernateException {
if(arg0==arg1){
return true;
}
if(arg0==null||arg1==null){
return false;
}
return arg0.equals(arg1);
}
public int hashCode(Object arg0) throws HibernateException {
Address address = (Address)arg0;
return address.hashCode();
}
public boolean isMutable() {
return true;
}
public Object nullSafeGet(ResultSet arg0, String[] arg1, Object arg2)
throws HibernateException, SQLException {
String province = (String)Hibernate.STRING.nullSafeGet(arg0,
arg1[3]);
String city = (String)Hibernate.STRING.nullSafeGet(arg0,
arg1[4]);
return (province==null&&city==null)?null:
new Address(province,city);
}
public void nullSafeSet(PreparedStatement arg0, Object arg1, int arg2)
throws HibernateException, SQLException {
Address address = (arg1==null)?new Address():(Address)arg1;
Hibernate.STRING.nullSafeSet(arg0, address.getProvince(), arg2);
Hibernate.STRING.nullSafeSet(arg0, address.getCity(), arg2+1);
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return arg0;
}
public Class returnedClass() {
return Address.class;
}
public int[] sqlTypes() {
return TYPES;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package funnyweb;
import java.sql.Date;
public class User {
private String name;
private String sex;
private Date birthday;
private Address address;
private String education;
private String school;
private String password;
public User(){
}
public User(String name,String sex,Date birthday,
Address address,String education,String school,
String password){
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
this.education = education;
this.school = school;
this.password = password;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSex(String sex){
this.sex = sex;
}
public String getSex(){
return this.sex;
}
public void setBirthday(Date birthday){
this.birthday = birthday;
}
public Date getBirthday(){
return this.birthday;
}
public void setAddress(Address address){
this.address = address;
}
public Address getAddress(){
if(this.address==null){
this.address = new Address();
}
return this.address;
}
public void setEducation(String education){
this.education = education;
}
public String getEducation(){
return this.education;
}
public void setSchool(String school){
this.school = school;
}
public String getSchool(){
return this.school;
}
public void setPassword(String password){
this.password = password;
}
public String getPassword(){
return this.password;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package funnyweb;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static{
try{
sessionFactory = new Configuration().configure()
.buildSessionFactory();
}catch(Throwable ex){
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session =
new ThreadLocal();
public static Session currentSession()
throws HibernateException{
Session s = (Session)session.get();
if(s==null||!s.isOpen()){
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void rollBack(Transaction tx){
try{
if(tx!=null){
tx.rollback();
}
}catch(HibernateException e){
System.out.println("rollback failed." + e);
}
}
public static void closeSession() throws HibernateException{
Session s = (Session)session.get();
session.set(null);
if(s!=null)
s.close();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
package funnyweb;
import org.hibernate.*;
public class UserDAO {
private Session session;
private Transaction tx;
public UserDAO(){
session = HibernateUtil.currentSession();
}
public void create(User user) throws RuntimeException{
try{
tx = session.beginTransaction();
session.save(user);
session.flush();
tx.commit();
}catch(HibernateException e){
HibernateUtil.rollBack(tx);
}finally{
HibernateUtil.closeSession();
}
}
}
posted on 2009-09-16 15:37 FunnyLeung 阅读(295) 评论(0) 编辑 收藏 举报