程序员人生 网站导航

Hibernate的映射机制

栏目:数据库应用时间:2014-12-23 09:05:57
Hibernate的映照机制
对象关系映照(Object Relation Mapping(ORM))是1种为了解决面向对象与面向关系数据库互不匹配现象的技术,简而言之
ORM是通过使用描写对象之间映照的元数据,将java程序中的对象自动持久化到关系数据库中,这类映照机制从本质上来讲
其实就是将数据从1种情势转化为另外一种情势


Hibernate的基本映照数据类型
Hibernate的基本映照数据类型是java基本类型与标准SQL类型相互转化的桥梁,其关系
java类型----------->Hibernate的映照数据类型----------->标准SQL类型
通过Hibernate的基本映照数据类型可以非常方便地将数据从1种情势转化成另外一个情势,完成高质量的ORM任务
...
<!--
name:这是持久化类User.java中1个String类型的属性
column:这是数据库表user中1个类型为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 datetime Datetime型数据
MYTIMESTAMP timestamp Timestamp型数据
MYBINARY varbinary(10240) Binary型数据
MYBLOB longblob Blob型数据
------------------------------------------------------------------------------------------------
其对应的持久化类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)创建1个可供测试用的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();
}
//读取2进制流
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))是1种为了解决面向对象与面向关系数据库互不匹配现象的技术,简而言之
ORM是通过使用描写对象之间映照的元数据,将java程序中的对象自动持久化到关系数据库中,这类映照机制从本质上来讲
其实就是将数据从1种情势转化为另外一种情势


Hibernate的基本映照数据类型
Hibernate的基本映照数据类型是java基本类型与标准SQL类型相互转化的桥梁,其关系
java类型----------->Hibernate的映照数据类型----------->标准SQL类型
通过Hibernate的基本映照数据类型可以非常方便地将数据从1种情势转化成另外一个情势,完成高质量的ORM任务
...
<!--
name:这是持久化类User.java中1个String类型的属性
column:这是数据库表user中1个类型为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 datetime Datetime型数据
MYTIMESTAMP timestamp Timestamp型数据
MYBINARY varbinary(10240) Binary型数据
MYBLOB longblob Blob型数据
------------------------------------------------------------------------------------------------
其对应的持久化类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)创建1个可供测试用的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();
}
//读取2进制流
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;
}
------分隔线----------------------------
------分隔线----------------------------

最新技术推荐