经过N天的痛苦探索,终于找到了大文本字段在hiberate中的处理方法
配置如下:
applicationContent.xml中。注意:此处的配置为关键
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="lobHandler">
<ref bean="myLobHandler"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!--
clob类型配置
-->
<bean id="myLobHandler" class="com.zytech.util.MyLobHandler" >
<property name="oracleLobHandler"><ref local="oracleLobHandler"/></property>
<property name="defaultLobHandler"><ref local="defaultLobHandler"/></property>
<!-- 数据库类型,不同数据库处理不一样 -->
<property name="dataBaseType"><value>Oracle</value></property>
</bean>
<bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor"><ref local="nativeJdbcExtractor"/></property>
</bean>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler">
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor">
</bean>
<!-- clob配置结束 -->
com.zytech.util.MyLobHandler.java
用于完成clob到string的自动转化工作
/**
*
*/
package com.zytech.util;
import java.io.InputStream;
import java.io.Reader;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
/**
* @author Administrator
*
*/
public class MyLobHandler implements LobHandler {
private final Log log = LogFactory.getLog(getClass());
private LobHandler defaultLobHandler;
private LobHandler oracleLobHandler;
private LobHandler myLobHandler;
private boolean isOracle;
private String dataBaseType;
/**
*
*/
private MyLobHandler() {
super();
}
/**
* @return Returns the isOracle.
*/
private boolean isOracle() {
return getDataBaseType().toLowerCase().indexOf("Oracle") != -1;
}
/**
* @return Returns the oracleLobHandler.
*/
private LobHandler getOracleLobHandler() {
return oracleLobHandler;
}
/**
* @param oracleLobHandler
* The oracleLobHandler to set.
*/
public void setOracleLobHandler(LobHandler oracleLobHandler) {
this.oracleLobHandler = oracleLobHandler;
}
/**
* @return Returns the myLobHandler.
*/
private LobHandler getMyLobHandler() {
if (isOracle()) {
myLobHandler = getOracleLobHandler();
} else {
myLobHandler = getDefaultLobHandler();
}
return myLobHandler;
}
/**
* @param myLobHandler
* The myLobHandler to set.
*/
private void setMyLobHandler(LobHandler myLobHandler) {
this.myLobHandler = myLobHandler;
}
/**
* @return Returns the defaultLobHandler.
*/
private LobHandler getDefaultLobHandler() {
return defaultLobHandler;
}
/**
* @param defaultLobHandler
* The defaultLobHandler to set.
*/
public void setDefaultLobHandler(LobHandler defaultLobHandler) {
this.defaultLobHandler = defaultLobHandler;
}
/**
* @return Returns the dataBaseType.
*/
private String getDataBaseType() {
return dataBaseType;
}
/**
* @param dataBaseType
* The dataBaseType to set.
*/
public void setDataBaseType(String dataBaseType) {
this.dataBaseType = dataBaseType;
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBytes(java.sql.ResultSet, java.lang.String)
*/
public byte[] getBlobAsBytes(ResultSet arg0, String arg1)
throws SQLException {
return getMyLobHandler().getBlobAsBytes(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBytes(java.sql.ResultSet, int)
*/
public byte[] getBlobAsBytes(ResultSet arg0, int arg1) throws SQLException {
return getMyLobHandler().getBlobAsBytes(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBinaryStream(java.sql.ResultSet, java.lang.String)
*/
public InputStream getBlobAsBinaryStream(ResultSet arg0, String arg1)
throws SQLException {
return getMyLobHandler().getBlobAsBinaryStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getBlobAsBinaryStream(java.sql.ResultSet, int)
*/
public InputStream getBlobAsBinaryStream(ResultSet arg0, int arg1)
throws SQLException {
return getMyLobHandler().getBlobAsBinaryStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsString(java.sql.ResultSet, java.lang.String)
*/
public String getClobAsString(ResultSet arg0, String arg1)
throws SQLException {
return getMyLobHandler().getClobAsString(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsString(java.sql.ResultSet, int)
*/
public String getClobAsString(ResultSet arg0, int arg1) throws SQLException {
return getMyLobHandler().getClobAsString(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsAsciiStream(java.sql.ResultSet, java.lang.String)
*/
public InputStream getClobAsAsciiStream(ResultSet arg0, String arg1)
throws SQLException {
return getMyLobHandler().getClobAsAsciiStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsAsciiStream(java.sql.ResultSet, int)
*/
public InputStream getClobAsAsciiStream(ResultSet arg0, int arg1)
throws SQLException {
return getMyLobHandler().getClobAsAsciiStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsCharacterStream(java.sql.ResultSet, java.lang.String)
*/
public Reader getClobAsCharacterStream(ResultSet arg0, String arg1)
throws SQLException {
return getMyLobHandler().getClobAsCharacterStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getClobAsCharacterStream(java.sql.ResultSet, int)
*/
public Reader getClobAsCharacterStream(ResultSet arg0, int arg1)
throws SQLException {
return getMyLobHandler().getClobAsCharacterStream(arg0, arg1);
}
/* (non-Javadoc)
* @see org.springframework.jdbc.support.lob.LobHandler#getLobCreator()
*/
public LobCreator getLobCreator() {
return getMyLobHandler().getLobCreator();
}
}
典型的.hbm.xml配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class
name="jaoso.news.domain.Sq"
table="SQ"
dynamic-update="true"
dynamic-insert="true"
>
<cache usage="read-write"/>
<id
name="id"
type="java.lang.String"
column="ID"
>
<generator class="uuid.hex" />
</id>
<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
unique="false"
length="40"
/>
<property
name="content"
type="org.springframework.orm.hibernate3.support.ClobStringType"
column="CONTENT"
not-null="false"
length="999999"
>
</property>
</class>
</hibernate-mapping>
请注意,type为org.springframework.orm.hibernate3.support.ClobStringType
clob到string的转化使用了spring的功能
实体类Sq.java
import java.io.Serializable;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Sq implements Serializable {
private String id;
private String name;
private String content;
public Sq(){
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
一段典型的测试代码:
package jaoso.framework;
import jaoso.news.domain.Sq;
import jaoso.news.service.SqService;
public class testSq extends AbstractTestBean {
private SqService service;
public void testInsert(){
service = (SqService)applicationContext.getBean("sqService");
try{
Sq sq = new Sq();
sq.setName("xiangck");
sq.setContent("clob test ........");
service.createSq(sq);
assertEquals(sq.getId()!=null,true);
}
catch(Exception ex){
}
}
public void testFind(){
service = (SqService)applicationContext.getBean("sqService");
try{
Sq sq = null;
sq = service.getSq("402881e30679645101067964543f0001");
System.out.print(sq.getContent());
assertEquals(sq.getId()!=null,true);
}
catch(Exception ex){
}
}
}
请注意:oracle的驱动程序应该使用9i的最新版本(到oracle网上下载)
8i或是10g发现都有问题
9.0.2以上版本才可以使用LobHander