我正在执行以下查询以查找临时表中的记录并且在master中不存在然后将结果插入到主表中
@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);
}
答案 0 :(得分:2)
JPA执行更新而不是插入的唯一原因是主键已存在于持久层或表中。请仔细检查或发布您的源代码,以便查看并查找错误(如果问题不在您的数据中)。
现在您已经使用源代码更新了问题,您的错误可能在于postpaytransaction对象的实例化。
尝试在其他所有内容之前插入循环
postpaytransaction = new PostPayTransaction ()
答案 1 :(得分:0)
我遇到了完全相同的问题。我的观点是,SpringBoot在启动时会自动重新创建数据库,错误地在表中创建了identity列。
寻找spring.jpa.hibernate.ddl-auto
。