从Java8迁移到Java11后,hibernate字节码增强停止工作

cunj1qz1  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(239)

我在kotlin项目中使用编译时hibernate字节码增强:

plugins {
  id "org.jetbrains.kotlin.jvm"
  id "org.jetbrains.kotlin.plugin.jpa"
}

apply plugin: "org.hibernate.orm"

hibernate {
  enhance {
    enableLazyInitialization = true
    enableDirtyTracking = true
  }
}

compileKotlin {
  kotlinOptions.jvmTarget = JavaVersion.VERSION_11
   doLast {
    EnhancementHelper.enhance(
            sourceSets.getByName("main"),
            project.extensions.findByType(HibernateExtension.class).enhance,
            project
    )
  }
}

我有jpa实体:

@Entity
@Table(schema = "the_schema", name = "the_table")
class TheEntity(
   var field1: String? = null
) {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  val id: Long = 0

  @OneToMany(
    cascade = [CascadeType.ALL],
    orphanRemoval = true,
    mappedBy = "otherField"
  )
  @OrderBy("id ASC")
  private val someList: MutableList<Some> = ArrayList()

  // other fields
}

应用程序启动失败,出现以下错误:

Caused by: java.lang.IllegalAccessError: Update to non-static final field 
my.package.TheEntity.someList attempted from a different method ($$_hibernate_write_someList) than the initializer method <init> 
at my.package.TheEntity.$$_hibernate_write_someList(TheEntity.kt)
at my.package.TheEntity.<init>(TheEntity.kt:29)
at my.package.TheEntity.<init>(TheEntity.kt:17)
at my.package.TheEntity.<init>(TheEntity.kt)
... 43 common frames omitted

经过一些调查,我在这里发现了类似的问题:在Java9上使用最终字段进行eclipselink静态编织
在java9中修复了相应的错误:https://bugs.java.com/bugdatabase/view_bug.do?bug_id=jdk-8157181
如果我定义 someList 作为 var 错误明显消失。
我的问题是:是否有其他的解决方案,而不是使所有字段都不是最终的(即。 var )或者字节码增强与用Java11字节码编译的kotlin不兼容?
使用的库:
kotlin:1.3.72
hibernate:5.4.25.final (gradle插件和核心)
字节-buddy:1.10.17 (用于字节码增强)
javassist:3.27.0-ga

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题