设为首页 加入收藏

TOP

Hibernate框架映射Oracle中long类型字段(一)
2014-11-24 08:32:24 来源: 作者: 【 】 浏览:3
Tags:Hibernate 框架 映射 Oracle long 类型

在Oracle中:LONG 数据类型中存储的是可变长字符串,最大长度限制是2GB;对于超出一定长度的文本,基本只能用LONG类型来存储,数据字典中很多对象的定义就是用LONG来存储的(当然你也可以考虑使用像CLOB这种大字段处理这些文本);LONG类型主要用于不需要作字符串搜索的长串数据,如果要进行字符搜索就要用varchar2类型。当然,LONG也有限制:一个表中只能包含一个 LONG 类型的列;不能索引LONG类型列,等等。(以上对LONG的解析不完整,详细的可以再网上做了解)


明显,使用Hibernate做ORM时,java提供的类型或者Hibernate提供的数据都没有完全匹配LONG的。看网上有很多朋友建议使用 String,Char[]等,String的最多在我的方法中使用到String类。但是如果直接使用String做ORM时,数据量很大在插入式就会 出现问题了。



解决办法:做ORM时使用自定类型,对LONG的字段用流进行读写。


package org.comsys.userType;


import java.io.IOException;


import java.io.Reader;


import java.io.Serializable;


import java.io.StringReader;


import java.sql.PreparedStatement;


import java.sql.ResultSet;


import java.sql.SQLException;


import org.hibernate.HibernateException;


import org.hibernate.usertype.UserType;


public class CustomLong implements UserType {


publicCustomLong() {


}


//序列化


publicObject assemble(Serializable ser, Object obj)


throwsHibernateException {


//TODO Auto-generated method stub


returnnull;


}


//深度拷贝,强制返回字符串


publicObject deepCopy(Object value) throws HibernateException {


//TODO Auto-generated method stub


if(null == value)


return"";


else


returnnew String((String) value);


}


//反序列化


publicSerializable disassemble(Object arg0) throws HibernateException {


//TODO Auto-generated method stub


returnnull;


}


//判断是否相等


publicboolean equals(Object x, Object y) throws HibernateException {


//TODO Auto-generated method stub


return(x == y) || (x != null && y != null && (x.equals(y)));


}


//获取哈希码


public inthashCode(Object arg0) throws HibernateException {


//TODO Auto-generated method stub


return0;


}


// 是否可变


publicboolean isMutable() {


//TODO Auto-generated method stub


returnfalse;


}


//获取字段值


publicObject nullSafeGet(ResultSet rs, String[] name, Object obj)


throwsHibernateException, SQLException {


//TODO Auto-generated method stub


char[]content = new char[1024000];//第一字符数组保存流读出的内容


char[]buffer = new char[1024];//存放每次读的内容


intlen = 0;


intoff = 0;


intcontentLen=1024000;


Readerreader = rs.getCharacterStream(name[0]);//ResultSet结果集中读字符流的方法


//下面是基本的字符流的方法


try {


while(true){


len= reader.read(buffer);


if(len==-1)


break;


if(off+len> contentLen){//代表字段的内容超出了我们预定义的内容的大小,需要扩充


char[]tmp = new char[contentLen+1024000];//定义扩充区


System.arraycopy(content,0, tmp, 0, off);//将content拷贝到扩充区中,扩充


content= tmp;


contentLen= contentLen + 1024000;//长度扩充


}


System.arraycopy(buffer,0, content, off, len);//最后将每次读的都拷贝到content中


off+=len;//记录读的位置长度


}


}catch (IOException e) {


e.printStackTrace();


}


returnnew String(content,0,off);


}


public voidnullSafeSet(PreparedStatement pr, Object value, int index)


throwsHibernateException, SQLException {


//TODO Auto-generated method stub


Strings = (String) value;


System.out.println(s);


if(null == s)


s= "";


Readerre = new StringReader(s);//将内容字符串用StringReader读入


pr.setCharacterStream(index,re, s.length());//最后用PreparedStatement的方法设置字段内容


}


publicObject replace(Object arg0, Object arg1, Object arg2)


throwsHibernateException {


//TODO Auto-generated method stub


returnnull;


}


public ClassreturnedClass() {//返回java类型


//TODO Auto-generated method stub


returnjava.lang.String.class;


}


public int[]sqlTypes() {//返回sql属性类型


//TODO Auto-generated method stub


returnnew int[] { java.sql.Types.LONGVARCHAR };


}


}


接下来是配置****.hbm.xml






最后在hibernate.cfg.xml中加入配置



0


否则会有以下错误



实体类中对应字段的类型依然为String,这样就ok了,希望对正在尝试hibernate映射Oracle中LONG的朋友们有

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UNIX TCP回射服务器/客户端 下一篇Linux下多线程通过蒙特卡洛法来求..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·定义一个类模板并实 (2025-12-27 06:52:28)
·一文搞懂怎么用C语言 (2025-12-27 06:52:25)
·常用C模板范文_百度 (2025-12-27 06:52:21)
·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)