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

流过昼夜 提交于 2019-12-13 13:16:12

问题


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,


回答1:


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;


来源:https://stackoverflow.com/questions/13039252/jdodetachedfieldaccessexception-you-have-just-attempted-to-access-field-attach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!