Using a BLOB data type in a CMP Enity Bean

by Debu Panda

I got an email from a customer who wanted to use BLOB data type in their CMP entity bean. In this blog, I will provide an example and will discuss how can you develop your CMP EJBs to use a BLOB database type.

I got an email from a customer who wanted to use BLOB data type in their CMP entity bean. They store images in BLOB data types in Oracle database and want to map their CMP entity bean field to a BLOB data type. Manipulation of CLOB and BLOB from J2EE applications is a challenging task. However doing this from a CMP Entity Bean hides a lot of complexities of JDBC. In this blog, I will provide an example and will discuss how can you develop your CMP EJBs to use a BLOB database type.

Download this free guide

Download now: Java EE moves to the Eclipse Foundation

What are application developers and market analysts saying about Oracles decision to move Java EE to the Eclipse Foundation? What will this change? Find out here.

By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

In relational databases like Oracle, CLOB and BLOB column types are used to store large objects. BLOB stands for Binary Large Objects and this data type is used to store images, pictures, videos, etc. CLOB stands for Character Large Objects and can store large volumes of character-based data.

JDBC defines Java types java.sql.Clob and java.sql.Blob as the corresponding types for manipulating CLOB and BLOB types in the database. However these types cannot be used in Entity Beans because they are not Serializable. Hence you cannot define a field of type java.sql.Clob or java.sql.Blob in the CMP entity bean.

If you want to use a BLOB column in the database you have to declare a CMP field of byte[] in your entity bean and map this field to the database column of type BLOB. For using CLOB column types in the database you have to use either types java.lang.String or char[] for the field types in the CMP entity bean.

An Entity bean example

In a company you may want to store pictures of your employees in database table for security purposes. Let us use an EmployeePicture entity bean as an example that manipulates pictures for employees. The EmployeePictureBean has two container managed fields empno, picture. The picture field is defined of type byte[].

The EmployeePicture entity bean is mapped to EMPPIC database table, the picture field is mapped to picture column of EMPPIC table and the picture column is defined to be of type BLOB.

Let us assume that we are using OC4J 9.0.4 . We need to define the mapping of entity bean fields to tables and columns in the orion-ejb-jar.xml. If you are using any other J2EE container you need to define the O-R mapping in the respective vendor deployment descriptor. This shows the picture field in the EmployeePicture bean to be mapped to a BLOB column in OC4J:

The Client:

There is nothing special in the client for CMP entity bean that manipulates a BLOB column. The only difference is reading the IMAGE FILEs using BufferedInputStream during update to the database and using OutputStream while retrieving from the database.

Conclusion

You cannot directly map a java.sql.Blob field type in CMP entity bean to BLOB data type in database and use byte[] to manipulate BLOB data type. You have to remember that this is not an efficient approach if you are storing data that is very large (more than 25M) and probably consider alternatives.

About the author

I'm Debu Panda (debabrata.panda@oracle.com) and I'm the Product Manager responsible for EJB Container in the Oracle Application Server development team. I've around 13 years of experience in IT mainly in the Database and Application Server technologies. I've authored articles in several journals and presented in several conferences.

0 comments

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy