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,
The main problem is
private byte[] attachment;
.@Log
attribute will beFetchType.LAZY
.Persistence Context
will be clear after theclear()
process ofEntityManager
. That mean all of the managed entities will become detached state. More Information is here.Entity
is detached state, if you access fetching value, you will get the problem as you mention.Short Answer :
After
em.clear()
process, your entity instancesurveys
will be detached. That's why, you cannot retrieve the valueattachment
because ofattachment
is lazy loading(FetchType.LAZY
).Solution : use
FetchType.EAGER
. I think, most of the people don't recommend to use eager loading.