引发者:java.sql.SQLException:在SET子句中多次指定列名

时间:2016-04-04 11:58:11

标签: hibernate

我在将数据保存到表格时面临以下问题。

错误:

引起:java.sql.SQLException:在SET子句中多次指定列名“SubFundId”。在同一SET子句中不能为列分配多个值。修改SET子句以确保列只更新一次。如果SET子句更新视图的列,则列名“SubFundId”可能会在视图定义中出现两次。

我保存数据的代码是:

 @Transactional
            public FundSalesCreditCalcMethodDTO addNewSubFundCalculationMethod(FundSalesCreditCalcMethodDTO dto) {
                try{
                    @SuppressWarnings("unchecked")
                    List<Object> id = entityManager.createNamedQuery("findSelectedSubFund").setParameter("subFundId",dto.getSubFundId()).getResultList();
                    System.out.println("*********in addNewSubFundCalculationMethod " +id.toString());
                    if (id.isEmpty()){
                        System.out.println("*********in addNewSubFundCalculationMethod: List is empty");
                        FundSalesCreditCalcMethod fundSalesCreditCalcMethod = new FundSalesCreditCalcMethod();
    fundSalesCreditCalcMethod.setSubFundId(dto.getSubFundId());

                    fundSalesCreditCalcMethod.setEffectiveFromDate(dto.getEffectiveFromDate());
                    fundSalesCreditCalcMethod.setEffectiveToDate(dto.getEffectiveToDate());
    fundSalesCreditCalcMethod.setCreatedBy(dto.getCreatedBy());
                    fundSalesCreditCalcMethod.setCreatedOn(dto.getCreatedOn());
                    fundSalesCreditCalcMethod.setLastUpdatedBy(dto.getLastUpdatedBy());
                    fundSalesCreditCalcMethod.setLastUpdatedOn(dto.getLastUpdatedOn());

                    fundSalesCreditCalcMethod.setSalesCreditCalcMethodId(1);
                    fundSalesCreditCalcMethod.setPaymentFrequencyId(1);
                    Date date = new Date();
                    fundSalesCreditCalcMethod.setFirstPaymentDate(date);
this.entityManager.persist(fundSalesCreditCalcMethod);

            }
        }
        catch(NoResultException exception){

        }

        this.entityManager.flush();
        return dto;
    }
    }

使用SubFund表进行监控如下:

@ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="SubFundId", referencedColumnName = "SubFundId")
    private SubFund subFund;


    private long salesCreditCalcMethodId;
    private long subFundId;
    private Date effectiveFromDate;
    private Date effectiveToDate;
    private long paymentFrequencyId;
    private Date firstPaymentDate;
    private Date createdOn;
    private Date lastUpdatedOn;
    private String createdBy;
    private String lastUpdatedBy;


Can anyone please help what is the issue.I have been struggling with it from morning.

2 个答案:

答案 0 :(得分:2)

在您的课程中,您有两个字段映射到同一列SubFundId,如下所示:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="SubFundId", referencedColumnName = "SubFundId")
private SubFund subFund;

private long subFundId;

因此,hibernate现在对于subFundId列需要使用哪个字段感到困惑。

要解决此问题,请删除字段long subFundId,或者如果您需要两个字段,请在private long subFundId上放置 insertable = false,updatable = false ,如下所示:

@Column(name="SubFundId", insertable = false, updatable = false)

答案 1 :(得分:0)

您已声明对SubFund实体的引用:

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="SubFundId", referencedColumnName = "SubFundId")
private SubFund subFund;

此外,您还宣布了一个映射相同参考的字段

private long subFundId;

选择你要保留的那个,如果你需要同时处理实体,保留第一个,如果在你的所有应用程序中它们被单独处理,你可以省去一些麻烦并保留第二个。