Hibernate4 解决Clob问题,网上很多都是hibernate3

阅读更多

hibernate4在处理clob字段上面和hibernate3有很大的不一样

今天感觉遇到了很大一个坑,但是好在解决了。

解决方案如下

创建类

package org.moon.framework.util.clob;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Map;

/**
 * Created by Administrator on 2017/1/5.
 */
public class SpringContextHolder implements ApplicationContextAware {

    private static ApplicationContext applicationContext;


    //实现ApplicationContextAware接口的context注入函数, 将其存入静态变量.
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextHolder.applicationContext = applicationContext;
    }


    //取得存储在静态变量中的ApplicationContext.
    public static ApplicationContext getApplicationContext() {
        checkApplicationContext();
        return applicationContext;
    }

    //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    @SuppressWarnings("unchecked")
    public static  T getBean(String name) {
        checkApplicationContext();
        return (T) applicationContext.getBean(name);
    }


    //从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
    //如果有多个Bean符合Class, 取出第一个.
    @SuppressWarnings("unchecked")
    public static  T getBean(Class clazz) {
        checkApplicationContext();
        @SuppressWarnings("rawtypes")
        Map beanMaps = applicationContext.getBeansOfType(clazz);
        if (beanMaps!=null && !beanMaps.isEmpty()) {
            return (T) beanMaps.values().iterator().next();
        } else{
            return null;
        }
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
        }
    }

}

再创建

package org.moon.framework.util.clob;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.compare.EqualsHelper;
import org.hibernate.usertype.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.jdbc.support.lob.OracleLobHandler;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2017/1/5.
 */
public class ClobTypeString implements UserType, Serializable{



    public int[] sqlTypes() {
        return new int[] { Types.CLOB };
    }


    public Class returnedClass() {
        return String.class;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return EqualsHelper.equals(x, y);
    }


    public int hashCode(Object x) throws HibernateException {
        // TODO Auto-generated method stub
        return x.hashCode();
    }


    public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
        if (null != names && names.length > 0) {
            LobHandler lobHandler = getCurrentLobHandler(session);
            return lobHandler.getClobAsString(rs, names[0]);
        }
        return "";
    }


    public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
            throws HibernateException, SQLException {

        LobHandler lobHandler = getCurrentLobHandler(session);
        lobHandler.getLobCreator().setClobAsString(st, index, (String) value);

    }


    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }


    public boolean isMutable() {
        // TODO Auto-generated method stub
        return false;
    }


    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable) value;
    }


    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }


    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return original;
    }

    private LobHandler getCurrentLobHandler(SessionImplementor session) {
        String dialect = session.getFactory().getDialect().getClass().getName();
        if (null != dialect && dialect.toLowerCase().contains("oracle")) {
            return SpringContextHolder.getBean("oracleLobHandler");
        }
        return SpringContextHolder.getBean("defaultLobHandler");
    }
}

 

    applicationContext.xml 中加入




   
      
   






    因为使用的是alibab的druid数据连接池,还要引用一个jar

<dependency>
  <groupId>c3p0groupId>
  <artifactId>c3p0artifactId>
  <version>0.9.1.2version>
dependency>

   hbm配置

xml version="1.0"?>
hibernate-mappingPUBLIC"-//Hibernate/Hibernate Mapping DTD 4.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.sofn.cms.beans.Article" table="CMS_ARTICLE">
        <id name="pk_article" type="string" column="pk_article">
            <generator class="org.moon.framework.util.sys.HibernateOIDGenerator"/>
        id>
        <version name="ts" column="ts" type="timestamp">version>
        <property name="articletitle" type="string" column="articletitle"/>
        <property name="pk_articletype" type="string" column="pk_articletype"/>
        <property name="articleauthor" type="string" column="articleauthor"/>
        <property name="articleimg" type="string" column="articleimg"/>
        <property name="articleorders" type="big_decimal" column="articleorders"/>
        <property name="articlestatus" type="string" column="articlestatus"/>
        <property name="articleinfo" type="org.moon.framework.util.clob.ClobTypeString" column="articleinfo"/>
        <property name="datastatus" type="string" column="datastatus"/>
    class>
hibernate-mapping>

   java bean对象

package com.sofn.cms.beans;

import org.moon.framework.beans.pub.AbstractValueObject;

import java.math.BigDecimal;
import java.sql.Clob;
import java.util.Date;
import java.sql.Blob;

/**
 * 文章信息模型对象
* @author moon.l
 *
 */
@SuppressWarnings("serial")
public class Article extends AbstractValueObject {


    /*
    * 主键    */
private String pk_article;//主键
/*
    * 对应字段    */
private String articletitle; //文章标题
private String pk_articletype; //文章类型
private String articleauthor; //文章作者
private String articleimg; //文章插图
private BigDecimal articleorders; //文章排序
private String articlestatus; //文章状态
private String articleinfo; //文章内容
private String datastatus; //数据状态
/*
    * 主键    */
public String getPrimaryKey() {
        return pk_article;
    }

