SpringBoot Hibernate Gradle 字节码增强

10

我正在尝试使hibernate的OneToOne关系懒加载。

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

即使这是一个有很多好文章涉及到的问题/特性,我意识到其中大部分都在处理maven。

幸运的是,有一个Gradle插件可以用于这个特定的原因,即使文档不是最好的,我已经找到了这篇帖子,它也处理了同样的问题,而且看起来是一个非常简单的解决方案。

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

./gradlew build

并注意到会输出以下消息:

Skipping Hibernate bytecode enhancement since no feature is enabled

根据插件的源代码,当未启用增强功能时,将显示此消息,但我使用的是:

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关联是否具有约束(例如非空)? - hovanessyan
这是一种可选的反向一对一关系,我正在使用带有Hibernate 5.0.12的Spring Boot 1.5.4。 在这些情况下,Hibernate会忽略获取Lazy。 - maxsap
你是否按照官方文档(示例5)中所描述的拥有所有部分?https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/pc/BytecodeEnhancement.html - hovanessyan
你正在使用哪个版本的Gradle插件? - hovanessyan
@maxsap,你能发布你的build.gradle文件或者更多相关代码吗?你在哪里应用插件和配置扩展,它们中的任何一个被嵌套在其他东西中吗? - wasyl
显示剩余4条评论
2个回答

0
根据来源的说法,如果您正确创建了build.gradle文件,则应该可以正常工作。在最新的文档中,它如下所述。
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文件。 - maxsap

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接