插入H2数据库NotSerializableException:org.springframework.jdbc.support.GeneratedKeyHolder“ [90026-196]

问题描述 投票:0回答:1

以下代码,尝试使用ID列作为表的主键插入H2数据库表。 ID值应自动生成!

我真的需要在开始生产之前在H2轻量级数据库中测试我的应用程序逻辑。

public Customer addCustomer(Customer c) {
    String sql = "INSERT INTO Customer_Tbl(FirstName, LastName, Email, PhoneNumber, CreatedById, Status, Version)"
                + "VALUES (?, ?, ?, ?, ?, ?, ?)";

    KeyHolder keyHolder = new GeneratedKeyHolder();

    Db.update(sql, new Object[] {c.getFirstName(), c.getLastName(), c.getEmail(), c.getPhoneNumber(), c.getCreatedById(), 1, 0},  keyHolder);

    long id = (long)keyHolder.getKey();

    return getCustomerById(id);
}

并且Db是数据成员:

private JdbcTemplate Db;

这将引发以下异常,任何帮助。

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [INSERT INTO Customer_Tbl(FirstName, LastName, Email, PhoneNumber, CreatedById, Status, Version)VALUES (?, ?, ?, ?, ?, ?, ?)]; SQL state [90026]; error code [90026]; Serialization failed, cause: "java.io.NotSerializableException: org.springframework.jdbc.support.GeneratedKeyHolder" [90026-196]; nested exception is org.h2.jdbc.JdbcSQLException: Serialization failed, cause: "java.io.NotSerializableException: org.springframework.jdbc.support.GeneratedKeyHolder" [90026-196]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1443)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:862)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:917)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:927)
    at com.activeteam.eab.storage.h2repository.CustomerRepositoryImpl.addCustomer(CustomerRepositoryImpl.java:29)
    at com.activeteam.eab.storage.h2repository.AppTest.addPendingCustomerShouldSuccess(AppTest.java:35)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.h2.jdbc.JdbcSQLException: Serialization failed, cause: "java.io.NotSerializableException: org.springframework.jdbc.support.GeneratedKeyHolder" [90026-196]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:168)
    at org.h2.util.JdbcUtils.serialize(JdbcUtils.java:377)
    at org.h2.value.ValueJavaObject.getNoCopy(ValueJavaObject.java:48)
    at org.h2.value.DataType.convertToValue1(DataType.java:1131)
    at org.h2.value.DataType.convertToValue(DataType.java:1016)
    at org.h2.jdbc.JdbcPreparedStatement.setObject(JdbcPreparedStatement.java:471)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:411)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:232)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:163)
    at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.doSetValue(ArgumentPreparedStatementSetter.java:69)
    at org.springframework.jdbc.core.ArgumentPreparedStatementSetter.setValues(ArgumentPreparedStatementSetter.java:50)
    at org.springframework.jdbc.core.JdbcTemplate.lambda$update$0(JdbcTemplate.java:865)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
    ... 28 more
Caused by: java.io.NotSerializableException: org.springframework.jdbc.support.GeneratedKeyHolder
    at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1185)
    at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:349)
    at org.h2.util.JdbcUtils.serialize(JdbcUtils.java:374)
    ... 40 more

我搜索网络时没有任何点击!

java h2 jdbctemplate
1个回答
0
投票

尝试在pojo类Customer中实现Serializable接口。

© www.soinside.com 2019 - 2024. All rights reserved.