JBoss.org Community Documentation

Chapter 4. Introduction to Blob and Clob support in EJB3

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 org.jboss.tutorial.blob.bean.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.