我已经使用 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。