SpringBoot hibernate gradle byte code enhancement

3.3k Views Asked by At

I am trying to mane hibernate OneToOne relationships work lazily.

My setup is spring-data-jpa using hibernate and Gradle.

Even this is a well-documented problem/feature and there is a lot of good reads out there, I have come to realise that most of them are dealing with maven.

Fortunately, there is a Gradle plugin that can be used for this particular reason and even though the documentation is not the best, I have found this post that also deals with the same issue and it seems like a pretty straightforward solution.

After I implemented the changes needed in my Gradle script, it seemed like hibernate still loads the OneToOne relationships eagerly, so I tried to perform

./gradlew build

and noticed that the following message is printed:

Skipping Hibernate bytecode enhancement since no feature is enabled

according to the source code of the plugin, this message is shown when the enhancement is not enabled, which is not the case as I am using:

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

So my question is has anyone got this to work properly? If yes can you please point me to some tutorial/guide in order to achieve it? I am using an interceptor in an integration test to verify the number of queries hibernate is using as well as monitoring the sql logs.

My gradle file looks like:

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
}
2

There are 2 best solutions below

1
On

According to sources it should work, if you have created your build.gradle file properly. In the latest docs it says following.

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

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

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

Make sure you have the plugin added in the buildscript section, and if you can post your build.gradle it might be more useful as well.

On a sidenote have a look at this question also, in order to resolve your original question of one-to-one lazy initialization.

0
On

Refer to following document https://docs.jboss.org/hibernate/orm/5.3/topical/html_single/bytecode/BytecodeEnhancement.html

The Right syntax is as follows

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