Entity Class:
public class CustomerSurvey implements Serializable { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CUSTOMER_SURVEY_SEQUENCE") @Column(name = "SURVEYID", nullable = false) private String surveyId; @Column(name="ATTACHMENT") @Lob private byte[] attachment; ....
Stability Class / Logic:
public List<CustomerSurvey> getSurveysByCustomer(String custNo) throws WorkServiceException { EntityManager em = entityManagerFactory.createEntityManager(); Query query = em.createNamedQuery("customerSurvey.findByCustomer") .setParameter("custNo", custNo); logger.debug(query); List<CustomerSurvey> surveys = query.getResultList(); em.clear(); em.close(); return surveys; }
consumer class / logic:
List<CustomerSurvey> reviewSurveys = workService.getSurveysByCustomer("testCNo2"); for(CustomerSurvey rsurvey: reviewSurveys) { Object obj = rsurvey.getAttachment(); byte[] buffer = (byte[]) obj; OutputStream out = new FileOutputStream("C:\\Temp\\TulipsOut.jpg"); out.write(buffer); }
Error:
Called: javax.jdo.JDODetachedFieldAccessException: you just tried to access a field “attachment”, but this field was not disconnected when you detached the object. Either do not access this field, or disconnect it when detaching the object obj ect. at com.ge.dsp.iwork.entity.CustomerSurvey.jdoGetattachment (CustomerSurvey.java) at com.ge.dsp.iwork.entity.CustomerSurvey.getAttachment (CustomerSurvey.java:89) at com.ge.dsp.iwork.test .WorkServiceTest.testSubmitSurveyResponse (WorkServiceTest.java:270) at sun.reflect.NativeMethodAccessorImpl.invoke0 (custom method) at sun.reflect.NativeMethodAccessorImpl.inodokeMethodmelativeMlocomplet.java : 25) in java.lang.reflect.Method.invoke (Method.javaPoint97) on org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod (AbstractAutowireCapableBeanFactory.java.s1581fact org. support.AbstractAutowireCapableBeanFactory.invokeInitMethods (AbstractAutowireCapableBeanFactory.java:1522) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.ini tializeBean (AbstractAutowireCapableBeanFactory.java:1452) ... 14 more
Thanks,
source share