JDODetachedFieldAccessException: You have just attempted to access field “attachment” yet this field was not detached when you detached the object

StackOverflow https://stackoverflow.com/questions/13039252

  •  14-07-2021
  •  | 
  •  

Question

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,

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top