当前位置: 首页 > 知识库问答 >
问题:

批处理条目0插入到借方(金额、cid、说明、did)值('100'、0、'Rajesh'、5)被中止:错误:插入或更新表“借方”

赵正雅
2023-03-14

我有两个表creditdebitcredit是父表,debit是子表。我可以提供一个一对多的关系与两个表。在贷方表中为单行数据,而在借方表中为多行数据。

我有尝试尝试插入数据,然后这种类型的错误是发生的。

package com.rojmat.entity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.core.annotation.Order;

@Entity
@Table(name="credit")
public class Credit extends BaseEntity{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column
    private long cid;
    @Column @Order
    private long openingbalance;
    @Column
    private Date date;
    @Column @Order
    private long debittotal;
    @Column @Order
    private long drawertotal;
    @Column @Order
    private long debittotalplusdrawertotal;
    @Column @Order
    private long todaybusiness;

    @OneToMany(cascade={CascadeType.ALL})
    /*@JoinTable(name="credit_debit", 
               joinColumns=@JoinColumn(name="c_id"), 
               inverseJoinColumns=@JoinColumn(name="d_id"))*/
     @JoinColumns({@JoinColumn(name = "cid" ,referencedColumnName = "cid")})
    private List<Debit> debits = new ArrayList<Debit>(Arrays.asList());
    public Credit() {

    }
    public Credit(long cid, long openingbalance, Date date, long debittotal, long drawertotal,
            long debittotalplusdrawertotal, long todaybusiness, List<Debit> debits) {
        super();
        this.cid = cid;
        this.openingbalance = openingbalance;
        this.date = date;
        this.debittotal = debittotal;
        this.drawertotal = drawertotal;
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
        this.todaybusiness = todaybusiness;
        this.debits = debits;
    }
    public long getCid() {
        return cid;
    }
    public void setCid(long cid) {
        this.cid = cid;
    }
    public long getOpeningbalance() {
        return openingbalance;
    }
    public void setOpeningbalance(long openingbalance) {
        this.openingbalance = openingbalance;
    }   
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public long getDebittotal() {
        return debittotal;
    }
    public void setDebittotal(long debittotal) {
        this.debittotal = debittotal;
    }
    public long getDrawertotal() {
        return drawertotal;
    }
    public void setDrawertotal(long drawertotal) {
        this.drawertotal = drawertotal;
    }
    public long getDebittotalplusdrawertotal() {
        return debittotalplusdrawertotal;
    }
    public void setDebittotalplusdrawertotal(long debittotalplusdrawertotal) {
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
    }
    public long getTodaybusiness() {
        return todaybusiness;
    }
    public void setTodaybusiness(long todaybusiness) {
        this.todaybusiness = todaybusiness;
    }
    public List<Debit> getDebit() { 
        return debits;
    }   
    public void setDebit(List<Debit> debit) {   
        this.debits = debits;
    }
    @Override
    public String toString() {
            return "Credit [cid=" + cid + ", openingbalance =" + openingbalance + ", date=" + date + ", debittotal= " + debittotal + ", debittotalplusdrawertotal=" + debittotalplusdrawertotal + ", todaybusiness=" + todaybusiness + "]";
    }
}

package com.rojmat.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="debit")
public class Debit {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private long did;
    @Column(name="cid")
    private long cid;
    @Column
    private String amount;
    @Column
    private String description; 

    public long getDid() {
        return did;
    }
    public void setDid(long did) {
        this.did = did;
    }
    public Debit() {

    }
    public Debit(String amount, String description) {
        super();
        this.amount = amount;
        this.description = description;
    }
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
            return "Debit [did=" + did + ", amount =" + amount + ", description=" + description + "]";
    }
}

3.CreditController.java

