最近一直在弄这方面的,由于关联到新闻内容字段是Clob类型的,但是Clob字段类型的数据需要特殊处理,因此最近弄了几个晚上(不过是每个晚上弄两三个小时),下面我将成功的代码贴出:
1.pojo类:NewsCnt
public class NewsCnt implements java.io.Serializable {
private Integer id;
private String cntTitle;
// private Clob cntContent;
private String cntContent;
private Date cntIssdate;
private String cntUrl;
private NewsEmp emp;
private NewsModel model;
public NewsModel getModel() {
return model;
}
public void setModel(NewsModel model) {
this.model = model;
}
// public Clob getCntContent() {
// return cntContent;
// }
//
// public void setCntContent(Clob cntContent) {
// this.cntContent = cntContent;
// }
public String getCntContent() {
return cntContent;
}
public void setCntContent(String cntContent) {
this.cntContent = cntContent;
}
public Date getCntIssdate() {
return cntIssdate;
}
public void setCntIssdate(Date cntIssdate) {
this.cntIssdate = cntIssdate;
}
public String getCntTitle() {
return cntTitle;
}
public void setCntTitle(String cntTitle) {
this.cntTitle = cntTitle;
}
public String getCntUrl() {
return cntUrl;
}
public void setCntUrl(String cntUrl) {
this.cntUrl = cntUrl;
}
public NewsEmp getEmp() {
return emp;
}
public void setEmp(NewsEmp emp) {
this.emp = emp;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((id == null) ? 0 : id.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final NewsCnt other = (NewsCnt) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
2.pojo对应的hibernate映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="news.uu.pojo">
<class name="NewsCnt" table="NEWS_CNT" lazy="false">
<id name="id" column="ID" type="int">
<generator class="increment"></generator>
</id>
<property name="cntTitle" column="CNT_TITLE" type="string"></property>
<property name="cntContent" column="CNT_CONTENT"
type="org.springframework.orm.hibernate.support.ClobStringType">
</property>
<property name="cntIssdate" column="CNT_ISSDATE" type="date"></property>
<property name="cntUrl" column="CNT_URL" type="string"></property>
<many-to-one name="emp" column="FK_EMP" class="NewsEmp"
cascade="save-update" outer-join="auto">
</many-to-one>
<many-to-one name="model" column="FK_MODEL" class="NewsModel"
cascade="save-update" outer-join="auto">
</many-to-one>
</class>
</hibernate-mapping>
注意到cntContent在pojo类中的类型为String型,对应的映射类型为:org.springframework.orm.hibernate.support.ClobStringType,
这样就可以通过getHibernateTemplate().save(obj)的方法直接保存对象,而不用对clob字段实施特殊处理;
3.同时需要在spring配置文件中配置如下信息:
<!-- 数据源的配置 -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/news</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
t3://localhost:7001
</prop>
<prop key="java.naming.security.principal">
weblogic
</prop>
<prop key="java.naming.security.credentials">
weblogic
</prop>
</props>
</property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="lobHandler">
<ref bean="oracleLobHandler" />
</property>
<property name="mappingResources">
<list>
<value>news/uu/pojo/NewsCnt.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置nativeJdbcExtrator -->
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true">
</bean>
<!-- 配置oracleLobHandler -->
<bean id="oracleLobHandler"
class="org.springframework.jdbc.support.lob.OracleLobHandler"
lazy-init="true">
<property name="nativeJdbcExtractor">
<ref bean="nativeJdbcExtractor" />
</property>
</bean>
<!-- 事物配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- 事物拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">
PROPAGATION_REQUIRED,readOnly,-Exception
</prop>
<prop key="set*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="update*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="search*">
PROPAGATION_REQUIRED,readOnly,-Exception
</prop>
</props>
</property>
</bean>
<!-- 配置动态代理 -->
<bean id="proxy"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
<value>*DaoImpl</value>
<value>*Service</value>
<value>*ServiceImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
4.相应Dao中的操作
public class NewsListDaoImpl extends HibernateDaoSupport implements NewsListDao {
private Log log = LogFactory.getLog(this.getClass());
private SessionFactory sessionFactory;
public boolean addNews() throws Exception {
boolean flag = false;
NewsCnt cnt = new NewsCnt();
cnt.setCntTitle("clob字段数据的插入");
cnt.setCntContent("今天晚上大家都几乎通宵了,不知道对大家来说值不值得"
+ ",但至少对于我来说是值得的,因为今天我們的工作是起草企業策劃書,"
+ "而且我从小的人生目标是创办自己企业,因此我覺得非常值得");
cnt.setCntIssdate(new Date());
cnt.setEmp(this.getEmpById("cmm"));
cnt.setModel(this.getModelById("uu_model_001"));
try {
this.getHibernateTemplate().save(cnt);
log.debug("clob字段数据插入成功");
flag = true;
} catch (Exception e) {
log.debug("clob字段插入时出现异常:" + e.getMessage());
flag = false;
throw new Exception(e.getMessage());
}
return flag;
}
public NewsCnt searchNews(String id) throws Exception{
NewsCnt cnt = null;
String hql = "select c from NewsCnt c where c.id=?";
Object[] values = { id };
try {
List lst = this.getHibernateTemplate().find(hql, values);
if (lst.size() < 1) {
log.debug("所查询新闻不存在,数据源出现问题");
throw new DivantBusinessException("所查询新闻不存在,数据源出现问题");
}
cnt = (NewsCnt) lst.get(0);
log.debug("新闻信息查询成功");
} catch (Exception e) {
log.debug("新闻信息查询时出现异常:" + e.getMessage());
throw new Exception(e.getMessage());
}
return cnt;
}
public NewsEmp getEmpById(String empId) throws Exception {
NewsEmp emp = null;
String hql = "select e from NewsEmp e where e.empId=?";
Object[] values = { empId };
try {
List lst = this.getHibernateTemplate().find(hql, values);
if (lst.size() < 1) {
log.debug("所查询员工不存在,数据源出现问题");
throw new DivantBusinessException("所查询员工不存在,数据源出现问题");
}
emp = (NewsEmp) lst.get(0);
log.debug("员工信息查询成功");
} catch (Exception e) {
log.debug("员工信息查询时出现异常:" + e.getMessage());
throw new Exception(e.getMessage());
}
return emp;
}
public NewsModel getModelById(String modelId) throws Exception {
NewsModel model = null;
String hql = "select m from NewsModel m where m.modelId=?";
Object[] values = { modelId };
try {
List lst = this.getHibernateTemplate().find(hql, values);
if (lst.size() < 1) {
log.debug("所查询员工不存在,数据源出现问题");
throw new DivantBusinessException("所查询员工不存在,数据源出现问题");
}
model = (NewsModel) lst.get(0);
log.debug("员工信息查询成功");
} catch (Exception e) {
log.debug("员工信息查询时出现异常:" + e.getMessage());
throw new Exception(e.getMessage());
}
return model;
}
}