JDODetachedFieldAccessException: you just tried to access the field “attachment”, but this field was not disabled when you detached the object

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; .... 

Stability 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:

Called: javax.jdo.JDODetachedFieldAccessException: you just tried to access a field “attachment”, but this field was not disconnected when you detached the object. Either do not access this field, or disconnect it when detaching the object 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 (custom method) at sun.reflect.NativeMethodAccessorImpl.inodokeMethodmelativeMlocomplet.java : 25) in java.lang.reflect.Method.invoke (Method.javaPoint97) on org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod (AbstractAutowireCapableBeanFactory.java.s1581fact org. support.AbstractAutowireCapableBeanFactory.invokeInitMethods (AbstractAutowireCapableBeanFactory.java:1522) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.ini tializeBean (AbstractAutowireCapableBeanFactory.java:1452) ... 14 more

Thanks,

+4
source share
1 answer

The main problem is private byte[] attachment; .

  • By default, the @Log attribute will default to FetchType.LAZY .
  • Persistence Context will be clear after the clear() EntityManager process. This means that all managed objects will become disconnected. More info here .
  • When the Entity state is in a disconnected state, if you access the selection, you will get this problem as you mention it.

Short answer:

After the em.clear() process, your instance of the surveys object will be disconnected. Therefore, you cannot get the attachment value due to attachment - this is lazy loading ( FetchType.LAZY ).

Solution: use FetchType.EAGER . I think most people do not recommend using active downloads.

  @Column(name="ATTACHMENT") @Basic(fetch = FetchType.EAGER) @Lov private byte[] attachment; 
+7
source

Source: https://habr.com/ru/post/1441656/


All Articles