5

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,

beetri
  • 1,039
  • 11
  • 24
  • 40

1 Answers1

7

The main problem is private byte[] attachment;.

  1. The default loading of @Log attribute will be FetchType.LAZY.
  2. The Persistence Context will be clear after the clear() process of EntityManager. That mean all of the managed entities will become detached state. More Information is here.
  3. When an 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 instance surveys will be detached. That's why, you cannot retrieve the value attachment because of attachment is lazy loading(FetchType.LAZY).

Solution : use FetchType.EAGER. I think, most of the people don't recommend to use eager loading.

    @Column(name="ATTACHMENT")
    @Basic(fetch = FetchType.EAGER)
    @Lov
    private byte[] attachment;
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • thank you that works. I m new to JPA. When we say eager loading is not recommend, then what is the alternate approach in such case? how do I get/load those information when needed? – beetri Oct 23 '12 at 21:52
  • @beetri, I am sorry for "I think, most of the people don't recommend to use eager loading." It can be my lack of experience. The main is, if you would like to get `CustomerSurvey` together with `attachment`, use `EAGER`. Otherwise, use `LAZY`. Reference : http://www.melihsakarya.com/2011/12/jpa-fetch-stratejileri-ve-lazy-loading/ – Zaw Than oo Oct 24 '12 at 03:28