简体   繁体   中英

Caused by: java.sql.SQLException:The column name is specified more than once in the SET clause

I am facing a below issue while persisting data to a table.

Error :

Caused by: java.sql.SQLException: The column name 'SubFundId' is specified more than once in the SET clause. A column cannot be assigned more than one value in the same SET clause. Modify the SET clause to make sure that a column is updated only once. If the SET clause updates columns of a view, then the column name 'SubFundId' may appear twice in the view definition.

My piece of code for persisting data is :

 @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;
    }
    }

MApping with SubFund Table is as below:

@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.

In your class, you have two fields that are mapped to the same column SubFundId as below:

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

AND

private long subFundId;

So, hibernate is now confused as to which field needs to be used for subFundId column.

To solve this either you remove field long subFundId or if you need both then place insertable = false, updatable = false on private long subFundId as below :

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

You have declared a reference to a SubFund entity:

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

plus you've also declared a field which maps the same reference

private long subFundId;

Choose which one you want to keep, if you need to handle the entities at the same time keep the first, if in all your application they are handled separately you can save yourself some troubles and keep the second.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM