Question

I have detached all entities with em.clear() but I do not see the associated object detached? How to detache an associated object?

 I have a method as : 

 public CustomerSurvey getSurveyByRequest(long requestNo)
        throws WorkServiceException {
    logger.debug("Inside getSurveyByRequest : " + requestNo);
    EntityManager em = entityManagerFactory.createEntityManager();
    Query query = em.createNamedQuery("customerSurvey.findByRequest")
            .setParameter("srNo", requestNo);
    List<CustomerSurvey> surveys = query.getResultList();
    **em.clear();**
    em.close();
    return surveys.get(0);
}

CustomerSurvey.java :

@Entity
@Table(name = "CUSTOMER_SURVEY", uniqueConstraints = { @UniqueConstraint(columnNames 
= "SERVEYID") })
@SequenceGenerator(name="CUSTOMER_SURVEY_SEQUENCE", 
sequenceName="CUSTOMER_SURVEY_SEQUENCE", initialValue=1, allocationSize=100)

@NamedQueries({
@NamedQuery(name="customerSurvey.findByRequest",
        query="SELECT survey FROM CustomerSurvey survey " +
                "WHERE survey.serviceRequest.srNo = :srNo")
})

public class CustomerSurvey implements Serializable {

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, 
generator="CUSTOMER_SURVEY_SEQUENCE")
@Column(name = "SURVEYID", nullable = false)
private String surveyId;

     @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.DETACH)
@JoinColumn(name="CUSTNO", referencedColumnName="CustNO")
private Customer customer;

@Column(name="AVGRATINGS")
private int avgRatings;

@Column(name="COMMENTS")
private String comments;

@Column(name="SENTON")
private Date sentOn;

@Column(name="RESPONDEDON")
private Date respondedOn;

@OneToMany(fetch=FetchType.LAZY,mappedBy="customerSurvey")
private Set<SurveyResponse> responses;

@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="SRNO")
private ServiceRequest serviceRequest;

my test class :

CustomerSurvey survey = workService.getSurveyByRequest(request.getSrNo());
        System.out.println("survey = " + survey);
        System.out.println("survey id = " + survey.getSurveyId());
        System.out.println("survey customer = " + survey.getCustomer());

Error Message:

survey = com.ge.dsp.iwork.entity.CustomerSurvey@36b88ea5 survey id = 131 Exception in thread "SpringOsgiExtenderThread-134" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testCloseRequest' defined in URL [bundle://178.124:0/META-INF/spring/module- context.xml]: Invocation of init method failed; nested exception is javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "customer" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:69) at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:355) at org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85) at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320) at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132) at java.lang.Thread.run(Thread.java:662) Caused by: javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "customer" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object. at com.ge.dsp.iwork.entity.CustomerSurvey.jdoGetcustomer(CustomerSurvey.java) at com.ge.dsp.iwork.entity.CustomerSurvey.getCustomer(CustomerSurvey.java:89) at com.ge.dsp.iwork.test.WorkServiceTest.testCloseRequest(WorkServiceTest.java:181) 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

Was it helpful?

Solution

All fields that are currently loaded are detached. Your field is not eager loaded and you don't appear to have accessed it, so it isn't loaded, hence it isn't detached. You can obviously access it, or make it EAGER, or use DataNucleus fetch groups extension to load it. You can also just look at the log to see the detach process

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top