0

客户调查.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 builder 和 equals builder 之后,我得到了错误的以下检查: System.out.println(":" +survey.equals(dbSurvey)); 的测试 给出 false System.out.println(survey.equals(cs)) 的测试;上面给出了错误。

请指教

4

1 回答 1

1

根据 JPA 规范, “瞬态”和“分离”是不同的状态,并且在merge()中会有不同的行为。瞬态将创建一个新对象。与更新现有的分离。在修改之前获取分离的对象,或阅读http://www.datanucleus.org/products/accessplatform_3_2/jpa/object_lifecycle.html上的“分离”部分

于 2012-12-15T07:27:14.040 回答