相关疑难解决方法(0)

在纯JPA设置中获取数据库连接

我们有一个JPA应用程序(使用hibernate),我们需要调用一个需要JDBC数据库连接作为参数的旧报告工具.是否有一种简单的方法可以访问hibernate设置的JDBC连接?

java orm hibernate jpa jdbc

59
推荐指数
7
解决办法
11万
查看次数

如何在Spring Boot应用程序中清理数据库表?

我当前的尝试(根据此答案)如下所示:

@Service
class VacuumDatabaseService(
        private val entityManager: EntityManager
) {
    fun vacuumAllTables() {
        val session = entityManager.unwrap(org.hibernate.Session::class.java)
        val sessionImpl = session as org.hibernate.internal.SessionImpl
        val connection = sessionImpl.connection()
        connection.prepareStatement("VACUUM FULL").execute()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是它抛出:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: No transactional EntityManager available
Run Code Online (Sandbox Code Playgroud)

使用以下功能注释功能@Transactional

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException

Caused by: org.postgresql.util.PSQLException: ERROR: VACUUM cannot run inside a transaction block
Run Code Online (Sandbox Code Playgroud)

可以使用以下方法,但是会感到危险的错误:

    @Transactional
    fun vacuumAllTables() {
        val session = entityManager.unwrap(org.hibernate.Session::class.java)
        val sessionImpl = …
Run Code Online (Sandbox Code Playgroud)

postgresql hibernate vacuum kotlin spring-boot

2
推荐指数
1
解决办法
116
查看次数

标签 统计

hibernate ×2

java ×1

jdbc ×1

jpa ×1

kotlin ×1

orm ×1

postgresql ×1

spring-boot ×1

vacuum ×1