    public void setPrimaryKey(String key) {
        this.pk_article = key;
    }

    public String getPk_article(){
        return pk_article;
    }
    //pk_article set
public void setPk_article(String pk_article){
        this.pk_article = pk_article;
    }

    /*
    *其余字段    */
    //文章标题 get
public String getArticletitle(){
        return articletitle;
    }
    //文章标题 set
public void setArticletitle(String articletitle){
        this.articletitle = articletitle;
    }
    //文章类型 get
public String getPk_articletype(){
        return pk_articletype;
    }
    //文章类型 set
public void setPk_articletype(String pk_articletype){
        this.pk_articletype = pk_articletype;
    }
    //文章作者 get
public String getArticleauthor(){
        return articleauthor;
    }
    //文章作者 set
public void setArticleauthor(String articleauthor){
        this.articleauthor = articleauthor;
    }
    //文章插图 get
public String getArticleimg(){
        return articleimg;
    }
    //文章插图 set
public void setArticleimg(String articleimg){
        this.articleimg = articleimg;
    }
    //文章排序 get
public BigDecimal getArticleorders(){
        return articleorders;
    }
    //文章排序 set
public void setArticleorders(BigDecimal articleorders){
        this.articleorders = articleorders;
    }
    //文章状态 get
public String getArticlestatus(){
        return articlestatus;
    }
    //文章状态 set
public void setArticlestatus(String articlestatus){
        this.articlestatus = articlestatus;
    }
    //文章内容 get
public String  getArticleinfo(){
        return articleinfo;
    }
    //文章内容 set
public void setArticleinfo(String articleinfo){
        this.articleinfo = articleinfo;
    }
    //数据状态 get
public String getDatastatus(){
        return datastatus;
    }
    //数据状态 set
public void setDatastatus(String datastatus){
        this.datastatus = datastatus;
    }
}

   对应字段类型为String

  

   再来做下测试

  TestSpring.java

package org.moon;

import com.sofn.cms.beans.Article;
import com.sofn.cms.service.ArticleService;
import freemarker.template.TemplateException;
import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.LobHelper;
import org.hibernate.internal.SessionImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.moon.framework.beans.Table;
import org.moon.framework.dao.BaseDAO;
import org.moon.framework.service.BaseService;
import org.moon.framework.service.FreemarkerSrcService;
import org.moon.framework.service.codefree.CreateBean;
import org.moon.framework.service.codefree.CreateHbm;
import org.moon.framework.util.sys.DataType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobCreatorUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2016/12/30.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestSpring {

    private static Logger logger = Logger.getLogger(TestSpring.class);

//    @Autowired
//    private ApplicationContext applicationContext;
@Autowired
private ArticleService articleService;

    @Autowired
FreemarkerSrcService freemarkerSrcService;

//    @Test
//    public void createCode() throws TemplateException, IOException, SQLException {
//        Table table = new Table();
//        table.setTableName("CMS_ARTICLE");
//        table.setBeanCode("Article");
//        table.setBeanName("文章信息");
//        table.setPackageName("com.sofn.cms");
//        table.setFilesUrl("E:/code");
//        table = freemarkerSrcService.getTableInfo(table);
//        CreateBean.createBean(table);
//        CreateHbm.createHbm(table);
//    }
@Test
public void test(){


        Article vo = new Article();
        vo.setArticletitle("测试");
        vo.setArticleinfo("啊实打实我问问的");
        articleService.save(vo);
        //
String hql = "from Article vo";
        List
list = (List
)articleService.query(hql); for (Article article : list) { System.out.println(article.getArticleinfo()); } } }

  运行结果

Hibernate: insert into CMS_ARTICLE (ts, articletitle, pk_articletype, articleauthor, articleimg, articleorders, articlestatus, articleinfo, datastatus, pk_article) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select article0_.pk_article as pk_artic1_1_, article0_.ts as ts2_1_, article0_.articletitle as articlet3_1_, article0_.pk_articletype as pk_artic4_1_, article0_.articleauthor as articlea5_1_, article0_.articleimg as articlei6_1_, article0_.articleorders as articleo7_1_, article0_.articlestatus as articles8_1_, article0_.articleinfo as articlei9_1_, article0_.datastatus as datasta10_1_ from CMS_ARTICLE article0_
爱出发穿
啊实打实的
啊实打实我问问的

 问题已解决

  完整:applicationContext.xml 





	
	
	
	

	
	


	
	
		
		
		

		
		
		
		
		
		
		
		
		
		

		
		

		
		
		
		

		
		
		
		

		
		
		
		
		
		

		
		
		
	


	
	
		
		
			
				
				${hibernate.dialect}
				${hibernate.show_sql}
				${hibernate.format_sql}
			
		

		
		
			
				classpath:hbm
				classpath:pubhbm
			
		
	

	
	
		
	


	


	
	
		
	

	
	
		
		
	



	
	
	
		
			
		
	
	
	

	

	


 

 

你可能感兴趣的:(Hibernate4,解决Clob问题)