CustomerSurvey.java
public class CustomerSurvey
implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_SURVEY_SEQUENCE") @Column(name = "SURVEYID", nullable = false) private String surveyId;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "CUSTNO", referencedColumnName = "CustNO") private Customer customer;
@Column(name = "AVGRATINGS") private double avgRatings;
@Column(name = "COMMENTS") private String comments;
@Column(name = "SENTON") private Date sentOn;
@Column(name = "RESPONDEDON") private Date respondedOn;
@Column(name = "RESPONDED") int responded;
@Basic(fetch = FetchType.EAGER) @OneToMany(mappedBy = "customerSurvey", cascade = CascadeType.ALL) private Set<SurveyResponse> responses;
@XmlTransient @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH) @JoinColumn(name = "SRNO") private ServiceRequest serviceRequest;
@Column(name = "ATTACHMENT") @Lob @Basic(fetch = FetchType.EAGER) private byte[] attachment;
--- getters and setters---
public int hashCode()
{
return new HashCodeBuilder(17,31)
.append(this.surveyId)
// .append(this.avgRatings)
// .append(this.getResponded())
.toHashCode();
}
@Override
public boolean equals(Object obj)
{
return new EqualsBuilder().
append("SurveyId",this.surveyId)
// .append("responded", this.getResponded())
// .append("AvgRatings", this.getAvgRatings())
.isEquals();
}
持久性代码: public void submitSurveyResponse(列表调查) 抛出WorkServiceException {
if ( this.entityManagerFactory == null )
{
this.entityManagerFactory = this.getEntityManagerFactory();
}
EntityManager em = this.entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
for (CustomerSurvey survey : surveys)
{
tx.begin();
survey.setRespondedOn(Calendar.getInstance().getTime());
survey.setResponded(BooleanResponse.YES.getCode());
survey.setCustomer(em.find(Customer.class, survey.getCustomer().getCustNo().trim()));
survey.setComments("Survey Response submitted");
for (SurveyResponse response : survey.getResponses())
{
response.setQuestionair(em.find(Questionair.class, response.getQuestionair().getqNo()));
}
CustomerSurvey cs = em.find(CustomerSurvey.class, survey.getSurveyId());
System.out.println(survey.equals(cs));
Query query1 = em.createQuery("SELECT from CustomerSurvey customerSurvey where customerSurvey.surveyId = :id")
.setParameter("id", survey.getSurveyId());
CustomerSurvey dbSurvey = (CustomerSurvey) query1.getResultList().get(0);
System.out.println(":" + survey.equals(dbSurvey));
if(survey.getServiceRequest() == null) {
cs = em.find(CustomerSurvey.class, survey.getSurveyId());
survey.setServiceRequest(cs.getServiceRequest());
}
CustomerSurvey mSurvey = em.merge(survey);
String surveyId = mSurvey.getSurveyId();
Query query = em.createNamedQuery("surveyResponse.getAverageRatings");
logger.trace(query.toString());
query.setParameter("surveyId", surveyId);
logger.trace(query.toString());
Double avgRating = (Double) query.getSingleResult();
if ( avgRating == null )
{
avgRating = new Double(0);
}
BigDecimal bd = BigDecimal.valueOf(avgRating);
bd = bd.setScale(2, RoundingMode.HALF_UP);
avgRating = bd.doubleValue();
mSurvey.setAvgRatings(avgRating);
em.flush();
tx.commit();
}
em.clear();
em.close();
}
它不会更新,而是插入新行。 在使用hashcode构建器和equals构建器后,我得到以下检查的错误: System.out.println的测试(“:”+ survey.equals(dbSurvey));假的 System.out.println的测试(survey.equals(cs)); 上面给出了错误。
请咨询
答案 0 :(得分:1)
“transient”和“detached”是不同的状态,并且根据JPA规范在 merge()中会有不同的行为。 transient将创建一个新对象。与更新现有的分离。在修改之前获取分离的对象,或者阅读http://www.datanucleus.org/products/accessplatform_3_2/jpa/object_lifecycle.html上的“分离”部分