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;
....
Persistence 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 I get as:
Caused by: javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "attachment" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the 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(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) ... 14 more
Thanks,