繁体   English   中英

在 Hibernate/Oracle 上执行批量更新/插入时出现异常

[英]Exception while executing batch update/insert on Hibernate/Oracle

我在将对象持久保存到数据库中时遇到异常,下面是代码库模型:

@Entity
@Table(name = "customer_data")
public class BsslpCustomerInfo {
    @Id
    @Column(name = "id", length = 10)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "service_Id", length = 50, unique = true, nullable = false)
    private String serviceId;

    @Column(name = "quarter", length = 255, nullable = false)
    private String quarter;

    @Column(name = "year")
    private int year;

    @Column(name = "activated_Customer_Name")
    private String activatedCustomerName;

    @Column(name = "deactivated_Customer_Name")
    private String deactivatedCustomerName;

    @Column(name = "quarter_Start_Date")
    private String quarterStartDate;

    @Column(name = "quarter_End_Date")
    private String quarterEndDate;

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public String getQuarter() {
        return quarter;
    }

    public void setQuarter(String quarter) {
        this.quarter = quarter;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String getActivatedCustomerName() {
        return activatedCustomerName;
    }

    public void setActivatedCustomerName(String activatedCustomerName) {
        this.activatedCustomerName = activatedCustomerName;
    }

    public String getDeactivatedCustomerName() {
        return deactivatedCustomerName;
    }

    public void setDeactivatedCustomerName(String deactivatedCustomerName) {
        this.deactivatedCustomerName = deactivatedCustomerName;
    }

    public String getQuarterStartDate() {
        return quarterStartDate;
    }

    public void setQuarterStartDate(String quarterStartDate) {
        this.quarterStartDate = quarterStartDate;
    }

    public String getQuarterEndDate() {
        return quarterEndDate;
    }

    public void setQuarterEndDate(String quarterEndDate) {
        this.quarterEndDate = quarterEndDate;
    }

    public int getId() {
        return id;
    }

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

...这是“控制器”代码:

for (BsslpParty bsslp : data) {
    Date activationDate = sdf.parse(bsslp.getAssocStartDate());
    Date deactivationDate = sdf.parse(bsslp.getAssocEndDate());
    if(activationDate.compareTo(startDate)>=0 && activationDate.compareTo(endDate)<=0){
        activeCount++;
        BsslpCustomerInfo bsslpCustomerInfo = new BsslpCustomerInfo();
        bsslpCustomerInfo.setServiceId(bsslp.getBsslpName());
        bsslpCustomerInfo.setActivatedCustomerName(bsslp.getEntitledPartyName());
        bsslpCustomerInfo.setQuarter(quarterDateRange.getStartdate());
        bsslpCustomerInfo.setYear(quarterDateRange.getFinYear());
        bsslpCustomerInfo.setQuarterStartDate(quarterDateRange.getStartdate());
        bsslpCustomerInfo.setQuarterEndDate(quarterDateRange.getEndDate());
        listOfActDeActCust.add(bsslpCustomerInfo);              
    }

    usageMetricschartService.saveAllCustomerData(listOfActDeActCust);
}

“服务”实现:

@Override
@Transactional
public void saveAllCustomerData(List<BsslpCustomerInfo> customersData) {
    // TODO Auto-generated method stub

    for (BsslpCustomerInfo bsslpCustomerInfo : customersData) {
        usageMetricsCustomersRepository.save(bsslpCustomerInfo);
    }

}

它抛出一个错误,如:

java.sql.BatchUpdateException: ORA-01722: 无效数字

oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343) org.springframework.dao.InvalidDataAccessResourceUsageException:无法执行批处理; SQL [插入customer_data (activated_Customer_Name、deactivated_Customer_Name、quad、quority_End_Date、quarter_Start_Date、service_Id、year、id) 值(?, ?, ?, ?, ?, ?, ?, ?)]; 嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法执行批处理

我尝试了所有可能的方法都无法解决这个问题; 任何人都可以帮助我吗?

您已将id定义为自动生成,但对于您创建的每个实体,您都将 0 值传递给数据库。 这是因为您使用的原始int类型默认初始化为 0。如果您为id字段设置一个值,则不会自动生成。

id属性更改为Integer类型,以便在持久化时将值发送到 JPA,这将导致生成 id。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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