用Spring HibernateTemplate

时间:2017-02-09 15:24:52

标签: java spring hibernate

public class AfpProcessSummaryDetail implements Serializable {

private String srNo;
private String fileName;
private String status;
private String location;
private String comments;
private Character convertStatus;
private AfpProcessDetail afpProcessDetail;

public AfpProcessSummaryDetail() {
}

public AfpProcessSummaryDetail(String srNo, String fileName, String status, String location, String comments,
        AfpProcessDetail afpProcessDetail) {
    this.srNo = srNo;
    this.fileName = fileName;
    this.status = status;
    this.location = location;
    this.comments = comments;
    this.afpProcessDetail = afpProcessDetail;
}

@ManyToOne
@JoinColumn(name = "PROCESSDETAIL")
public AfpProcessDetail getAfpProcessDetail() {
    return afpProcessDetail;
}

AfpProcessDetail

public class AfpProcessDetail implements Serializable {

private String processID;
private String processDate;
private Integer fileCount;
private Integer successCount;
private Integer failureCount;
private Character active;
private Set<AfpProcessSummaryDetail> processSummaryDetails = new HashSet<AfpProcessSummaryDetail>(0);

public AfpProcessDetail() {
}

public AfpProcessDetail(String processID, String processDate, Integer fileCount, Integer successCount,
        Integer failureCount) {
    this.processID = processID;
    this.processDate = processDate;
    this.fileCount = fileCount;
    this.successCount = successCount;
    this.failureCount = failureCount;
}

public AfpProcessDetail(String processID, String processDate, Integer fileCount, Integer successCount,
        Integer failureCount, Set<AfpProcessSummaryDetail> processSummaryDetails) {
    this.processID = processID;
    this.processDate = processDate;
    this.fileCount = fileCount;
    this.successCount = successCount;
    this.failureCount = failureCount;
    this.processSummaryDetails = processSummaryDetails;
}

@Column(name = "FAILURECOUNT")
public Integer getFailureCount() {
    return failureCount;
}

public void setFailureCount(Integer failureCount) {
    this.failureCount = failureCount;
}

@Column(name = "FILECOUNT")
public Integer getFileCount() {
    return fileCount;
}

public void setFileCount(Integer fileCount) {
    this.fileCount = fileCount;
}

@Column(name = "PROCESSDATE")
public String getProcessDate() {
    return processDate;
}

public void setProcessDate(String processDate) {
    this.processDate = processDate;
}

@Id
@Column(name = "PROCESSID", unique = true, nullable = false)
public String getProcessID() {
    return processID;
}

public void setProcessID(String processID) {
    this.processID = processID;
}

@Column(name = "SUCESSCOUNT")
public Integer getSuccessCount() {
    return successCount;
}

public void setSuccessCount(Integer successCount) {
    this.successCount = successCount;
}

@JoinColumn(name="PROCESSDETAIL")
@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
public Set<AfpProcessSummaryDetail> getProcessSummaryDetails() {
    return processSummaryDetails;
}

public void setProcessSummaryDetails(Set<AfpProcessSummaryDetail> processSummaryDetails) {
    this.processSummaryDetails = processSummaryDetails;
}

更新代码

public String updateSummaryDetails(ViewFile viewFile, String codeID) {
        if (viewFile != null && codeID != null) {
            HibernateTemplate transactionTemplate = new HibernateTemplate(sessionFactory, true);
            Object result = transactionTemplate.execute(new HibernateCallback<Object>() {
                @Override
                public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
                    AfpProcessSummaryDetail processSummary =null,newProcessSummary =null;
                    AfpProcessDetail processDetail =  (AfpProcessDetail)session.get(AfpProcessDetail.class,codeID);
                    List<FileProperty> fileList = viewFile.getFileList();
                    Set<AfpProcessSummaryDetail> setProcessSummary=new HashSet<AfpProcessSummaryDetail>();
                    Set<AfpProcessSummaryDetail> modSetProcessSummary=new HashSet<AfpProcessSummaryDetail>();                   
                    setProcessSummary =processDetail.getProcessSummaryDetails();
                    Iterator<AfpProcessSummaryDetail> itrProcessSumm=setProcessSummary.iterator();
                    int srNo = 0;
                    while (itrProcessSumm.hasNext()){
                        processSummary =(AfpProcessSummaryDetail)itrProcessSumm.next();

                    for (FileProperty fileProperty : fileList) {
                        newProcessSummary =new AfpProcessSummaryDetail();
                        newProcessSummary.setSrNo(codeID + "" + srNo);
                        newProcessSummary.setFileName(fileProperty.getName());
                        newProcessSummary.setLocation(fileProperty.getPath());
                        newProcessSummary.setComments(fileProperty.getComment());
                        newProcessSummary.setStatus(fileProperty.getStatus());
                        newProcessSummary.setConvertStatus(fileProperty.getConvertStatus());
                        newProcessSummary.setAfpProcessDetail(processDetail);
                        modSetProcessSummary.add(newProcessSummary);
                        /*if (processSummary.getFileName().trim().equals(fileProperty.getName().trim())){
                            System.out.println("Element removed");
                            itrProcessSumm.remove();
                            modSetProcessSummary.add(newProcessSummary);
                            break;
                        }*/
                        srNo++;
                    }
                }   
            //  setProcessSummary.addAll(modSetProcessSummary);
                processDetail.setProcessSummaryDetails(modSetProcessSummary);
                processDetail.setFailureCount(viewFile.getExceptionNo());
                processDetail.setSuccessCount(viewFile.getSuccessNo());
                processDetail.setActive(viewFile.getActive());
                transactionTemplate.flush();
                session.merge(processDetail);

                System.out.println("updated successfully");
                return codeID;
            }
        });

期望的结果

我想通过Set执行与AfpProcessDetail相关的一对多更新-AfpProcessSummaryDetail。当我尝试替换更新的设置值时,它会尝试将主键更新为null。如果我不替换更新,请不要进行更新。如果我设置了级联,则会给出错误 - 具有相同标识符值的不同对象已与会话关联:

  

[com.aurionpro.convertor.dto.AfpProcessSummaryDetail#15a236f fc961];   嵌套异常是org.hibernate.NonUniqueObjectException:a   具有相同标识符值的不同对象已经关联   与会议。

请建议

0 个答案:

没有答案