一、区别与定义
LONG: 可变长的字符串数据,最长2G,LONG具有VARCHAR2列的特性,可以存储长文本一个表中最多一个LONG列
LONG RAW: 可变长二进制数据,最长2G
CLOB: 字符大对象Clob 用来存储单字节的字符数据
NCLOB: 用来存储多字节的字符数据
BLOB: 用于存储二进制数据
BFILE: 存储在文件中的二进制数据,这个文件中的数据只能被只读访。但该文件不包含在数据库内。
bfile字段实际的文件存储在文件系统中,字段中存储的是文件定位指针.bfile对oracle来说是只读的,也不参与事务性控制和数据恢复。
二、clob、blob实现方式
1、
Clob字段 - 注解方式的实现
2、
Clob字段 - 映射文件的实现
3、
Blob字段 - 注解方式的实现
4、
Blob字段 - 映射文件的实现
1、
Clob字段 - 注解方式的实现
package michael.hibernate.bigstring.oracle;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_BIG_STR_CLOB")
public class TbBigStrClob {
private Integer id;
private String name;
private String content;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
/**
* @return the name
*/
@Column(name = "NAME", length = 20)
public String getName() {
return name;
}
/**
* @return the content
*/
@Lob
//@Type(type = "text")
@Column(name = "CONTENT", columnDefinition = "CLOB")
public String getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(String pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TbBigStrClob :: id = [" + id + "],name = [" + name
+ "],content = [" + content + "].";
}
}
Hibernate 配置文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:@192.168.8.49:1521:ora9i
</property>
<property name="connection.username">mytest</property>
<property name="connection.password">123456</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.SetBigStringTryClob">true</property>
<property name="connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">10</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">update</property>
<!-- 注释 -->
<mapping
class="michael.hibernate.bigstring.oracle.TbBigStrClob" />
</session-factory>
</hibernate-configuration>
测试程序:
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestTbBigStr {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 测试Hibernate 注释方法 操作 大字段 Clob
TestTbBigStr.testClob();
}
/**
* 测试Hibernate 注释方法 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testClob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from TbBigStrClob t ";
System.out.println("start test clob ...");
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<TbBigStrClob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init clob data");
List<TbBigStrClob> initlist = initClobData(5);
for (TbBigStrClob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("query data list size = " + list.size());
for (TbBigStrClob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<TbBigStrClob> initClobData(int dataSize) {
List<TbBigStrClob> list = new ArrayList<TbBigStrClob>();
for (int i = 1; i <= dataSize; i++) {
TbBigStrClob vo = new TbBigStrClob();
vo.setName("test_" + i);
vo.setContent("我是大字段<Clob>,at date:" + new Date());
list.add(vo);
}
return list;
}
}
提示:
1. 在Oracle10g + 10g驱动(或者Oracle11g + 11g驱动)的环境下,只需要在Clob字段上,增加注解@Column(name = "CONTENT", columnDefinition = "CLOB"),即可成功实现建表、Clob字段的读写操作。
2. 在Oracle9i+9i的驱动环境下,仅增加注释@Column(name = "CONTENT", columnDefinition = "CLOB")时,建表、写人数据均能正常,但读取时会显示数据为空。如果在Oracle9i下又必须用9i的驱动,可以在Clob字段中再增加一行注解@Lob 或者 @Type(type = "text")。([color=blue]运用spring中org.springframework.jdbc.support.lob.OracleLobHandler,这个没有具体实践过,就不在本文展开了 )
3. 在Oracle9i+10g的驱动环境下,增加注解@Column(name = "CONTENT", columnDefinition = "CLOB"),同样可以成功实现建表、Clob字段的读写操作,可见9i的驱动对Clob支持不是很好,但10g、11g已经做了更新。[/color]
2、
Clob字段 - 映射文件的实现
Bean文件:
package michael.hibernate.bigstring.oracle;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class BigStrClob {
private Integer id;
private String name;
private String content;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(String pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "BigStrClob :: id = [" + id + "],name = [" + name
+ "],content = [" + content + "].";
}
}
映射文件:tb_bigstr.hb.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.hibernate.bigstring.oracle">
<class name="BigStrClob" table="MY_HB_BIG_STR_CLOB">
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string"
not-null="false" length="20" />
<property name="content" column="CONTENT" type="text"
not-null="false" />
</class>
</hibernate-mapping>
在hibernate.cfg.xml文件中增加域对象的映射文件:
<mapping resource="michael/hibernate/bigstring/oracle/tb_bigstr.hb.xml" />
测试程序:
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestHbBigStr {
/**
* @param args
*/
public static void main(String[] args) {
// 测试Hibernate 映射文件 操作 大字段 Clob
TestHbBigStr.testClob();
}
/**
* 测试Hibernate 映射文件 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testClob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from BigStrClob t ";
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<BigStrClob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init clob data");
List<BigStrClob> initlist = initClobData(5);
for (BigStrClob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("Query data list size = " + list.size());
for (BigStrClob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<BigStrClob> initClobData(int dataSize) {
List<BigStrClob> list = new ArrayList<BigStrClob>();
for (int i = 1; i <= dataSize; i++) {
BigStrClob vo = new BigStrClob();
vo.setName("test_" + i);
vo.setContent("我是大字段<Clob>,at date:" + new Date());
list.add(vo);
}
return list;
}
}
提示:
如果是映射文件的,只需要把Clob字段的类型指定为text即:type="text"。
3、Blob字段 - 注解 方式的实现
package michael.hibernate.bigstring.oracle;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
@Entity
@Table(name = "MY_TB_BIG_STR_BLOB")
public class TbBigStrBlob {
private Integer id;
private String name;
private byte[] content;
/**
* @return the id
*/
@Id
@GeneratedValue
@Column(name = "ID")
public Integer getId() {
return id;
}
/**
* @return the name
*/
@Column(name = "NAME", length = 20)
public String getName() {
return name;
}
/**
* @return the content
*/
@Lob
@Column(name = "CONTENT", columnDefinition = "BLOB")
public byte[] getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(byte[] pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TbBigStrBlob :: id = [" + id + "],name = [" + name
+ "],content = [" + new String(content) + "].";
}
}
在hibernate.cfg.xml中增加域对象:
<mapping class="michael.hibernate.bigstring.oracle.TbBigStrBlob" />
运行测试程序:
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestTbBigStr {
/**
* @param args
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) {
// 测试Hibernate 注释方法 操作 大字段 Blob
TestTbBigStr.testBlob();
}
/**
* 测试Hibernate 注释方法 操作 大字段 Blob
*/
@SuppressWarnings("unchecked")
public static void testBlob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from TbBigStrBlob t ";
System.out.println("start test clob ...");
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<TbBigStrBlob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init blob data");
List<TbBigStrBlob> initlist = initBlobData(5);
for (TbBigStrBlob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("query data list size = " + list.size());
for (TbBigStrBlob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<TbBigStrBlob> initBlobData(int dataSize) {
List<TbBigStrBlob> list = new ArrayList<TbBigStrBlob>();
for (int i = 1; i <= dataSize; i++) {
TbBigStrBlob vo = new TbBigStrBlob();
vo.setName("test_" + i);
vo.setContent(("我是大字段<Blob>,at date:" + new Date()).getBytes());
list.add(vo);
}
return list;
}
}
提示:
对于byte[] 类型的字段如果要映射成Blob字段,只需要在该字段增加如下注解即可:
@Lob
@Column(name = "CONTENT", columnDefinition = "BLOB")
4、Blob字段 - 映射文件的实现
bean文件
package michael.hibernate.bigstring.oracle;
/**
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class BigStrBlob {
private Integer id;
private String name;
private byte[] content;
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the content
*/
public byte[] getContent() {
return content;
}
/**
* @param pId the id to set
*/
public void setId(Integer pId) {
id = pId;
}
/**
* @param pName the name to set
*/
public void setName(String pName) {
name = pName;
}
/**
* @param pContent the content to set
*/
public void setContent(byte[] pContent) {
content = pContent;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "BigStrBlob :: id = [" + id + "],name = [" + name
+ "],content = [" + new String(content) + "].";
}
}
映射文件:tb_bigstr.hb.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="michael.hibernate.bigstring.oracle">
<class name="BigStrBlob" table="MY_HB_BIG_STR_BLOB">
<id name="id" type="int" unsaved-value="null">
<generator class="increment" />
</id>
<property name="name" column="NAME" type="string"
not-null="false" length="20" />
<property name="content" column="CONTENT" type="binary"
not-null="false" />
</class>
</hibernate-mapping>
测试程序:
package michael.hibernate.bigstring.oracle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
* 大字段Clob测试
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class TestHbBigStr {
/**
* @param args
*/
public static void main(String[] args) {
// 测试Hibernate 映射文件 操作 大字段 Blob
TestHbBigStr.testBlob();
}
/**
* 测试Hibernate 映射文件 操作 大字段 Clob
*/
@SuppressWarnings("unchecked")
public static void testBlob() {
SessionFactory sessionFactory = null;
Transaction ta = null;
try {
System.out.println("../../hibernate.cfg.xml configure");
Configuration config = new AnnotationConfiguration()
.configure("michael/hibernate/bigstring/oracle/hibernate.cfg.xml");
sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
String hsql = "select t from BigStrBlob t ";
ta = session.beginTransaction();
org.hibernate.Query query = session.createQuery(hsql);
List<BigStrBlob> list = query.list();
if (list.size() == 0) {
System.out.println("start to init blob data");
List<BigStrBlob> initlist = initBlobData(5);
for (BigStrBlob po : initlist) {
session.save(po);
}
list = query.list();
}
System.out.println("Query data list size = " + list.size());
for (BigStrBlob vo : list) {
System.out.println(vo);
}
ta.commit();
} catch (Exception e) {
e.printStackTrace();
ta.rollback();
} finally {
if (null != sessionFactory) {
sessionFactory.close();
}
}
}
/**
*
* @param dataSize
* @return List
*/
public static List<BigStrBlob> initBlobData(int dataSize) {
List<BigStrBlob> list = new ArrayList<BigStrBlob>();
for (int i = 1; i <= dataSize; i++) {
BigStrBlob vo = new BigStrBlob();
vo.setName("test_" + i);
vo.setContent(("我是大字段<Blob>,at date:" + new Date()).getBytes());
list.add(vo);
}
return list;
}
}
提示
对于是映射文件的方式,Blob字段的类型指定为binary即:type=" binary "。
本文转自:http://sjsky.iteye.com/blog/1154916