Use BLOB Data Type in CMP object Bean

Source: Internet
Author: User

Topic: Use BLOB Data Type in CMP object Bean
Author: Debu panda
Translation: Cai Yi (Caiyi0903@hotmail.com)
Time: 2005-6-23

In relational databases such as Oracle, clob and blob types are used to store enlarged objects. Bolb indicates a Binary Large Object. This type of data is used to save images, images, videos, and so on. Clob indicates a large character object, which can store a large amount of character-based data.

JDBC defines the clob and blob types in the database corresponding to Java. SQL. clob and Java. SQL. Blob. However, these two types cannot be used in the Entity Bean, because these two classes are not serialized (serializable ). Therefore, we cannot define the java. SQL. clob or Java. SQL. BLOB Data Types in the CMP Entity Bean.

If we want to use the Blob field of the database, we must declare the CMP fieldByte []And map this field to the Blob field of the database. To use the clob field, we need to define the CMP fieldJava. Lang. StringOrChar [].

Example of an entity

In an enterprise, for security considerations, we may need to save employee images to the database table. Here we use employeepicture as an example to represent the image entity of an employee. This employeepicturebean CMP has two attributes.Empno,Picture.PictureThe field is definedByte []Type.

The employeepicture Entity Bean is mapped to the emppic data table,PictureAttributes are mapped to the attributes in the emppic table.PictureThis picture field is defined as blob type.

Below are some core code of employeepicturebean. Java:

public abstract class EmployeePictureBean implements EntityBean{  ....  public abstract byte[] getPicture();  public abstract void setPicture(byte[] newPicture);  public Long ejbCreate(Long empno, byte[] newPicture)  {    setEmpno(empno);    setPicture(newPicture);    return empno;  }  public void ejbPostCreate(Long empno, byte[] newPicture)  {  }  ...}

If we are using Solaris 9.0.4, we need to define the ing of the Entity Bean attribute to the data table field in the orion-ejb-jar.xml. If other J2EE containers are used, you need to define the O-R ing in the deployment descriptor of the relevant vendor. The following shows the oc4j ing code in the graphic studio:

<Entity-deployment name = "employeepicture" data-source = "JDBC/oracleds" table = "emppic">
<Primkey-mapping>
<CMP-field-mapping name = "empno" persistence-name = "empno" persistence-type = "number (8)"/>
</Primkey-mapping>
<CMP-field-mapping name = "empno" persistence-name = "empno" persistence-type = "number (8)"/>
<CMP-field-mapping name = "picture" persistence-name = "picture" persistence-type = "blob"/>
</Entity-deployment>

Client:

The CMP Entity Bean client does not have any special processing. The only thing to note is that bufferedinputstream is used to read the image file when updating the data table, but outputstream is used to retrieve the data table.

The following shows how to read an image file and create a bean instance:

    // Locate and open the file    File imgFile = new File(fileName);    long imgFileSize= imgFile.length();    // initialize the byte array    byte byteValue[] = new byte[(int)imgFileSize];    // Read the file into the byte array    InputStream is = new BufferedInputStream(new FileInputStream(imgFile));    int len = is.read(byteValue);    //Add the byte to the entity bean field    if(len!=imgFileSize)    {           System.out.println("Read bytes did not equal file size on directory");    }    else    {      EmployeePictureLocal employeePicture = empHome.create(empNo , byteValue);
Conclusion

We cannot directly map the java. SQL. Blob type to the Blob field in the database in the CMP Entity Bean. Instead, you must use byte [] to operate the BLOB data type. However, we should note that if the stored data is very large (more than 25 MB), the processing efficiency of this method will be very low. At that time, we can only consider other optional solutions.

The complete sample code can be obtained from the following address: http://www.oracle.com/technology/tech/java/oc4j/904/how_to/how-to-ejb-cmpblob.zip.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.