Spring Data JDBC-一对多-Kotlin

时间:2018-12-05 17:54:22

标签: java spring spring-data spring-data-jdbc

我使用spring boot 2.1.1.RELEASE和spring-data-jdbc 1.0.3.RELEASE,Kotlin 1.3.10。

我在kotlin中有以下简单的类定义:

@Table(value = "CUSTOMER")
data class Customer(
    @Id
    var id: Long?,
    @Column("NAME")
    var name: String,
    @Column("PUBLICATION_NAME")
    var publicationName: String,
    @Column("START_DATE")
    var startDate: String,
    @Column("END_DATE")
    var endDate: String,

    var items: MutableSet<Item>

)

@Table(value = "ITEM")
data class Item  (
    @Id
    var id: Long?,
    @Column("name")
    var name: String,
    @Column("title")
    var title: String,
    @Column("weight")
    var weight: Double
)

以及以下数据库表定义:

CREATE TABLE customer(
  id                SERIAL PRIMARY KEY,
  name              VARCHAR(256) NOT NULL,
  publication_name  VARCHAR(128) NOT NULL,
  start_date        VARCHAR(10),
  end_date          VARCHAR(10)
);

CREATE TABLE item(
  id               SERIAL PRIMARY KEY,
  customer         INTEGER,
  name             VARCHAR(256)     NOT NULL,
  title            VARCHAR(128),
  weight           DOUBLE PRECISION NOT NULL
);

我收到以下错误:

org.springframework.data.mapping.MappingException: Could not read property @org.springframework.data.annotation.Id()private java.lang.Long net.service.model.query.Item.id from result set!

    at org.springframework.data.jdbc.core.EntityRowMapper.readFrom(EntityRowMapper.java:130)
    at org.springframework.data.jdbc.core.EntityRowMapper.readEntityFrom(EntityRowMapper.java:143)
    at org.springframework.data.jdbc.core.EntityRowMapper.readFrom(EntityRowMapper.java:124)
    at org.springframework.data.jdbc.core.EntityRowMapper.lambda$createInstance$0(EntityRowMapper.java:167)
    at org.springframework.data.relational.core.conversion.BasicRelationalConverter$ConvertingParameterValueProvider.getParameterValue(BasicRelationalConverter.java:251)
    at org.springframework.data.convert.KotlinClassGeneratingEntityInstantiator$DefaultingKotlinClassInstantiatorAdapter.extractInvocationArguments(KotlinClassGeneratingEntityInstantiator.java:230)
    at org.springframework.data.convert.KotlinClassGeneratingEntityInstantiator$DefaultingKotlinClassInstantiatorAdapter.createInstance(KotlinClassGeneratingEntityInstantiator.java:204)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:84)
    at org.springframework.data.relational.core.conversion.BasicRelationalConverter.createInstance(BasicRelationalConverter.java:141)
    at org.springframework.data.jdbc.core.EntityRowMapper.createInstance(EntityRowMapper.java:160)
    at org.springframework.data.jdbc.core.EntityRowMapper.mapRow(EntityRowMapper.java:71)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:61)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:679)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:617)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:669)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:694)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:748)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:235)
    at org.springframework.data.jdbc.core.DefaultDataAccessStrategy.findById(DefaultDataAccessStrategy.java:204)
    at org.springframework.data.jdbc.core.JdbcAggregateTemplate.findById(JdbcAggregateTemplate.java:135)
    at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.findById(SimpleJdbcRepository.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:99)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy95.findById(Unknown Source)
    at net.service.model.query.repository.CustomerRepositoryV2Test.should save Customer with all Items(CustomerRepositoryV2Test.kt:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.h2.jdbc.JdbcSQLException: Column "items_id" not found [42122-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.jdbc.JdbcResultSet.getColumnIndex(JdbcResultSet.java:3148)
    at org.h2.jdbc.JdbcResultSet.get(JdbcResultSet.java:3247)
    at org.h2.jdbc.JdbcResultSet.getObject(JdbcResultSet.java:529)
    at org.springframework.data.jdbc.core.EntityRowMapper.readFrom(EntityRowMapper.java:127)
    ... 88 more

从spring文档中: “被引用实体的表应具有与引用实体表相同的附加列。您可以通过实现NamingStrategy.getReverseColumnName(RelationalPersistentProperty属性)来更改此名称。”

我在做什么错了?

更新: 添加Java集集合并没有没有帮助

var items: java.util.HashSet<Item> = java.util.HashSet()

更新2: 仍然没有解决办法,在github上添加了可复制的示例项目:

https://github.com/ielkhalloufi/spring-data-jdbc-example

3 个答案:

答案 0 :(得分:2)

尝试将集合移出构造函数并移至主体,如

@Table(value = "CUSTOMER")
data class Customer(
    @Id
    var id: Long?,
    @Column("NAME")
    var name: String,
    @Column("PUBLICATION_NAME")
    var publicationName: String,
    @Column("START_DATE")
    var startDate: String,
    @Column("END_DATE")
    var endDate: String
){
    var items: MutableSet<Item> = HashSet()
}

然后像这样编辑测试

val gasLineItem = Customer(
            id = null,
            name = "name",
            publicationName = "",
            startDate = "2018-12-01",
            endDate = "2019-02-01"

    ).apply {
        items = mutableSetOf(
                gasCreative1,
                gasCreative2
        )
    }

答案 1 :(得分:1)

这似乎是与通过构造函数设置的集合值属性有关的错误。

Kotlin用所有属性(包括Set)创建一个构造函数。 不幸的是,这触发了一个错误,即Spring Data JDBC试图从外部实体的ResultSet加载所包含的实体。 相反,它应该发出另一个选择,当只能通过setter或“ wither”设置该属性时,它将执行该选择。

请在this

提交问题

一种解决方法是从构造函数中排除集合值属性。 由于我对Kotlin几乎一无所知,所以我不能告诉你如何做到这一点。

答案 2 :(得分:1)

您还可以使用@PersistenceConstructor明确地告诉Spring Data JDBC一个不包含Set(这是导致该错误的原因)的构造函数。例如:

@Table(value = "CUSTOMER")
data class Customer(
    @Id
    var id: Long?,
    @Column("NAME")
    var name: String,
    @Column("PUBLICATION_NAME")
    var publicationName: String,
    @Column("START_DATE")
    var startDate: String,
    @Column("END_DATE")
    var endDate: String,
    var items: MutableSet<Item>
) {

    @PersistenceConstructor
    constructor(
        id: Long?, name: String, publicationName: String, startDate: String, endDate: String
    ) : this(
        id,
        name,
        publicationName,
        startDate,
        endDate,
        hashSetOf()
    )
}