@RequestMapping(value="/useraccount", method=RequestMethod.POST)
    public String userAccount(@RequestParam(value = "amount") String[] amount, 
                              @RequestParam(value = "description") String[] description, 
                              ModelMap model, HttpServletRequest request, HttpSession session,@ModelAttribute("command")Credit credit, BindingResult result)
    {
        try {
            List<Debit> debits = new ArrayList<Debit>(Arrays.asList());
            debits = credit.getDebit();
            for(int i=0; i<amount.length; i++) {
                Debit debit = new Debit();
                debit.setAmount(amount[i]);
                debit.setDescription(description[i]);
                debits.add(debit);
            }   
            System.out.println("debits ="+debits);
            List<Debit> debits1 = new ArrayList<Debit>();
            credit.setDebit(debits1);
            for(int i=0; i<debits.size(); i++)
            {
                debits1.add(new Debit(debits.get(i).getAmount(),debits.get(i).getDescription()));
            }
            System.out.println("debits1 ="+debits1);
            User user = new User();
             // Credit Data Set
                credit.setOpeningbalance(credit.getOpeningbalance());
                credit.setDate(credit.getDate());
                credit.setDebittotal(credit.getDebittotal());
                credit.setDrawertotal(credit.getDrawertotal());
                credit.setDebittotalplusdrawertotal(credit.getDebittotalplusdrawertotal());
                credit.setTodaybusiness(credit.getTodaybusiness());
                credit.setCreatedBy(user.getEmail());
                credit.setCreatedDate(new Date());
                credit.setUpdatedBy(user.getEmail());
                credit.setUpdatedDate(new Date());  
            //  Debit Data set  
                System.out.println("Debit List = " + debits1.size());
                System.out.println("Credit and Debit data seved successfully");
                creditService.addCreditDebit(credit);
                model.put("success", "Data Saved Successfully");
        }catch(Exception e) {
            e.printStackTrace();
        }
        return "redirect:useraccount";
    }

4.creditdaoimpl.java

package com.rojmat.daoImpl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.rojmat.dao.CreditDao;
import com.rojmat.entity.Credit;

@Repository
public class CreditDaoImpl implements CreditDao{

    @Autowired
    private SessionFactory sessionFactory;
    @Override
    public void addCreditDebit(Credit credit) {
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(credit);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

共有1个答案

邬宏扬
2023-03-14

这个问题由我自己解决

 类似资料:
  • 问题内容: 我有一个dao,它基本上使用hibernate将记录插入到一​​个表中,该dao用标记为注释,并且我有一个服务,该服务会生成其他一些东西,然后调用我的dao。我的服务也标注了使用。 我叫服务循环。我在dao上的插入内容是否可以批量或一个接一个地工作?我如何确定它们可以批量工作?hibernateTransaction Manager是否管理批处理插入? 我正在使用Oracle DB。

  • 我是Talendel的新手,正在使用Talend Open Studio for Big Data V6.2。 我开发了一个简单的Talend ETL作业,它从tFileInputExcel和tOracleInput(维度日期)中获取数据并将数据插入我的本地Oracle数据库。 以下是我的包裹外观: 此作业运行,但我得到0行插入到我的本地Oracle数据库

  • 问题内容: 有没有办法像在MySQL服务器上那样批量执行查询? 将无法使用,因为如果该字段已经存在,它将直接忽略该字段并且不插入任何内容。 将无法工作,因为如果该字段已经存在,它将首先对其进行处理,然后再次进行处理,而不是对其进行更新。 可以使用,但不能批量使用。 因此,我想知道是否可以批量发出这样的命令(一次不止一次发送)。 问题答案: 您 可以 使用INSERT … ON DUPLICATE

  • 问题内容: 我有责任将我们的代码从sqlite切换到postgres。我遇到麻烦的查询之一复制到下面。 当有重复的记录时,就会出现此问题。在此表中,两个值的组合必须唯一。我在其他地方使用了一些plpgsql函数来执行更新或插入操作,但是在这种情况下,我可以一次执行多个插入操作。我不确定如何为此编写存储的例程。感谢您提供的所有sql专家的所有帮助! 问题答案: 有 3个 挑战。 您的查询在表和之间没

  • 我需要在我的MySQL数据库中进行批量插入(近10000个)。我正在使用JPA/Hibernate和spring Boot。我从hibernate文档中读到了在hibernate中执行大容量插入/更新,我认为我的代码不能正常工作,因为它顺序地插入hibernate查询,而不是在批处理插入中执行它们。下面是我的代码。我是不是漏掉了什么? 这是我的数据源配置。 我是不是漏掉了什么? 请帮帮忙。

  • 我正在写一个数据挖掘程序,可以批量插入用户数据。 当前SQL只是一个普通的批量插入: 如果发生冲突,如何进行更新?我试过: 但它抛出

  • 问题内容: UPSERT操作会更新表或在表中插入一行,这取决于表是否已经有与数据匹配的行: 由于Oracle没有特定的UPSERT语句,执行此操作的最佳方法是什么? 问题答案: MERGE(“老式方式”)的替代方法:

  • 问题内容: 我需要从每日CSV文件中消耗大量数据。CSV包含约12万条记录。使用hibernate模式时,这会减慢爬行速度。基本上,当使用saveOrUpdate()时,hibernate似乎在每个单独的INSERT(或UPDATE)之前执行SELECT。对于使用saveOrUpdate()持久存储的每个实例,在实际的INSERT或UPDATE之前发出SELECT。我能理解为什么要这样做,但是在进