在读取期间,带有Kotlin的spring-data-mongodb上的@ transient 导致异常

4uqofj5v  于 2023-01-12  发布在  Go
关注(0)|答案(4)|浏览(123)

我在项目中遇到了一个问题,即Kotlin、SpringBoot 2.0和MongoDB(带有Spring Data)的组合,以及@Transient

@Document data class Child(@Id val id: String?, val name: String)
@Document data class Parent(@Id val id: String?, val child: Child? = null, val childId: String)

我知道有一种更好的方法可以从Parent引用Child,但是MongoDB是一个现有的方法。
这里的问题是,我不想在Parent中持久化child属性,所以我尝试用kotlin.jvm.Transientorg.springframework.data.annotation.Transient,或两者来注解该属性,但似乎没有一种组合能解决我的问题。
1.当我使用kotlin.jvm.Transient时,它正在加载,但是当我将它保存回来时,它将与child属性一起持久化
1.当我使用org.springframework.data.annotation.Transient时,它无法使用堆栈跟踪进行读取:

org.springframework.data.mapping.MappingException: No property child found on entity class com.example.sample.domain.Parent to bind constructor parameter to!
    at org.springframework.data.mapping.model.PersistentEntityParameterValueProvider.getParameterValue(PersistentEntityParameterValueProvider.java:68)
    at org.springframework.data.mapping.model.SpELExpressionParameterValueProvider.getParameterValue(SpELExpressionParameterValueProvider.java:49)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.extractInvocationArguments(ClassGeneratingEntityInstantiator.java:250)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(ClassGeneratingEntityInstantiator.java:223)
    at org.springframework.data.convert.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:84)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:272)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:245)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:194)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:190)
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:78)
    at org.springframework.data.mongodb.core.ReactiveMongoTemplate$ReadDocumentCallback.doWith(ReactiveMongoTemplate.java:2920)
    at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)
    at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76)
    at com.mongodb.reactivestreams.client.internal.ObservableToPublisher$1.onNext(ObservableToPublisher.java:68)
    at com.mongodb.async.client.AbstractSubscription.onNext(AbstractSubscription.java:135)
    at com.mongodb.async.client.AbstractSubscription.processResultsQueue(AbstractSubscription.java:203)
    at com.mongodb.async.client.AbstractSubscription.tryProcessResultsQueue(AbstractSubscription.java:159)
    at com.mongodb.async.client.SingleResultCallbackSubscription$1.onResult(SingleResultCallbackSubscription.java:48)
    at com.mongodb.async.client.FindIterableImpl$1$1.onResult(FindIterableImpl.java:213)
    at com.mongodb.async.client.FindIterableImpl$1$1.onResult(FindIterableImpl.java:204)
    at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:136)
    at com.mongodb.operation.AsyncQueryBatchCursor.next(AsyncQueryBatchCursor.java:100)
    at com.mongodb.async.client.FindIterableImpl$1.onResult(FindIterableImpl.java:204)
    at com.mongodb.async.client.FindIterableImpl$1.onResult(FindIterableImpl.java:198)
    at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
    at com.mongodb.async.client.OperationExecutorImpl$1$1.onResult(OperationExecutorImpl.java:82)
    at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
    at com.mongodb.operation.FindOperation$3.onResult(FindOperation.java:806)
    at com.mongodb.operation.OperationHelper$ReferenceCountedReleasingWrappedCallback.onResult(OperationHelper.java:364)
    at com.mongodb.operation.CommandOperationHelper$2.onResult(CommandOperationHelper.java:405)
    at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
    at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor$2.onResult(DefaultServer.java:227)
    at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
    at com.mongodb.internal.connection.CommandProtocolImpl$1.onResult(CommandProtocolImpl.java:85)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection$1.onResult(DefaultConnectionPool.java:461)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection$2.onResult(UsageTrackingInternalConnection.java:111)
    at com.mongodb.internal.async.ErrorHandlingResultCallback.onResult(ErrorHandlingResultCallback.java:49)
    at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:379)
    at com.mongodb.internal.connection.InternalStreamConnection$2$1.onResult(InternalStreamConnection.java:356)
    at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:651)
    at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback$MessageCallback.onResult(InternalStreamConnection.java:618)
    at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:494)
    at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:491)
    at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:236)
    at com.mongodb.internal.connection.InternalStreamConnection.readAsync(InternalStreamConnection.java:491)
    at com.mongodb.internal.connection.InternalStreamConnection.access$1000(InternalStreamConnection.java:74)
    at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:608)
    at com.mongodb.internal.connection.InternalStreamConnection$MessageHeaderCallback.onResult(InternalStreamConnection.java:593)
    at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:494)
    at com.mongodb.internal.connection.InternalStreamConnection$5.completed(InternalStreamConnection.java:491)
    at com.mongodb.connection.netty.NettyStream.readAsync(NettyStream.java:236)
    at com.mongodb.connection.netty.NettyStream.handleReadResponse(NettyStream.java:266)
    at com.mongodb.connection.netty.NettyStream.access$600(NettyStream.java:66)
    at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:325)
    at com.mongodb.connection.netty.NettyStream$InboundBufferHandler.channelRead0(NettyStream.java:322)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:648)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:583)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:500)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:844)

1.当我同时使用这两个注解时,结果与#1相同(很明显),因为应用程序在尝试执行某些逻辑之前阅读数据。
任何帮助都将不胜感激

ffdz8vbo

ffdz8vbo1#

添加一个新的Kotlin构造函数,并给予它注解@PersistenceConstructor

wztqucjr

wztqucjr2#

由于您是在构造函数中声明属性,我认为您需要执行以下操作来将注解应用于隐式创建的字段,因为Spring Data将查看字段的注解而不是构造函数属性的注解:

@get:Transient val child: Child? = null
06odsfpq

06odsfpq3#

这可能会非常烦人,但这是Spring Data 工作的非常确切的方式。
Spring Data 从Mongo加载Parent数据后,需要一个Child对象来创建Parent对象,但Mongo中没有Child数据,抛出异常。
因此,我们应该先创建一个没有Child对象的Parent对象,然后初始化Parent对象的子字段。
我认为使用@org.springframework.data.annotation.Transient和@PersistenceConstructor作为下面的代码是最好的方法。

@Document data class Child(@Id val id: String?, val name: String)
@Document data class Parent(@Id val id: String?, @Transient val child: Child? = null, val childId: String) {
  @PersistenceConstructor   
  constructor(id: String, childId: String): this(id = id, childId = childId, child = null)
}
oug3syen

oug3syen4#

我碰到了这个问题,意识到Koltin有自己的@Transient注解Map到kotlin.jvm.Transient
默认情况下,当您写入@Transient时,导入指向kotlin.jvm
对我有效的解决方案是:

@javax.persistence.Transient val child: Child? = null

相关问题