hibernate中对CLOB字段数据进行增删修
/*
* Copyright: Jiang Xi Strong Co.. Ltd.
*
* All right reserved.
*
*/
package com.strongit.credit.manager.infomanager.dao.impl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.lang.reflect.Method;
import java.sql.Clob;
import java.util.List;
import oracle.sql.CLOB;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.lob.SerializableClob;
import com.strongit.credit.common.dao.impl.CreditDAO;
import com.strongit.credit.common.filter.ResultFilter;
import com.strongit.credit.common.po.Info;
import com.strongit.credit.common.po.Stat;
import com.strongit.credit.common.po.Subject;
import com.strongit.credit.manager.infomanager.dao.InfoDao;
/*
* Create Date:2008-9-22 上午09:54:46
*
* Author:lanjh
*
* Version: V1.0
*
* Description:对进行修改的功能进行描述
*
*
*/
public class InfoDaoImpl extends CreditDAO implements InfoDao {
private Log log;
public InfoDaoImpl() {
log = LogFactory.getLog(InfoDaoImpl. class );
}
public void addip(Stat stat) {
this .getSession().save(stat);
}
public int count(ResultFilter resultfilter) {
// TODO Auto-generated method stub
return getResultCount(com.strongit.credit.common.po.InfoWrapper. class ,
resultfilter);
}
public void delete(Info info) {
// this.getHibernateTemplate().delete(info);
Transaction tx = this .getSession().beginTransaction();
this .getSession().delete(info);
tx.commit();
}
public Info findInfoById(Long long1) {
return loadInfo(com.strongit.credit.common.po.InfoWrapper. class , long1);
}
public Subject findSubjectById(String s) {
return (Subject) this .getSession().get(
com.strongit.credit.common.po.Subject. class , s);
}
public List getInfos(ResultFilter resultfilter) {
return getResults(com.strongit.credit.common.po.InfoWrapper. class ,
resultfilter);
}
public List getStat() {
String hql = "" ;
hql = " select sum(decode(t.comedate,null,0,1)) AS TOTAL, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,-1),'yyyy-mm'),1,0)) AS LASTMONTH, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,0),'yyyy-mm'),1,0)) AS THISMONTH, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE-1,'yyyy-mm-dd'),1,0)) AS YESTERDAY, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE,'yyyy-mm-dd'),1,0)) AS TODAY from STAT t " ;
SQLQuery query = this .getSession().createSQLQuery(hql);
return query.list();
}
public List getSubjects(ResultFilter resultfilter) {
return getResults(com.strongit.credit.common.po.Subject. class ,
resultfilter);
}
public void insert(Info info) {
Session ssesion = this .getSession();
Transaction ts = ssesion.beginTransaction();
info.setContentClob(Hibernate.createClob( " " ));
ssesion.save(info);
ssesion.flush();
ssesion.refresh(info,LockMode.UPGRADE);
SerializableClob clob = (SerializableClob)info.getContentClob();
java.sql.Clob wrapClob = clob.getWrappedClob();
CLOB tmpClob = (CLOB)wrapClob;
Writer wt;
try {
wt = tmpClob.getCharacterOutputStream();
String content = info.getContent();
wt.write(content);
wt.close();
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(Info info) {
// Session ssesion = this.getSession();
// Transaction ts = ssesion.beginTransaction();
// info = (Info)ssesion.load(Info.class,info.getId(),LockMode.UPGRADE);
//
// info.setContent(info.getContent());
// info.setContentClob(Hibernate.createClob(" "));
// ssesion.update(info);
// ssesion.flush();
// ssesion.refresh(info,LockMode.UPGRADE);
// SerializableClob clob=(SerializableClob)info.getContentClob();
// java.sql.Clob wrapClob = clob.getWrappedClob();
// CLOB tmpClob=(CLOB)wrapClob;
// Writer wt;
// try {
// wt = tmpClob.setCharacterStream(0);
// String content = info.getContent();
// wt.write(content);
// wt.close();
// ts.commit();
// } catch (Exception e) {
// e.printStackTrace();
// }
this .delete(info);
this .insert(info);
}
public void saveOrUpdate(Info info)
{
Session s;
BufferedWriter writer;
s = this .getHibernateTemplate().getSessionFactory().getCurrentSession();
writer = null ;
try
{
Class clazz = Info. class ;
Method m = null ;
String clobValue = null ;
clobValue = info.getContent();
m = clazz.getDeclaredMethod( " setContentClob " , new Class[] {
java.sql.Clob. class
});
m.setAccessible( true );
if (clobValue == null || clobValue.length() == 0 )
m.invoke(info, new Object[] {
Hibernate.createClob( "" )
});
else
m.invoke(info, new Object[] {
Hibernate.createClob( " " )
});
s.saveOrUpdate(info);
if (clobValue != null && clobValue.length() > 0 )
{
s.flush();
s.refresh(info, LockMode.UPGRADE);
CLOB clob = null ;
m = clazz.getDeclaredMethod( " getContentClob " , null );
m.setAccessible( true );
Object obj = m.invoke(info, null );
if (obj instanceof SerializableClob)
clob = (CLOB)((SerializableClob)obj).getWrappedClob();
if (clob != null )
{
writer = new BufferedWriter(clob.getCharacterOutputStream());
writer.write(clobValue);
}
}
}
catch (Exception e)
{
StringBuilder sb = new StringBuilder();
sb.append( " \u6301\u4E45\u5316POJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B " ).append(info.getClass().getName()).append( " \u3002 " ).append(e.getMessage());
log.error(sb.toString());
}
if (writer != null )
try
{
writer.close();
}
catch (IOException e)
{
log.error(e.getMessage());
}
return ;
}
public Info loadInfo(Class clazz, Serializable id) {
Object pojo;
Info info;
BufferedReader reader;
pojo = this .getSession().get(clazz, id);
info = (Info) pojo;
if (pojo == null || ! (pojo instanceof Info)) {
this .getSession().refresh(pojo, LockMode.READ);
}
reader = null ;
try {
Class c = com.strongit.credit.common.po.Info. class ;
Method m = c.getDeclaredMethod( " getContentClob " , null );
m.setAccessible( true );
Object result = m.invoke(pojo, null );
if (result != null ) {
reader = new BufferedReader(((Clob) result)
.getCharacterStream());
StringBuilder sb = new StringBuilder();
for (String line = null ; (line = reader.readLine()) != null ;)
sb.append(line);
if (sb.length() > 0 )
info.setContent(sb.toString());
}
} catch (Exception e) {
StringBuilder sb = new StringBuilder();
sb
.append(
" \u52A0\u8F7DPOJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B " )
.append(clazz.getName()).append( " \u3002 " ).append(
e.getMessage());
log.error(sb.toString());
}
if (reader != null )
try {
reader.close();
} catch (IOException e) {
log.error(e.getMessage());
}
return info;
}
}
* Copyright: Jiang Xi Strong Co.. Ltd.
*
* All right reserved.
*
*/
package com.strongit.credit.manager.infomanager.dao.impl;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.lang.reflect.Method;
import java.sql.Clob;
import java.util.List;
import oracle.sql.CLOB;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.lob.SerializableClob;
import com.strongit.credit.common.dao.impl.CreditDAO;
import com.strongit.credit.common.filter.ResultFilter;
import com.strongit.credit.common.po.Info;
import com.strongit.credit.common.po.Stat;
import com.strongit.credit.common.po.Subject;
import com.strongit.credit.manager.infomanager.dao.InfoDao;
/*
* Create Date:2008-9-22 上午09:54:46
*
* Author:lanjh
*
* Version: V1.0
*
* Description:对进行修改的功能进行描述
*
*
*/
public class InfoDaoImpl extends CreditDAO implements InfoDao {
private Log log;
public InfoDaoImpl() {
log = LogFactory.getLog(InfoDaoImpl. class );
}
public void addip(Stat stat) {
this .getSession().save(stat);
}
public int count(ResultFilter resultfilter) {
// TODO Auto-generated method stub
return getResultCount(com.strongit.credit.common.po.InfoWrapper. class ,
resultfilter);
}
public void delete(Info info) {
// this.getHibernateTemplate().delete(info);
Transaction tx = this .getSession().beginTransaction();
this .getSession().delete(info);
tx.commit();
}
public Info findInfoById(Long long1) {
return loadInfo(com.strongit.credit.common.po.InfoWrapper. class , long1);
}
public Subject findSubjectById(String s) {
return (Subject) this .getSession().get(
com.strongit.credit.common.po.Subject. class , s);
}
public List getInfos(ResultFilter resultfilter) {
return getResults(com.strongit.credit.common.po.InfoWrapper. class ,
resultfilter);
}
public List getStat() {
String hql = "" ;
hql = " select sum(decode(t.comedate,null,0,1)) AS TOTAL, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,-1),'yyyy-mm'),1,0)) AS LASTMONTH, sum(decode(to_char(t.comedate,'yyyy-mm') ,to_char(ADD_MONTHS(SYSDATE,0),'yyyy-mm'),1,0)) AS THISMONTH, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE-1,'yyyy-mm-dd'),1,0)) AS YESTERDAY, sum(decode(to_char(t.comedate,'yyyy-mm-dd') ,to_char(SYSDATE,'yyyy-mm-dd'),1,0)) AS TODAY from STAT t " ;
SQLQuery query = this .getSession().createSQLQuery(hql);
return query.list();
}
public List getSubjects(ResultFilter resultfilter) {
return getResults(com.strongit.credit.common.po.Subject. class ,
resultfilter);
}
public void insert(Info info) {
Session ssesion = this .getSession();
Transaction ts = ssesion.beginTransaction();
info.setContentClob(Hibernate.createClob( " " ));
ssesion.save(info);
ssesion.flush();
ssesion.refresh(info,LockMode.UPGRADE);
SerializableClob clob = (SerializableClob)info.getContentClob();
java.sql.Clob wrapClob = clob.getWrappedClob();
CLOB tmpClob = (CLOB)wrapClob;
Writer wt;
try {
wt = tmpClob.getCharacterOutputStream();
String content = info.getContent();
wt.write(content);
wt.close();
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void update(Info info) {
// Session ssesion = this.getSession();
// Transaction ts = ssesion.beginTransaction();
// info = (Info)ssesion.load(Info.class,info.getId(),LockMode.UPGRADE);
//
// info.setContent(info.getContent());
// info.setContentClob(Hibernate.createClob(" "));
// ssesion.update(info);
// ssesion.flush();
// ssesion.refresh(info,LockMode.UPGRADE);
// SerializableClob clob=(SerializableClob)info.getContentClob();
// java.sql.Clob wrapClob = clob.getWrappedClob();
// CLOB tmpClob=(CLOB)wrapClob;
// Writer wt;
// try {
// wt = tmpClob.setCharacterStream(0);
// String content = info.getContent();
// wt.write(content);
// wt.close();
// ts.commit();
// } catch (Exception e) {
// e.printStackTrace();
// }
this .delete(info);
this .insert(info);
}
public void saveOrUpdate(Info info)
{
Session s;
BufferedWriter writer;
s = this .getHibernateTemplate().getSessionFactory().getCurrentSession();
writer = null ;
try
{
Class clazz = Info. class ;
Method m = null ;
String clobValue = null ;
clobValue = info.getContent();
m = clazz.getDeclaredMethod( " setContentClob " , new Class[] {
java.sql.Clob. class
});
m.setAccessible( true );
if (clobValue == null || clobValue.length() == 0 )
m.invoke(info, new Object[] {
Hibernate.createClob( "" )
});
else
m.invoke(info, new Object[] {
Hibernate.createClob( " " )
});
s.saveOrUpdate(info);
if (clobValue != null && clobValue.length() > 0 )
{
s.flush();
s.refresh(info, LockMode.UPGRADE);
CLOB clob = null ;
m = clazz.getDeclaredMethod( " getContentClob " , null );
m.setAccessible( true );
Object obj = m.invoke(info, null );
if (obj instanceof SerializableClob)
clob = (CLOB)((SerializableClob)obj).getWrappedClob();
if (clob != null )
{
writer = new BufferedWriter(clob.getCharacterOutputStream());
writer.write(clobValue);
}
}
}
catch (Exception e)
{
StringBuilder sb = new StringBuilder();
sb.append( " \u6301\u4E45\u5316POJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B " ).append(info.getClass().getName()).append( " \u3002 " ).append(e.getMessage());
log.error(sb.toString());
}
if (writer != null )
try
{
writer.close();
}
catch (IOException e)
{
log.error(e.getMessage());
}
return ;
}
public Info loadInfo(Class clazz, Serializable id) {
Object pojo;
Info info;
BufferedReader reader;
pojo = this .getSession().get(clazz, id);
info = (Info) pojo;
if (pojo == null || ! (pojo instanceof Info)) {
this .getSession().refresh(pojo, LockMode.READ);
}
reader = null ;
try {
Class c = com.strongit.credit.common.po.Info. class ;
Method m = c.getDeclaredMethod( " getContentClob " , null );
m.setAccessible( true );
Object result = m.invoke(pojo, null );
if (result != null ) {
reader = new BufferedReader(((Clob) result)
.getCharacterStream());
StringBuilder sb = new StringBuilder();
for (String line = null ; (line = reader.readLine()) != null ;)
sb.append(line);
if (sb.length() > 0 )
info.setContent(sb.toString());
}
} catch (Exception e) {
StringBuilder sb = new StringBuilder();
sb
.append(
" \u52A0\u8F7DPOJO\u65F6\u51FA\u73B0\u9519\u8BEF\uFF0C\u7C7B " )
.append(clazz.getName()).append( " \u3002 " ).append(
e.getMessage());
log.error(sb.toString());
}
if (reader != null )
try {
reader.close();
} catch (IOException e) {
log.error(e.getMessage());
}
return info;
}
}