1、Hibernate3.6的主要AIP組件
1.1 Configuration:用於配置並且啓動Hibernate框架
每個Hibernate配置文件對應一個Configuration對象。Hibernate通過Configuration實例的configure()方法讀取配置文件Hibernate.cfg.xml,在通過Configuration實例的buildSessionFactory()方法創建SessionFactory實例。
Configuration configuration=new Configuration().configure();
1.2 SessionFactory:會話工廠
一個SessionFactory實例對應一個數據源,即一個數據庫。
SessionFactory sessionFactory=configuration.buildSessionFactory();
1.3 Session:會話管理器,簡稱“會話”
一個Session實例完成一次會話,一次會話就是一次對數據庫的訪問。應用程序通過SessionFactory的openSession()方法或getCurrentSession()方法中獲得Session實例。
Session session=sessionFactroy.openSession();
1.4 Query:查詢組件
Query用於從數據庫中查詢對象,並控制執行查詢的過程。Query包裝了HQL查詢語句,HQL查詢語句是變面向對象的,它引用類名及類的屬性名,而不是應用表名及字段名。
通過Session實例的createQuery()方法創建Query實例,典型的語句爲:
Query query=session.createQuery(HQL語句);
1.5 Transaction:事務管理器
Transaction是Hibernate的數據庫事務管理器,它對底層的事務做了封裝,底層事務包括JDBC事務和JTA事務
try
{
Transaction tran=session.beginTransaction();
...
tran.commit();
}
catch(Exception e)
{
e.printStackTrace();
tran.rollback();
}
2、實例:學生Student和班級Clazz,雙向一對多關係
2.1 導入Hibernate必需jar包
2.2 Hibernate配置文件hibernate.cfg.xml的編寫
//放於/src目錄下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--jdbc驅動,此處使用的數據庫爲sqlserver-->
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.password">
</property>
<property name="hibernate.connection.url">
jdbc:sqlserver://127.0.0.1:1433;DatabaseName=數據庫名字
</property>
<property name="hibernate.connection.username">
sa
</property>
<!--數據庫方言-->
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!--根據映射文件自動創建數據庫表格-->
<property name="hbm2ddl.auto">
create
</property>
<mapping resource="com/po/Student.hbm.xml" />
<mapping resource="com/po/Clazz.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.3 創建session的公共類
//HibernateServiceProvider.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateServiceProvider {
private static SessionFactory sessionFactory=null;
private static Configuration configuration=new Configuration();
public HibernateServiceProvider()
{
this.initHibernate();
}
public synchronized void initHibernate()
{
if(sessionFactory==null)
{
try
{
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void rebuildSessionFactory()
{
try
{
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Session getSession()
{
if(sessionFactory==null)
{
this.rebuildSessionFactory();
}
Session session=sessionFactory.openSession();
return session;
}
public void closeSession()
{
try
{
sessionFactory.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
2.4 PO類和映射文件的編寫
//Clazz.java
import java.util.HashSet;
import java.util.Set;
public class Clazz
{
private long claId;
private String claName;
private Set<Student> students=new HashSet<Student>();
public long getClaId() {
return claId;
}
public void setClaId(long claId) {
this.claId = claId;
}
public String getClaName() {
return claName;
}
public void setClaName(String claName) {
this.claName = claName;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
//映射文件Clazz.hbm.xml,與po類放於同一目錄下
<?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="com.po.Clazz" table="Clazz">
<id name="claId" type="long">
<column name="lClaId"/>
<generator class="native"/>
</id>
<property name="claName" type="java.lang.String">
<column name="vClaName" length="20"/>
</property>
<set name="students" table="Student" lazy="true" cascade="all">
<key>
<column name="lClaId"/>
</key>
<one-to-many class="com.po.Student"/>
</set>
</class>
</hibernate-mapping>
//Student.java
public class Student
{
private long stuId;
private String stuName;
private Clazz stuCla;
public long getStuId() {
return stuId;
}
public void setStuId(long stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Clazz getStuCla() {
return stuCla;
}
public void setStuCla(Clazz stuCla) {
this.stuCla = stuCla;
}
}
//映射文件Student.hbm.xml,與對應po類放於同一目錄下
<?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="com.po.Student" table="Student">
<id name="stuId" type="long">
<column name="lStuId"/>
<generator class="native"/>
</id>
<property name="stuName" type="java.lang.String">
<column name="vStuName" length="20"/>
</property>
<many-to-one name="stuCla" class="com.po.Clazz" fetch="join" cascade="all">
<column name="lClaId"/>
</many-to-one>
</class>
</hibernate-mapping>
2.5 Dao類的編寫
//ClazzDao.java
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.po.Clazz;
public class ClazzDao
{
HibernateServiceProvider hsProvider=new HibernateServiceProvider();
public boolean addClass(Clazz clazz)
{
boolean flag=false;
Session session=hsProvider.getSession();
Transaction trans=null;
try
{
trans=session.beginTransaction();
session.save(clazz);
trans.commit();
flag=true;
}
catch(Exception e)
{
e.printStackTrace();
trans.rollback();
}
return flag;
}
}
省略其他Dao類