最近,使用Hibernate操作Clob。上网看了不少资料,感觉五花八门,实现起来的方法都各不相同。
有的是Hibernate2.0上的。有的是加入了spring的支持,把clob当成string做处理(的确很好,但是不适合新手)
........
而且,某些代码根本都执行不了~浪费我们的时间,55555555。
于是,法老参考了一些官网的方法加以修改,干脆重新写一个完整元操作版本。
包含:insert,update,delete,select 四大基本方法!
供大家参考!
-------------------------------------------
测试环境介绍:
WINDWOS XP SP2;Eclipse 3.2;JDK 1.4.2
Hibernate-Version: 3.0.5 ; oracle 9i ;
=====================
重点说明:
1。配置文件hbm.xml里把clob的type="clob"
片段如下
<property name="bsznContent" type="clob">
<column name="BSZN_CONTENT" not-null="true" />
</property>
2。实体bean中,导入 java.sql.Clob包 (注意不是 oracle.sql.CLOB 个人习惯用血统纯点的.这里鄙视一下oracle。嘿嘿)
在该字段对应的实体文件里面,增加以下两个变量及其相应的get/set方法
import java.sql.Clob;
...
private Clob bsznContent;
private String bsznContentString;
...
public Clob getBsznContent() {
return this.bsznContent;
}
public void setBsznContent(Clob bsznContent) {
this.bsznContent = bsznContent;
}
public String getBsznContentString() {
return bsznContentString;
}
public void setBsznContentString(String bsznContentString) {
this.bsznContentString = bsznContentString;
}
bsznContent 属性是默认的clob, bsznContentString 属性是对 bsznContent做转换时候用的
----------------------------------------
好了废话不多说,把代码写下来吧~
建表SQL
=================
hibernate.cfg.xml 由于调试用,用的是JDBC连接方法,要使用连接池,请自行修改.
记得自己把数据库地址和用户名,秘密改下
cmpBszn.hbm.xml
抽象类AbstractcmpBszn.java
有的是Hibernate2.0上的。有的是加入了spring的支持,把clob当成string做处理(的确很好,但是不适合新手)
........
而且,某些代码根本都执行不了~浪费我们的时间,55555555。
于是,法老参考了一些官网的方法加以修改,干脆重新写一个完整元操作版本。
包含:insert,update,delete,select 四大基本方法!
供大家参考!
-------------------------------------------
测试环境介绍:
WINDWOS XP SP2;Eclipse 3.2;JDK 1.4.2
Hibernate-Version: 3.0.5 ; oracle 9i ;
=====================
重点说明:
1。配置文件hbm.xml里把clob的type="clob"
片段如下
<property name="bsznContent" type="clob">
<column name="BSZN_CONTENT" not-null="true" />
</property>
2。实体bean中,导入 java.sql.Clob包 (注意不是 oracle.sql.CLOB 个人习惯用血统纯点的.这里鄙视一下oracle。嘿嘿)
在该字段对应的实体文件里面,增加以下两个变量及其相应的get/set方法
import java.sql.Clob;
...
private Clob bsznContent;
private String bsznContentString;
...
public Clob getBsznContent() {
return this.bsznContent;
}
public void setBsznContent(Clob bsznContent) {
this.bsznContent = bsznContent;
}
public String getBsznContentString() {
return bsznContentString;
}
public void setBsznContentString(String bsznContentString) {
this.bsznContentString = bsznContentString;
}
bsznContent 属性是默认的clob, bsznContentString 属性是对 bsznContent做转换时候用的
----------------------------------------
好了废话不多说,把代码写下来吧~
建表SQL
=================
/**/
/* Table: "cmp_bszn" 相关SQL */
/**/ /*==============================================================*/
create table "cmp_bszn" (
"id" INTEGER not null ,
"kind" CHAR ( 2 ) not null ,
"bszn_title1" VARCHAR2 ( 200 ) not null ,
"bszn_title2" VARCHAR2 ( 200 ),
"bszn_code" VARCHAR2 ( 50 ),
"bszn_bumen" VARCHAR2 ( 50 ),
"bszn_date" VARCHAR2 ( 50 ),
"bszn_content" CLOB not null ,
"sys_date" DATE default SYSDATE not null ,
constraint PK_CMP_BSZN primary key ("id")
);
/**/ /*==============================================================*/
create table "cmp_bszn" (
"id" INTEGER not null ,
"kind" CHAR ( 2 ) not null ,
"bszn_title1" VARCHAR2 ( 200 ) not null ,
"bszn_title2" VARCHAR2 ( 200 ),
"bszn_code" VARCHAR2 ( 50 ),
"bszn_bumen" VARCHAR2 ( 50 ),
"bszn_date" VARCHAR2 ( 50 ),
"bszn_content" CLOB not null ,
"sys_date" DATE default SYSDATE not null ,
constraint PK_CMP_BSZN primary key ("id")
);
hibernate.cfg.xml 由于调试用,用的是JDBC连接方法,要使用连接池,请自行修改.
记得自己把数据库地址和用户名,秘密改下
<?
xml version='1.0' encoding='UTF-8'
?>
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > sa </ property >
< property name ="connection.url" >
jdbc:oracle:thin:@127.0.0.1:1521:web
</ property >
< property name ="dialect" >
org.hibernate.dialect.Oracle9Dialect
</ property >
< property name ="connection.password" > saweb </ property >
< property name ="connection.driver_class" >
oracle.jdbc.driver.OracleDriver
</ property >
< property name ="show_sql" > true </ property >
< property name ="connection.useUnicode" > true </ property >
< property name ="connection.characterEncoding" > GBK </ property >
<!-- 設定事務管理的工廠類 -->
< property name ="hibernate.transaction.factory_class" >
org.hibernate.transaction.JDBCTransactionFactory
</ property >
< property name ="hibernate.query.factory_class" >
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</ property >
< mapping resource ="clob/cmpBszn.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > sa </ property >
< property name ="connection.url" >
jdbc:oracle:thin:@127.0.0.1:1521:web
</ property >
< property name ="dialect" >
org.hibernate.dialect.Oracle9Dialect
</ property >
< property name ="connection.password" > saweb </ property >
< property name ="connection.driver_class" >
oracle.jdbc.driver.OracleDriver
</ property >
< property name ="show_sql" > true </ property >
< property name ="connection.useUnicode" > true </ property >
< property name ="connection.characterEncoding" > GBK </ property >
<!-- 設定事務管理的工廠類 -->
< property name ="hibernate.transaction.factory_class" >
org.hibernate.transaction.JDBCTransactionFactory
</ property >
< property name ="hibernate.query.factory_class" >
org.hibernate.hql.classic.ClassicQueryTranslatorFactory
</ property >
< mapping resource ="clob/cmpBszn.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
<?
xml version="1.0"
?>
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
< class name ="cmpBszn" table ="CMP_BSZN" schema ="SA" >
< id name ="id" type ="java.lang.Long" >
< column name ="ID" precision ="22" scale ="0" />
< generator class ="increment" ></ generator >
</ id >
< property name ="kind" type ="java.lang.String" >
< column name ="KIND" length ="2" not-null ="true" />
</ property >
< property name ="bsznTitle1" type ="java.lang.String" >
< column name ="BSZN_TITLE1" length ="200" not-null ="true" />
</ property >
< property name ="bsznTitle2" type ="java.lang.String" >
< column name ="BSZN_TITLE2" length ="200" />
</ property >
< property name ="bsznCode" type ="java.lang.String" >
< column name ="BSZN_CODE" length ="50" />
</ property >
< property name ="bsznBumen" type ="java.lang.String" >
< column name ="BSZN_BUMEN" length ="50" />
</ property >
< property name ="bsznDate" type ="java.lang.String" >
< column name ="BSZN_DATE" length ="50" />
</ property >
< property name ="bsznContent" type ="clob" >
< column name ="BSZN_CONTENT" not-null ="true" />
</ property >
< property name ="sysDate" type ="java.util.Date" >
< column name ="SYS_DATE" length ="7" not-null ="true" />
</ property >
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
< hibernate-mapping >
< class name ="cmpBszn" table ="CMP_BSZN" schema ="SA" >
< id name ="id" type ="java.lang.Long" >
< column name ="ID" precision ="22" scale ="0" />
< generator class ="increment" ></ generator >
</ id >
< property name ="kind" type ="java.lang.String" >
< column name ="KIND" length ="2" not-null ="true" />
</ property >
< property name ="bsznTitle1" type ="java.lang.String" >
< column name ="BSZN_TITLE1" length ="200" not-null ="true" />
</ property >
< property name ="bsznTitle2" type ="java.lang.String" >
< column name ="BSZN_TITLE2" length ="200" />
</ property >
< property name ="bsznCode" type ="java.lang.String" >
< column name ="BSZN_CODE" length ="50" />
</ property >
< property name ="bsznBumen" type ="java.lang.String" >
< column name ="BSZN_BUMEN" length ="50" />
</ property >
< property name ="bsznDate" type ="java.lang.String" >
< column name ="BSZN_DATE" length ="50" />
</ property >
< property name ="bsznContent" type ="clob" >
< column name ="BSZN_CONTENT" not-null ="true" />
</ property >
< property name ="sysDate" type ="java.util.Date" >
< column name ="SYS_DATE" length ="7" not-null ="true" />
</ property >
</ class >
</ hibernate-mapping >
//
default package
import java.sql.Clob;
import java.util.Date;
/** */ /**
* AbstractcmpBszn generated by MyEclipse - Hibernate Tools
*/
public abstract class AbstractcmpBszn implements java.io.Serializable ... {
// Fields
private Long id;
private String kind;
private String bsznTitle1;
private String bsznTitle2;
private String bsznCode;
private String bsznBumen;
private String bsznDate;
private Clob bsznContent;
private String bsznContentString;
private Date sysDate;
// Constructors
/** *//** default constructor */
public AbstractcmpBszn() ...{
}
/** *//** minimal constructor */
public AbstractcmpBszn(String kind, String bsznTitle1, Clob bsznContent, Date sysDate) ...{
this.kind = kind;
this.bsznTitle1 = bsznTitle1;
this.bsznContent = bsznContent;
this.sysDate = sysDate;
}
/** *//** full constructor */
public AbstractcmpBszn(String kind, String bsznTitle1, String bsznTitle2, String bsznCode, String bsznBumen, String bsznDate, Clob bsznContent, Date sysDate) ...{
this.kind = kind;
this.bsznTitle1 = bsznTitle1;
this.bsznTitle2 = bsznTitle2;
this.bsznCode = bsznCode;
this.bsznBumen = bsznBumen;
this.bsznDate = bsznDate;
this.bsznContent = bsznContent;
import java.sql.Clob;
import java.util.Date;
/** */ /**
* AbstractcmpBszn generated by MyEclipse - Hibernate Tools
*/
public abstract class AbstractcmpBszn implements java.io.Serializable ... {
// Fields
private Long id;
private String kind;
private String bsznTitle1;
private String bsznTitle2;
private String bsznCode;
private String bsznBumen;
private String bsznDate;
private Clob bsznContent;
private String bsznContentString;
private Date sysDate;
// Constructors
/** *//** default constructor */
public AbstractcmpBszn() ...{
}
/** *//** minimal constructor */
public AbstractcmpBszn(String kind, String bsznTitle1, Clob bsznContent, Date sysDate) ...{
this.kind = kind;
this.bsznTitle1 = bsznTitle1;
this.bsznContent = bsznContent;
this.sysDate = sysDate;
}
/** *//** full constructor */
public AbstractcmpBszn(String kind, String bsznTitle1, String bsznTitle2, String bsznCode, String bsznBumen, String bsznDate, Clob bsznContent, Date sysDate) ...{
this.kind = kind;
this.bsznTitle1 = bsznTitle1;
this.bsznTitle2 = bsznTitle2;
this.bsznCode = bsznCode;
this.bsznBumen = bsznBumen;
this.bsznDate = bsznDate;
this.bsznContent = bsznContent;