我正在尝试使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
}