Hibernate的映射機制

來源:互聯網
上載者:User

Hibernate的映射機制

Hibernate的映射機制

對象關係映射(Object Relation Mapping(ORM))是一種為瞭解決物件導向與面向關聯式資料庫互不匹配現象的技術,簡而言之ORM是通過使用描述對象之間映射的中繼資料,將java程式中的對象自動持久化到關聯式資料庫中,這種映射機制從本質上來說其實就是將資料從一種形式轉化為另一種形式


Hibernate的基本映射資料類型
Hibernate的基本映射資料類型是java基本類型與標準SQL類型相互轉化的橋樑,其關係
java類型----------->Hibernate的映射資料類型----------->標準SQL類型
通過Hibernate的基本映射資料類型可以非常方便地將資料從一種形式轉化成另一個形式,完成高品質的ORM任務
...
<!--
name:這是持久化類User.java中一個String類型的屬性
column:這是資料庫表user中一個類型為char(20)的欄位
type:這是Hibernate映射類型
-->
<property name="password"
column="password"
type="string"
...
>


datamap資料表
-----------------------------------------------------------------------------------------------
欄位名稱 資料類型 主鍵 自增 允許為空白描述
ID int(4) 是 增1 否ID號
MYBOOLEAN bit(1)邏輯型資料
MYINT int(5) 整型資料
MYLONG bigint(11)長整型資料
MYFLOAT float(8,2)單精確度浮點型資料
MYDOUBLE double(10,2)雙精確度浮點型資料
MYDECIMAL decimal(10,2)Decimal型資料
MYSTRING varchar(100)字串資料
MYTEXT text Text型資料
MYDATE date Date型資料
MYTIME time Time型資料
MYDATETIME datetimeDatetime型資料
MYTIMESTAMP timestampTimestamp型資料
MYBINARY varbinary(10240)Binary型資料
MYBLOB longblobBlob型資料
------------------------------------------------------------------------------------------------
其對應的持久化類Datamap.java
com.hephec.orm
import java.io.Serializable


public class Datamap implements Serializable{
private int hashValue=0;
private Integer id;
private Boolean myboolean;
private Integer myint;
private Long mylong;
private Float myfloat;
private Double mydouble;
private BigDecimal mydecimal;
  private String mytext;
private String mytext;
private Date mydatel;
private Time mytime;
private Date mydatetime;
private Timestamp mytimestamp;
private byte[] mybinary;
private Blob myblob;
private Datamap(){}//構造方法
//getter...setter省略
}
datamap表與Datamap類的ORM對應檔Datamap.hbm.xml
<hibernate-mapping package="com.orm">
<class name="Datamap" table="datamap">
<id name="id" column="ID" type="Integer">
<generator class="identity"/>
</id>
<property name="myboolean" column="MYBOOLEAN" type="boolean"/>
<property name="myint" column="MYINT" type="integer"/>
<property name="mylong" column="MYLONG" type="long"/>
<property name="myfloat" column="MYFLOAT" type="float"/>
<property name="mydouble" column="MYDOUBLE" type="double"/>
<property name="mydecimal" column="MYDECIMAL" type="decimal"/>
<property name="mystring" column="MYSTRING" type="string"/>
<property name="mytext" column="MYTEXT" type="string"/>
<property name="mydate" column="MYDATE" type="date"/>
<property name="mytime" column="MYTIME" type="time"/>
<property name="mydatetime" column="MYDATETIME" type="timestamp"/>
<property name="mytimestamp" column="MYTEMESTAMP" type="timestamp"/>
<property name="mybinary" column="MYBINARY" type="binary"/>
<property name="myblob" column="MYBLOB" type="blob"/>
</class>
<hibernate-mapping/>


(1)建立資料訪問DAO介面TestDAO.java
package com.DAO;
import com.ORM.*;
public interface TestDAO{
public void addDatamap(Datamap datamap);
public Datamap loadDatamap(Integer id);
public void delDatamap(Integer id);
}


(2)建立資料訪問DAO介面實現TestDAOImpl.java
package com.DAO;
import com.ORM.*;
import org.hibernate.*;
public class TestDAOImpl implements TestDAO{
public void addDatamap(Datamap datamap){
Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
session.save(datamap);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
}
public Datamap loadDatamap(Integer id){


Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
datamap=(Datamap)session.get(Datamap.class,id);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
return datamap;
}
public void delDatamap(Integer id){
Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
Datamap datamap=(Datamap)session.load(Datamap.class,id);
session.delete(datamap);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
}
}
(3)建立一個可供測試用的TestBean.java
package com.bean;
import com.ORM.*;
import java.io.*;
import java.math.BigDecimal;
import java.net.*;
import org.hibernate.*;


public class TestBean{
TestDAO dao=new TestDAOImpl();
//得到指定URL的html內容
private String getHtmlByUrl(String url){
StringBuffer hmtl=new StringBuffer();
String line=null;
try{
URL u=new URL(url);
URLConnection uc=u.openConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));
while(line=(br.readLine())!=null){
html.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return html.toString();
}
//讀取二進位流
private byte[] readBinary(InputStream in){
byte[] binCodes=null;
try{
binCodes=new byte[in.available()];
in.read(binCodes);
in.close();
}catch(Exception e){
e.printStackTrace();
}
return binCodes;
}
}
//從輸出資料流中建立BLOB對象
private java.sql.Blob getBlob(InputStream in){
java.sql.Blob blob=null;
try{
blob=Hibernate.createBlob(in);
in.close();
}catch(Exception e){
e.printStackTrace();
}
return blob;
}
Hibernate的映射機制
對象關係映射(Object Relation Mapping(ORM))是一種為瞭解決物件導向與面向關聯式資料庫互不匹配現象的技術,簡而言之
ORM是通過使用描述對象之間映射的中繼資料,將java程式中的對象自動持久化到關聯式資料庫中,這種映射機制從本質上來說
其實就是將資料從一種形式轉化為另一種形式


Hibernate的基本映射資料類型
Hibernate的基本映射資料類型是java基本類型與標準SQL類型相互轉化的橋樑,其關係
java類型----------->Hibernate的映射資料類型----------->標準SQL類型
通過Hibernate的基本映射資料類型可以非常方便地將資料從一種形式轉化成另一個形式,完成高品質的ORM任務
...
<!--
name:這是持久化類User.java中一個String類型的屬性
column:這是資料庫表user中一個類型為char(20)的欄位
type:這是Hibernate映射類型
-->
<property name="password"
column="password"
type="string"
...
>


datamap資料表
-----------------------------------------------------------------------------------------------
欄位名稱 資料類型 主鍵 自增 允許為空白描述
ID int(4) 是 增1 否ID號
MYBOOLEAN bit(1)邏輯型資料
MYINT int(5) 整型資料
MYLONG bigint(11)長整型資料
MYFLOAT float(8,2)單精確度浮點型資料
MYDOUBLE double(10,2)雙精確度浮點型資料
MYDECIMAL decimal(10,2)Decimal型資料
MYSTRING varchar(100)字串資料
MYTEXT text Text型資料
MYDATE date Date型資料
MYTIME time Time型資料
MYDATETIME datetimeDatetime型資料
MYTIMESTAMP timestampTimestamp型資料
MYBINARY varbinary(10240)Binary型資料
MYBLOB longblobBlob型資料
------------------------------------------------------------------------------------------------
其對應的持久化類Datamap.java
com.hephec.orm
import java.io.Serializable


public class Datamap implements Serializable{
private int hashValue=0;
private Integer id;
private Boolean myboolean;
private Integer myint;
private Long mylong;
private Float myfloat;
private Double mydouble;
private BigDecimal mydecimal;
  private String mytext;
private String mytext;
private Date mydatel;
private Time mytime;
private Date mydatetime;
private Timestamp mytimestamp;
private byte[] mybinary;
private Blob myblob;
private Datamap(){}//構造方法
//getter...setter省略
}
datamap表與Datamap類的ORM對應檔Datamap.hbm.xml
<hibernate-mapping package="com.orm">
<class name="Datamap" table="datamap">
<id name="id" column="ID" type="Integer">
<generator class="identity"/>
</id>
<property name="myboolean" column="MYBOOLEAN" type="boolean"/>
<property name="myint" column="MYINT" type="integer"/>
<property name="mylong" column="MYLONG" type="long"/>
<property name="myfloat" column="MYFLOAT" type="float"/>
<property name="mydouble" column="MYDOUBLE" type="double"/>
<property name="mydecimal" column="MYDECIMAL" type="decimal"/>
<property name="mystring" column="MYSTRING" type="string"/>
<property name="mytext" column="MYTEXT" type="string"/>
<property name="mydate" column="MYDATE" type="date"/>
<property name="mytime" column="MYTIME" type="time"/>
<property name="mydatetime" column="MYDATETIME" type="timestamp"/>
<property name="mytimestamp" column="MYTEMESTAMP" type="timestamp"/>
<property name="mybinary" column="MYBINARY" type="binary"/>
<property name="myblob" column="MYBLOB" type="blob"/>
</class>
<hibernate-mapping/>


(1)建立資料訪問DAO介面TestDAO.java
package com.DAO;
import com.ORM.*;
public interface TestDAO{
public void addDatamap(Datamap datamap);
public Datamap loadDatamap(Integer id);
public void delDatamap(Integer id);
}


(2)建立資料訪問DAO介面實現TestDAOImpl.java
package com.DAO;
import com.ORM.*;
import org.hibernate.*;
public class TestDAOImpl implements TestDAO{
public void addDatamap(Datamap datamap){
Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
session.save(datamap);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
}
public Datamap loadDatamap(Integer id){


Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
datamap=(Datamap)session.get(Datamap.class,id);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
return datamap;
}
public void delDatamap(Integer id){
Session session=MySessionFactory.currentSession();
Transaction ts=null;
try{
ts=session.beginTransaction();
Datamap datamap=(Datamap)session.load(Datamap.class,id);
session.delete(datamap);
ts.commit();
}catch(Exception e){
if(ts!=null){
ts.rollback();
}
e.printStackTrace();
}finally{
MySessionFactory.closeSession();
}
}
}
(3)建立一個可供測試用的TestBean.java
package com.bean;
import com.ORM.*;
import java.io.*;
import java.math.BigDecimal;
import java.net.*;
import org.hibernate.*;


public class TestBean{
TestDAO dao=new TestDAOImpl();
//得到指定URL的html內容
private String getHtmlByUrl(String url){
StringBuffer hmtl=new StringBuffer();
String line=null;
try{
URL u=new URL(url);
URLConnection uc=u.openConnection();
BufferedReader br=new BufferedReader(new InputStreamReader(uc.getInputStream()));
while(line=(br.readLine())!=null){
html.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return html.toString();
}
//讀取二進位流
private byte[] readBinary(InputStream in){
byte[] binCodes=null;
try{
binCodes=new byte[in.available()];
in.read(binCodes);
in.close();
}catch(Exception e){
e.printStackTrace();
}
return binCodes;
}
}
//從輸出資料流中建立BLOB對象
private java.sql.Blob getBlob(InputStream in){
java.sql.Blob blob=null;
try{
blob=Hibernate.createBlob(in);
in.close();
}catch(Exception e){
e.printStackTrace();
}
return blob;
}

Hibernate整體理解

Hibernate 的詳細介紹:請點這裡
Hibernate 的:請點這裡

本文永久更新連結地址:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.