EJB3操作Oracle的CLOB/BLOB

本人有生以以来第一个blog文章,居然还是转贴,哈。

ava.sql.Blob and Clob support

The EJB 3.0 specification has support for Blob and Clob types. The specification allows you to map the following types to an entity property:
  • java.sql.Blob
  • java.sql.Clob
  • any Serializable Object
  • byte[], Byte[]
  • char[], String, Character[] 

To use this feature just need to use the @javax.persistence.Lob annotation. The Lob annotation is an encapsulation of what type of lob you want. Below is an example of defining fields in an entity that are blobs or clobs.

@Entity
public class BlobEntity implements Serializable
{
private long id;
private Blob blobby;
private Clob clobby;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

@Lob @Basic(fetch = FetchType.EAGER)
public Blob getBlobby()
{
return blobby;
}

public void setBlobby(Blob blobby)
{
this.blobby = blobby;
}

@Lob @Basic(fetch = FetchType.EAGER)
public Clob getClobby()
{
return clobby;
}

public void setClobby(Clob clobby)
{
this.clobby = clobby;
}


}

Working with Blobs and Clobs
Open up LobTesterBean and look for the create() method. JBoss EJB3 is built on top of the Hibernate persistence engine. Hibernate has some helper methods for creating blobs and clobs that LobTesterBean uses.

Blob Creation

org.hibernate.Hibernate.createBlob(byte[] bytes)
 
org.hibernate.Hibernate.createBlob(InputStream stream, int length)
 
org.hibernate.Hibernate.createBlob(InputStream stream)
 

Clob Creation

org.hibernate.Hibernate.createClob(String string)
 
org.hibernate.Hibernate.createClob(Reader reader, int length)
 

Blobs and clobs must only be accessed within a transaction. Blobs and clobs are also not serializable or detachable.

Mapping Strings/
byte[]
to Clob/Blob This is pretty easy, just look at BlobEntity2.java

@Entity
public class BlobEntity2 implements Serializable
{
private long id;
private byte[] blobby;
private String clobby;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

@Lob @Basic(fetch = FetchType.EAGER)
public byte[] getBlobby()
{
return blobby;
}

public void setBlobby(byte[] blobby)
{
this.blobby = blobby;
}

@Lob @Basic(fetch = FetchType.EAGER)
public String getClobby()
{
return clobby;
}

public void setClobby(String clobby)
{
this.clobby = clobby;
}


}

Building and Running
To build and run the example, make sure you have ejb3.deployer installed in JBoss 4.0.x and have JBoss running. See the reference manual on how to install EJB 3.0.
Unix:    $ export JBOSS_HOME=<where your jboss 4.0 distribution is>
Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is>
$ ant
$ ant run

View the tables and rows
You can view the tables created by JBoss by going to the Hypersonic SQL service, scrolling down to the startDatabaseManager button and clicking it. A Hypersonic SQL window will be minimized, but you can open it up to look at the tables and do queries.

本人简写的例子:
写数据:
entity.setContent(Hibernate.createClob("clob的内容"));
读数据:
 System.out.println(entity.getContent().getSubString(1,(int)bp.getContent().length()));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值