当我尝试使用spring r2dbc数据插入一个只有一个id的实体时,我得到了以下异常:
Exception while fetching data (addChat) : INSERT contains no values
java.lang.IllegalStateException: INSERT contains no values
at org.springframework.data.r2dbc.core.DefaultStatementMapper.getMappedObject(DefaultStatementMapper.java:159) ~[spring-data-r2dbc-1.0.0.RC1.jar:1.0.0.RC1]
at org.springframework.data.r2dbc.core.DefaultStatementMapper.getMappedObject(DefaultStatementMapper.java:144) ~[spring-data-r2dbc-1.0.0.RC1.jar:1.0.0.RC1]数据库postgres:
create table chats
(
id serial not null
constraint chats_pkey
primary key
);实体
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
@Table("chats")
data class ChatEntity(
@Id val id: Int? = null
)存储库:
import org.springframework.data.r2dbc.repository.R2dbcRepository
import org.springframework.stereotype.Repository
@Repository
interface ChatsRepository : R2dbcRepository<ChatEntity, Int>服务:
//Service
val chatAdded = chatsRepository.save(ChatEntity(id = null)).awaitFirst()在同一项目上,我有其他实体的id和其他列的工作很好。你知道我为什么会有这个错误吗?
发布于 2019-12-14 16:57:17
如果你使用postgres,你可以执行(…)如下所示,并返回插入的值。
val chatAdded = db.execute("INSERT INTO chats\n" +
" DEFAULT VALUES\n" +
" RETURNING *")
.map { row -> ChatEntity(id = row.get("id") as Int) }
.awaitOne()发布于 2021-04-29 08:09:37
我也遇到了同样的问题,并在我的实体实现org.springframework.data.domain.Persistable时持久化了它,其中T是实体id类型。
下面是我如何在kotlin和协程中实现它。我假设您的上下文配置是ok的,并且您正确地配置了org.springframework.r2dbc.core.DatabaseClient
实体:
import org.springframework.data.relational.core.mapping.Table
import org.springframework.data.annotation.*
import org.springframework.data.domain.Persistable
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size
@Table("`authority`")
data class Authority(
@Id val role: @NotNull @Size(max = 50) String
) : Persistable<String> {
override fun isNew() = true
override fun getId() = role
}DAO:
import org.springframework.data.repository.kotlin.CoroutineCrudRepository
@Repository("authorityRepository")
interface AuthorityRepository : CoroutineCrudRepository<Authority, String>功能测试
import kotlinx.coroutines.runBlocking
import kotlin.test.assertEquals
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import Authority
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.r2dbc.core.awaitSingle
@SpringBootTest
class AuthorityRepositoryFuncTest {
@Autowired
lateinit var db: DatabaseClient
@Autowired
private lateinit var authorityRepository: AuthorityRepository
suspend fun countAuthority(): Long = db
.sql("SELECT COUNT(*) FROM `authority`")
.fetch()
.awaitSingle()
.values
.first() as Long
@Test
fun `test save authority`() = runBlocking {
val countAuthorityBeforeSave = countAuthority()
val authorityTestValue = "ROLE_TEST"
authorityRepository.save(Authority(role = authorityTestValue))
assertEquals(countAuthorityBeforeSave + 1, countAuthority())
}
}https://stackoverflow.com/questions/59126551
复制相似问题