I am using Hibernat 3.3.1, and Hibernate JPA implementation to implemet EJB 3 application. I have a table (APPLICATION), which has a BLOB field (named ART) corresponding to an image file. My entity class is the follwoing
@Entity @Table(name="APPLICATION", schema="TEST_OWNER") public class Application implements Serializable{ ..... @Lob @Basic(fetch=FetchType.LAZY) @Column(name="ART") private byte[] art;
}
and my sessionbean is the following
@Stateless public class ApplicationManagerBean { @PersistenceContext(unitName="MyPU") private EntityManager em; ....... @TransactionAttribute(TransactionAttributeType.REQUIRED) public void createApplication(Application app, RequestContext rc) { byte[] b = convertFileToByteArray(app.getImage()); app.setArt(b); em.persist(app); } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void updateApplication (Application app, RequestContext rc) { byte[] b = convertFileToByteArray(app.getImage()); app.setArt(b); em.merge(app); }
private byte[] convertFileToByteArray(File file) { FileInputStream fis = null; byte[] b = null; try{ fis = new FileInputStream(file); b = new byte[(int) file.length()]; fis.read(b); }catch(IOException e){ log.error("Error in reading the art file, make sure the file exists.",e); }finally{ try{ if(fis !=null) fis.close(); }catch(IOException e){} } return b; } } I could successfully and save or update an APPLICATION record with image uploaded (i.e ART field). The only problem is I cannot lazily load this field. However After I instrumented my Application.class as instructed from hibernate documention
<taskdef name="instrument" classname="org.hibernate.tool.instrument.cglib.InstrumentTask"> <classpath refid="classpath"/> </taskdef> <instrument verbose="false"> <fileset dir="somepath/"> <include name="Application.class"/> </fileset> </instrument>
Now, I can lazily load the art field with existing records. But, when I tried to upload a new image to update or create a new record, it gave the follwoing the exception:
I am wondering why instrumentation can impact data saving. Your help is greatly appreciated.
[4/26/10 11:49:45:746 EDT] 00000020 ExceptionUtil E CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "updateApplication" on bean "BeanId(BessyEAR#BessyEJB.jar#ApplicationManagerBean, null)". Exception data: java.lang.ClassCastException: org.hibernate.intercept.LazyPropertyInitializer$1 incompatible with [B at org.hibernate.type.ByteArrayBlobType.isEqual(ByteArrayBlobType.java:45) at org.hibernate.type.AbstractLobType.isEqual(AbstractLobType.java:26) at org.hibernate.type.ByteArrayBlobType.replace(ByteArrayBlobType.java:188) at org.hibernate.type.TypeFactory.replace(TypeFactory.java:505) at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:392) at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:319) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:167) at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:81) at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:704) at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:688) at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:692) at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235) at com.ibm.ws.jpa.management.JPAExEmInvocation.merge(JPAExEmInvocation.java:335) at com.ibm.ws.jpa.management.JPAEntityManager.merge(JPAEntityManager.java:123) at com.boeing.he.bessy.bean.admin.ApplicationManagerBean.updateApplication(ApplicationManagerBean.java:174)
|