【问题标题】:SpringBoot hibernate gradle byte code enhancementSpring Boot hibernate gradle 字节码增强
【发布时间】:2018-08-08 10:05:42
【问题描述】:

我正试图让 OneToOne 关系懒惰地工作。

我的设置是使用 hibernate 和 Gradle 的 spring-data-jpa。

即使这是一个有据可查的问题/功能,并且有很多很好的读物,我已经意识到他们中的大多数都在处理 maven。

幸运的是,有一个 Gradle 插件可以用于这个特殊原因,即使 documentation 不是最好的,我发现 this post 也可以处理同样的问题,而且看起来很简单解决方案。

在我的 Gradle 脚本中实现所需的更改后,似乎 hibernate 仍然急切地加载 OneToOne 关系,所以我尝试执行

./gradlew 构建

并注意到打印了以下消息:

由于未启用任何功能,因此跳过 Hibernate 字节码增强

根据source code of the plugin,未启用增强功能时会显示此消息,而我使用的情况并非如此:

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

所以我的问题是有人让它正常工作吗? 如果是的话,你能指点我一些教程/指南以实现它吗? 我在集成测试中使用拦截器来验证 hibernate 正在使用的查询数量以及监视 sql 日志。

我的 gradle 文件如下所示:

buildscript {
repositories { mavenCentral() }
dependencies {
   classpath 'com.github.ben-manes:gradle-versions-plugin:+'
}
dependencies {
    classpath "org.hibernate:hibernate-gradle-plugin:5.2.15.Final"
 }
}

plugins {
 id "io.spring.dependency-management" version "1.0.3.RELEASE"
}

ext { springBootVersion = '1.5.4.RELEASE' }
ext['flyway.version'] = '4.0.3'

apply plugin: 'java'
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'org.hibernate.orm'


hibernate.enhance {
  enableLazyInitialization = true
  enableDirtyTracking = true
  enableAssociationManagement = true
  enableExtendedEnhancement = true
}

jar {
  baseName = 'domain'
  version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8

repositories { mavenCentral() }


configurations.all {
 exclude module: 'logback-classic'
}

dependencies {
  compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.15.Final'
  compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.9.RELEASE'
  compile('org.springframework.boot:spring-boot-starter')
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
  compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.3'
  compile group: 'org.flywaydb', name: 'flyway-core', version: '4.0.3'
  compile group: 'org.postgresql', name: 'postgresql', version: '42.1.1'
  compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:1.12'
  compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.4.Final'
  compileOnly 'org.projectlombok:lombok:1.16.20'
  testCompile('org.springframework.boot:spring-boot-starter-test')
  testCompile 'org.hsqldb:hsqldb:2.3.3'
  testCompile group: 'org.glassfish.web', name: 'el-impl', version: '2.2.1-b05'
  compileOnly 'com.google.code.findbugs:annotations:3.0.1'
}

dependencyManagement {
  imports { mavenBom("org.springframework.boot:spring-boot- 
  dependencies:${springBootVersion}") }
}

checkstyle {
   configFile = file("../checks.xml")
   toolVersion = '7.5.1'
}

tasks.withType(FindBugs) {
  ignoreFailures true
  effort 'min'
  reportLevel 'high' // low, medium, high
  reports {
     xml {
         enabled true
     }
      html.enabled false
   }
 }

sourceSets {
    integrationTest {
     java {
         compileClasspath += main.output + test.output
         runtimeClasspath += main.output + test.output
         srcDir file('src/integration_test/java')
     }
     resources.srcDir file('src/integration_test/resources')
   }
}

task integrationTest(type: Test) {
   testClassesDir = sourceSets.integrationTest.output.classesDir
   classpath = sourceSets.integrationTest.runtimeClasspath
   outputs.upToDateWhen { false }
}

configurations {
   integrationTestCompile.extendsFrom testCompile
   integrationTestRuntime.extendsFrom testRuntime
}

【问题讨论】:

  • 我以为@OneToOne 支持fetch=LAZY?为什么需要字节码增强等? docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/... 您使用的是哪个 Hibernate 版本?您的 @OneToOne 关联是否也有约束(例如不可为空)?
  • 这是一个可选的反向一对一关系,我使用的是hibernate 5.0.12附带的spring boot 1.5.4。在这些情况下, fetch Lazy 只是从休眠中忽略
  • 你有官方文档(示例5)中描述的所有部分吗? docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/…
  • 你用的是哪个版本的gradle插件?
  • @hovanessyan 抱歉回复晚了,是的,我正在关注官方文档,我的 gradle 版本是 3.2.1

标签: hibernate spring-boot gradle spring-data-jpa byte-code-enhancement


【解决方案1】:

根据sources,如果您正确创建了build.gradle 文件,它应该可以工作。在最新的docs 中,它说如下。

ext {
    hibernateVersion = 'hibernate-version-you-want'
}

buildscript {
    dependencies {
        classpath "org.hibernate:hibernate-gradle-plugin:$hibernateVersion"
    }
}

hibernate {
    enhance {
        // any configuration goes here
    }
}

确保您已将插件添加到 buildscript 部分,如果您可以发布您的 build.gradle,它可能也会更有用。

在旁注中也可以查看question,以解决您原来的一对一延迟初始化问题。

【讨论】:

  • 我已经定义了 buildscript 部分,我已经更新了我的问题以包括 gradle 文件。
【解决方案2】:

参考以下文件 https://docs.jboss.org/hibernate/orm/5.3/topical/html_single/bytecode/BytecodeEnhancement.html

正确的语法如下

hibernate {
    enhance {
        enableLazyInitialization= true
        enableDirtyTracking = true
        enableAssociationManagement = true
        enableExtendedEnhancement = false
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2019-03-17
    • 1970-01-01
    • 2023-02-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多