2

我已经使用 em.clear() 分离了所有实体,但我没有看到关联的对象被分离?如何分离关联的对象?

 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);
}

客户调查.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;

我的测试课:

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());

错误信息:

调查 = com.ge.dsp.iwork.entity.CustomerSurvey@36b88ea5 调查 id = 131 线程“SpringOsgiExtenderThread-134”中的异常 org.springframework.beans.factory.BeanCreationException:创建 URL [bundle 中定义的名称为“testCloseRequest”的 bean 时出错://178.124:0/META-INF/spring/module-context.xml]:init 方法调用失败;嵌套异常是 javax.jdo.JDODetachedFieldAccessException:您刚刚尝试访问字段“客户”,但在分离对象时该字段未分离。要么不要访问此字段,要么在分离对象时将其分离。 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)原因:javax.jdo.JDODetachedFieldAccessException:您刚刚尝试访问字段“客户”,但在您分离对象时该字段未分离。要么不要访问此字段,要么在分离对象时将其分离。在 com.ge.dsp.iwork.entity.CustomerSurvey.jdoGetcustomer(CustomerSurvey.java) 在 com.ge.dsp.iwork.entity.CustomerSurvey.getCustomer(CustomerSurvey.java:89) 在 com.ge.dsp.iwork.test .WorkServiceTest。

4

1 回答 1

2

当前加载的所有字段都被分离。您的字段未急切加载,并且您似乎没有访问过它,因此未加载它,因此未分离。您显然可以访问它,或将其设为 EAGER,或使用 DataNucleus 提取组扩展来加载它。您也可以只查看日志以查看分离过程

于 2012-10-10T07:16:22.320 回答