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)
{
}
...
}
如果我们使用OC4J 9.0.4 ,就需要在orion-ejb-jar.xml中定义实体BEAN属性到数据表字段的映射。如果使用了其他J2EE容器,就需要在相关厂商的部署描述符中定义O-R映射。下面演示在OC4J中的映射代码:
<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>
客户端:
CMP实体BEAN的客户端没有什么特别的处理。唯一需要注意的是在update数据表时要使用BufferedInputStream 来读取IMAGE文件,但在检索数据表时使用OutputStream 。
下面演示如何读取一个IMAGE文件,并创建一个BEAN实例:
// 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);