Spring Boot hibernate gradle字节码增强

问题描述 投票:8回答:1

我正在试图勉强地将hibernate OneToOne关系工作。

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

即使这是一个记录良好的问题/功能,并且有很多好的读取,我已经意识到他们中的大多数正在处理maven。

幸运的是,有一个Gradle插件可以用于这个特殊的原因,即使documentation不是最好的,我发现this post也处理相同的问题,它似乎是一个非常简单的解决方案。

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

./gradlew build

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

跳过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
}
hibernate spring-boot gradle spring-data-jpa byte-code-enhancement
1个回答
0
投票

根据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也是为了解决你原来的一对一懒惰初始化的问题。

© www.soinside.com 2019 - 2024. All rights reserved.