gpt4 book ai didi

java - Spring Boot Jpa 更新记录而不是插入

转载 作者:行者123 更新时间:2023-12-01 19:57:59 39 4
gpt4 key购买 nike

我正在执行以下查询来查找临时表中但主表中不存在的记录,然后将结果插入主表

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

但是,如果我将单个结果对象传递给 JpaRepository 保存方法(为了更新主表),它会执行更新而不是插入。我会做错什么吗?

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

}

大师类是一样的

下面是坚持到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);
}

最佳答案

JPA 执行更新而不是插入的唯一原因是主键已存在于您的持久层或表中。因此,请检查或发布您的源代码,以便查看它并找出问题所在(如果问题不在您的数据中)。

现在您已经使用源代码更新了问题,您的错误可能出在 postpaytransaction 对象的实例化上。

尝试在其他所有内容之前插入循环

postpaytransaction = new PostPayTransaction()

关于java - Spring Boot Jpa 更新记录而不是插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48827694/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com