简体   繁体   中英

Springboot Jpa updating records instead of inserting

I'm doing the below query to find records that are in a temp table and dont exist in master then insert the results to the master table

@Query(value = "select b from InboundTemp b where b.transactionId NOT IN (SELECT p2.transactionId FROM Inbound p2)")
ArrayList<InboundTemp> findMissing();

However if I pass a single result object to the JpaRepository save method (To update the master table)it does an update instead of an insert. what would I be doing wrong?

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;


    @Entity
    @Table(name="inbound_postpay_temp")
    @NamedQuery(name="InboundPostpayTemp.findAll", query="SELECT i FROM InboundPostpayTemp i")
    public class InboundPostpayTemp implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        private int id;

        @Column(name="bill_ref_no")
        private String billRefNo;

        @Column(name="business_shortcode")
        private String businessShortcode;

        private byte clicked;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="created_at")
        private Date createdAt;

        private String kpresponse;

        private String KPtransaction_id;

        @Column(name="mpesa_sender")
        private String mpesaSender;

        private String msisdn;

        @Column(name="Network")
        private String network;

        @Column(name="org_account_balance")
        private float orgAccountBalance;

        private String status;

        @Column(name="transaction_amount")
        private float transactionAmount;

        @Column(name="transaction_id")
        private String transactionId;

        @Column(name="transaction_time")
        private String transactionTime;

        @Column(name="transaction_type")
        private String transactionType;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="updated_at")
        private Date updatedAt;

        public InboundPostpayTemp() {
        }

        public int getId() {
            return this.id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getBillRefNo() {
            return this.billRefNo;
        }

        public void setBillRefNo(String billRefNo) {
            this.billRefNo = billRefNo;
        }

        public String getBusinessShortcode() {
            return this.businessShortcode;
        }

        public void setBusinessShortcode(String businessShortcode) {
            this.businessShortcode = businessShortcode;
        }

        public byte getClicked() {
            return this.clicked;
        }

        public void setClicked(byte clicked) {
            this.clicked = clicked;
        }

        public Date getCreatedAt() {
            return this.createdAt;
        }

        public void setCreatedAt(Date createdAt) {
            this.createdAt = createdAt;
        }

        public String getKpresponse() {
            return this.kpresponse;
        }

        public void setKpresponse(String kpresponse) {
            this.kpresponse = kpresponse;
        }

        public String getKPtransaction_id() {
            return this.KPtransaction_id;
        }

        public void setKPtransaction_id(String KPtransaction_id) {
            this.KPtransaction_id = KPtransaction_id;
        }

        public String getMpesaSender() {
            return this.mpesaSender;
        }

        public void setMpesaSender(String mpesaSender) {
            this.mpesaSender = mpesaSender;
        }

        public String getMsisdn() {
            return this.msisdn;
        }

        public void setMsisdn(String msisdn) {
            this.msisdn = msisdn;
        }

        public String getNetwork() {
            return this.network;
        }

        public void setNetwork(String network) {
            this.network = network;
        }

        public float getOrgAccountBalance() {
            return this.orgAccountBalance;
        }

        public void setOrgAccountBalance(float orgAccountBalance) {
            this.orgAccountBalance = orgAccountBalance;
        }

        public String getStatus() {
            return this.status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public float getTransactionAmount() {
            return this.transactionAmount;
        }

        public void setTransactionAmount(float transactionAmount) {
            this.transactionAmount = transactionAmount;
        }

        public String getTransactionId() {
            return this.transactionId;
        }

        public void setTransactionId(String transactionId) {
            this.transactionId = transactionId;
        }

        public String getTransactionTime() {
            return this.transactionTime;
        }

        public void setTransactionTime(String transactionTime) {
            this.transactionTime = transactionTime;
        }

        public String getTransactionType() {
            return this.transactionType;
        }

        public void setTransactionType(String transactionType) {
            this.transactionType = transactionType;
        }

        public Date getUpdatedAt() {
            return this.updatedAt;
        }

        public void setUpdatedAt(Date updatedAt) {
            this.updatedAt = updatedAt;
        }

    }

The master class is the same

Below is the method that is persisting to dB

missing = temprepo.findMissing();





    for (InboundPostpayTemp inboundPostpayTemp2 : missing) {

                        postpaytransaction.setBillRefNo(inboundPostpayTemp2.getBillRefNo());
                        postpaytransaction.setBusinessShortcode("");
                        // postpaytransaction.setClicked("0".t);
                        postpaytransaction
                                .setCreatedAt(new java.sql.Timestamp(inboundPostpayTemp2.getCreatedAt().getTime()));
                        postpaytransaction.setMpesaSender(inboundPostpayTemp2.getMpesaSender());
                        postpaytransaction.setMsisdn(inboundPostpayTemp2.getMsisdn());
                        postpaytransaction.setTransactionAmount(inboundPostpayTemp2.getTransactionAmount());
                        postpaytransaction.setTransactionId(inboundPostpayTemp2.getTransactionId());
                        postpaytransaction.setTransactionType("Paybill-Repost");
                        postpaytransaction.setStatus("CONFIRMED");
                        postpaytransaction.setTransactionTime(inboundPostpayTemp2.getTransactionTime());

                        //postpaytransactionx.add(postpaytransaction);

                        inboundpostpayrepo.save(postpaytransaction);
                    }

The only reason why JPA does an update instead of insert is that the Primary Key already exists in your persistence layer or your table. So kindly check or post your source code in order to review it and find what is wrong (if the issue is not in your data).

Now that you have updated the question with the source code, your bug is probably on the instantiation of the postpaytransaction object.

try to insert into your loop before everything else

postpaytransaction = new PostPayTransaction ()

I had the exactly same issue. My point was that the SpringBoot was automatically recreating the DB on startup, incorrectly creating the identity column in the table.

Look for spring.jpa.hibernate.ddl-auto .

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