Hibernate的映射機(jī)制
來源:程序員人生 發(fā)布時(shí)間:2014-12-23 09:05:57 閱讀次數(shù):3368次
Hibernate的映照機(jī)制
對象關(guān)系映照(Object Relation Mapping(ORM))是1種為了解決面向?qū)ο笈c面向關(guān)系
數(shù)據(jù)庫互不匹配現(xiàn)象的技術(shù),簡而言之
ORM是通過使用描寫對象之間映照的元數(shù)據(jù),將java程序中的對象自動持久化到關(guān)系
數(shù)據(jù)庫中,這類映照機(jī)制從本質(zhì)上來講
其實(shí)就是將數(shù)據(jù)從1種情勢轉(zhuǎn)化為另外一種情勢
Hibernate的基本映照數(shù)據(jù)類型
Hibernate的基本映照數(shù)據(jù)類型是java基本類型與標(biāo)準(zhǔn)SQL類型相互轉(zhuǎn)化的橋梁,其關(guān)系
java類型----------->Hibernate的映照數(shù)據(jù)類型----------->標(biāo)準(zhǔn)SQL類型
通過Hibernate的基本映照數(shù)據(jù)類型可以非常方便地將數(shù)據(jù)從1種情勢轉(zhuǎn)化成另外一個(gè)情勢,完成高質(zhì)量的ORM任務(wù)
...
<!--
name:這是持久化類User.java中1個(gè)String類型的屬性
column:這是
數(shù)據(jù)庫表user中1個(gè)類型為char(20)的字段
type:這是Hibernate映照類型
-->
<property name="password"
column="password"
type="string"
...
>
datamap數(shù)據(jù)表
-----------------------------------------------------------------------------------------------
字段名稱
數(shù)據(jù)類型
主鍵
自增
允許為空
描寫
ID
int(4)
是
增1
否
ID號
MYBOOLEAN
bit(1)
邏輯型數(shù)據(jù)
MYINT
int(5)
整型數(shù)據(jù)
MYLONG
bigint(11)
長整型數(shù)據(jù)
MYFLOAT
float(8,2)
單精度浮點(diǎn)型數(shù)據(jù)
MYDOUBLE
double(10,2)
雙精度浮點(diǎn)型數(shù)據(jù)
MYDECIMAL
decimal(10,2)
Decimal型數(shù)據(jù)
MYSTRING
varchar(100)
字符串?dāng)?shù)據(jù)
MYTEXT
text
Text型數(shù)據(jù)
MYDATE
date
Date型數(shù)據(jù)
MYTIME
time
Time型數(shù)據(jù)
MYDATETIME
datetime
Datetime型數(shù)據(jù)
MYTIMESTAMP
timestamp
Timestamp型數(shù)據(jù)
MYBINARY
varbinary(10240)
Binary型數(shù)據(jù)
MYBLOB
longblob
Blob型數(shù)據(jù)
------------------------------------------------------------------------------------------------
其對應(yīng)的持久化類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(){}//構(gòu)造方法
//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)創(chuàng)建數(shù)據(jù)訪問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)創(chuàng)建數(shù)據(jù)訪問DAO接口實(shí)現(xiàn)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)創(chuàng)建1個(gè)可供測試用的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內(nèi)容
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();
}
//讀取2進(jìn)制流
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;
}
}
//從輸出流中創(chuàng)建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的映照機(jī)制
對象關(guān)系映照(Object Relation Mapping(ORM))是1種為了解決面向?qū)ο笈c面向關(guān)系
數(shù)據(jù)庫互不匹配現(xiàn)象的技術(shù),簡而言之
ORM是通過使用描寫對象之間映照的元數(shù)據(jù),將java程序中的對象自動持久化到關(guān)系
數(shù)據(jù)庫中,這類映照機(jī)制從本質(zhì)上來講
其實(shí)就是將數(shù)據(jù)從1種情勢轉(zhuǎn)化為另外一種情勢
Hibernate的基本映照數(shù)據(jù)類型
Hibernate的基本映照數(shù)據(jù)類型是java基本類型與標(biāo)準(zhǔn)SQL類型相互轉(zhuǎn)化的橋梁,其關(guān)系
java類型----------->Hibernate的映照數(shù)據(jù)類型----------->標(biāo)準(zhǔn)SQL類型
通過Hibernate的基本映照數(shù)據(jù)類型可以非常方便地將數(shù)據(jù)從1種情勢轉(zhuǎn)化成另外一個(gè)情勢,完成高質(zhì)量的ORM任務(wù)
...
<!--
name:這是持久化類User.java中1個(gè)String類型的屬性
column:這是
數(shù)據(jù)庫表user中1個(gè)類型為char(20)的字段
type:這是Hibernate映照類型
-->
<property name="password"
column="password"
type="string"
...
>
datamap數(shù)據(jù)表
-----------------------------------------------------------------------------------------------
字段名稱
數(shù)據(jù)類型
主鍵
自增
允許為空
描寫
ID
int(4)
是
增1
否
ID號
MYBOOLEAN
bit(1)
邏輯型數(shù)據(jù)
MYINT
int(5)
整型數(shù)據(jù)
MYLONG
bigint(11)
長整型數(shù)據(jù)
MYFLOAT
float(8,2)
單精度浮點(diǎn)型數(shù)據(jù)
MYDOUBLE
double(10,2)
雙精度浮點(diǎn)型數(shù)據(jù)
MYDECIMAL
decimal(10,2)
Decimal型數(shù)據(jù)
MYSTRING
varchar(100)
字符串?dāng)?shù)據(jù)
MYTEXT
text
Text型數(shù)據(jù)
MYDATE
date
Date型數(shù)據(jù)
MYTIME
time
Time型數(shù)據(jù)
MYDATETIME
datetime
Datetime型數(shù)據(jù)
MYTIMESTAMP
timestamp
Timestamp型數(shù)據(jù)
MYBINARY
varbinary(10240)
Binary型數(shù)據(jù)
MYBLOB
longblob
Blob型數(shù)據(jù)
------------------------------------------------------------------------------------------------
其對應(yīng)的持久化類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(){}//構(gòu)造方法
//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)創(chuàng)建數(shù)據(jù)訪問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)創(chuàng)建數(shù)據(jù)訪問DAO接口實(shí)現(xiàn)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)創(chuàng)建1個(gè)可供測試用的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內(nèi)容
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();
}
//讀取2進(jìn)制流
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;
}
}
//從輸出流中創(chuàng)建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;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