org.hsqldb.HsqlException: 计算的行数不一致

org.hsqldb.HsqlException: discrepancy in the number of rows counted

我正在尝试使用一个不安全的应用程序 (Insecure Bank),并且我正在尝试移植它以使其作为个人项目更加安全。

我在将数据库内容插入内存中的数据库时遇到问题。有这两个表:

create table cashaccount (
    id int,
    number varchar(80) not null,
    username varchar(80)  not null,
    availablebalance double precision,
    description varchar(80)  not null,
    constraint pk_cashaccount primary key (id)
);

create table creditaccount(
    id int,
    number varchar(80) not null,
    username varchar(80)  not null,
    description varchar(80)  not null,
    availablebalance double precision,
    cashaccountid int,
    constraint pk_creditaccount primary key (id)
);

预计要插入的一些值是:

INSERT INTO cashaccount VALUES (1,'10669803404133150948', 'ray', 3424.32,'Checking Account');
INSERT INTO cashaccount VALUES (2,'00948343154448310446', 'joby', 2479.13,'Checking Account');
INSERT INTO cashaccount VALUES (3,'51846636433522240425', 'john', 2134.10,'Checking Account');

INSERT INTO creditaccount VALUES (1,'4024 0071 5848 6471', 'ray', 'Visa Gold',3424.32, 1);
INSERT INTO creditaccount VALUES (2,'4929 1294 1877 8806', 'joby', 'Visa Gold', 2479.13, 2);
INSERT INTO creditaccount VALUES (3,'5521 0508 7181 1232', 'john', 'MasterCard', 2134.10, 3);

但是当 Hibernate 尝试在应用程序开始时插入值(我在 resources 文件夹中使用 import.sql 文件)我得到此错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "INSERT INTO creditaccount VALUES (1,'4024 0071 5848 6471', 'ray', 'Visa Gold',3424.32, 1)" via JDBC Statement
...
Caused by: java.sql.SQLSyntaxErrorException: discrepancy in the number of rows counted
...
Caused by: org.hsqldb.HsqlException: discrepancy in the number of rows counted
...

我已经映射了这样的实体:

@Entity
@Table(name = "cashaccount")
public class CashAccount {

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

    @NotEmpty
    @Size(max = 80)
    private String number;

    @NotEmpty
    @Size(min = 2, max = 80)
    private String username;

    @NotNull
    private double availablebalance;

    @NotEmpty
    @Size(min = 10, max = 80)
    private String description;

    @OneToMany(mappedBy="cashAccount", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<CreditAccount> creditAccounts = new HashSet<>();

    public CashAccount() {
    }

    public CashAccount(@NotEmpty @Size(max = 80) String number, @NotEmpty @Size(min = 2, max = 80) String username,
                       @NotNull double availablebalance, @NotEmpty @Size(min = 10, max = 80) String description) {
        this.number = number;
        this.username = username;
        this.availablebalance= availablebalance;
        this.description = description;
    }
    
    // Setters and getters

@Entity
@Table(name = "creditaccount")
public class CreditAccount {

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

    @NotEmpty
    @Size(max = 80)
    private String number;

    @NotEmpty
    @Size(min = 2, max = 80)
    private String username;

    @NotEmpty
    @Size(min = 10, max = 80)
    private String description;

    @NotNull
    private double availablebalance;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", insertable = false, updatable = false)
    private CashAccount cashAccount;

    public CreditAccount() {
    }

    public CreditAccount(@NotEmpty @Size(max = 80) String number, @NotEmpty @Size(min = 2, max = 80) String username,
                         @NotEmpty @Size(min = 10, max = 80) String description, @NotNull double availablebalance,
                         @NotNull CashAccount cashAccount) {
        this.number = number;
        this.username = username;
        this.description = description;
        this.availablebalance = availablebalance;
        this.cashAccount = cashAccount;
    }
    
    // Setters and getters

但是我不知道哪里错了,哪里做错了。

根据@SternK 的建议,我更改了:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", insertable = false, updatable = false)
private CashAccount cashAccount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cashaccountid")
private CashAccount cashAccount;

但现在我收到错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "INSERT INTO creditaccount VALUES (1,'4024 0071 5848 6471', 'ray', 'Visa Gold',3424.32, 1)" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applySqlString(SchemaCreatorImpl.java:439) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources(SchemaCreatorImpl.java:492) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.performCreation(SchemaCreatorImpl.java:180) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:135) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.internal.SchemaCreatorImpl.doCreation(SchemaCreatorImpl.java:121) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:156) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:316) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
    at java.base/java.lang.Thread.run(Thread.java:835) ~[na:na]
Caused by: java.sql.SQLDataException: data exception: bad value for CAST
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-3.4.5.jar:na]
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-3.4.5.jar:na]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54) ~[hibernate-core-5.4.18.Final.jar:5.4.18.Final]
    ... 18 common frames omitted
Caused by: org.hsqldb.HsqlException: data exception: bad value for CAST
    at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.Scanner.convertToNumber(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.types.NumberType.convertToType(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.StatementDML.getInsertData(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.StatementInsert.getResult(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.StatementDMQL.execute(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.Session.executeDirectStatement(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.5.1.jar:2.5.1]
    ... 23 common frames omitted
  1. 我猜是这样的:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", insertable = false, updatable = false)
private CashAccount cashAccount;

应该这样更正:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cashaccountid")
private CashAccount cashAccount;
  1. 如本 article 中所建议:

Make sure you are mapping values to correct columns in query. Best way is to define column name and values in query as well.

因此,我建议您按以下方式重写 insert 语句:

INSERT INTO cashaccount(id, number, username, availablebalance, description)
VALUES (1,'10669803404133150948', 'ray', 3424.32,'Checking Account');
...
INSERT INTO creditaccount(id, number, username, description, availablebalance, cashaccountid)
VALUES
(1,'4024 0071 5848 6471', 'ray', 'Visa Gold',3424.32, 1);
...