Hibernate的配置

来源:互联网 发布:matlab2016b是什么软件 编辑:程序博客网 时间:2024/05/22 11:52

有段时间没有用了,生疏了,记录下

1)下载hibernate所用的包

http://www.hibernate.org

2) 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 name="foo"><!--数据库连接配置方式 下面是oracle数据库的配置方法-->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:TEST</property>
<!--<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>-->
<property name="hibernate.connection.username">lcunp</property>
<property name="hibernate.connection.password">lcunp</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property><!--运行的时候,显示sql语句-->
<mapping resource="com/lianchuang/hibernate/domain/User.hbm.xml"/><!--持久化映射文件-->
</session-factory>
</hibernate-configuration>

3)domain/User.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.lianchuang.hibernate.domain">
    <class name="User">
    <id name="id">
        <generator class="native"/>
    </id>
    <property name="userName"
     length="10"/>
    
    <property name="password"
     not-null="true"
     length="15"
     column="`password`"/>
</class>


</hibernate-mapping>

4) User.java

package com.lianchuang.hibernate.domain;


public class User{
private Long id;
private String userName;
private String password;
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 Long getId() {
return id;
}


public void setId(Long long1) {
id = long1;
}
}

5) 运用

package com.lianchuang.hibernate;


import java.io.Serializable;


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


public final class HibernateUtil {


public static SessionFactory sessionFactory;


static {
Configuration cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}


public static SessionFactory getSessionFactory() {


return sessionFactory;
}


public static Session getSession() {


return sessionFactory.openSession();
}


public static void add(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
session.save(entity);
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
if (tx == null) {
tx.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
}


public static void update(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
session.update(entity);
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
if (tx == null) {
tx.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
}


public static void delete(Object entity) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
session.delete(entity);
tx = session.beginTransaction();
tx.commit();
} catch (HibernateException e) {
if (tx == null) {
tx.rollback();
throw e;
}
} finally {
if (session != null) {
session.close();
}
}
}


public static Object get(Class clazz, Serializable id) {
Session session = null;
try {
session = HibernateUtil.getSession();
Object obj = session.get(clazz,id);
return obj;
} finally {
if (session != null) {
session.close();
}
}
}
}

6)理解下,临时,持久,脱管的概念