How to setup Hibernate Gradle plugin for bytecode enhancement?
Asked Answered
H

4

9

Hibernate Gradle plugin is an equivalent of hibernate-enhance-maven-plugin and offers build-time code enhancements. The official docs do not mention the apply plugin: 'something' line. If I just do as the guide says I get:

Could not find method hibernate() for arguments...

I tried guessing the plugin name with apply plugin: 'enhance' (as this thread suggests) and apply plugin: 'org.hibernate.orm' (as this test suggests) but it just says the plugin with that id is unknown.

Has anyone managed to set this plugin up successfully?

My build.gradle is as follows:

allprojects {
    group 'xxx'
    version '1.0-SNAPSHOT'
}

subprojects {
    apply plugin: 'java'

    sourceCompatibility = 1.8

    repositories {
        mavenLocal()
        mavenCentral()
    }

    dependencies {
        ...
    }
}

project(':xxx-model') {
    buildscript {
       repositories {
           mavenLocal()
           mavenCentral()
       }
       dependencies {
           classpath "org.hibernate:hibernate-gradle-plugin:5.0.7.Final"
       }
    }

    apply plugin: 'org.hibernate.orm'

    hibernate {
        enhance {}
    }
}

... more unrelated project blocks here

Experimented with moving the buildscript{...} into the root, allprojects and subprojects with no useful results.

Healion answered 22/2, 2016 at 11:11 Comment(0)
P
5
apply plugin: 'org.hibernate.orm'

The plugin code indicates what you got from the test is correct. What you may be missing is a repositories section inside your buildScript section, to fetch the plugin jar from.

Plentiful answered 22/2, 2016 at 12:38 Comment(4)
I've actually tried this as well and just got "Plugin with id... not found". Do you maybe see anything misconfigured in my gradle.build? I'll give it another go in the meantime.Healion
Where in your build.gradle are you putting apply plugin: 'org.hibernate.orm' ?Plentiful
It works if and only if I place the whole plugin config into sub-project's build.gradle. If the plugin config is in the root build.gradle under project(':xxx-model'), as exemplified in the question, it says the plugin is not found. Btw, I edited the question to show where I normally would put apply plugin: 'org.hibernate.orm'Healion
Note that we can use the newer plugins syntax when hibernate.atlassian.net/browse/HHH-13354 is resolved.Kerbela
R
17

For Gradle

A complete example looks like this:

apply plugin: 'java'

repositories {
    mavenLocal()
    mavenCentral()
}

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "org.hibernate:hibernate-gradle-plugin:$hibernateVersion"
    }
}

apply plugin: 'org.hibernate.orm'

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

dependencies {
    compile 'org.hibernate:hibernate-core:$hibernateVersion'
}

For Maven

<plugin>
    <groupId>org.hibernate.orm.tooling</groupId>
    <artifactId>hibernate-enhance-maven-plugin</artifactId>
    <version>${hibernate.version}</version>
    <executions>
        <execution>
            <configuration>
                <enableLazyInitialization>true</enableLazyInitialization>
                <enableDirtyTracking>true</enableDirtyTracking>
                <enableAssociationManagement>true</enableAssociationManagement>
                <enableExtendedEnhancement>false</enableExtendedEnhancement>
            </configuration>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

After you set the enhance plugin, Hibernate recompiles the entity classes and changes the bytecode like this:

public void setDetails(PostDetails details) {
    this.$$_hibernate_write_details(details);
}
 
public void $$_hibernate_write_details(PostDetails details) {
    if (!Objects.deepEquals(details, this.details)) {
        this.$$_hibernate_trackChange("details");
    }
 
    this.details = details;
}

public void $$_hibernate_trackChange(String property) {
    if (this.$$_hibernate_tracker == null) {
        this.$$_hibernate_tracker = new SimpleFieldTracker();
    }
 
    this.$$_hibernate_tracker.add(property);
}

The $$_ methods are Hibernate-specific and contain the entity instrumentation logic.

Rhinelandpalatinate answered 23/2, 2016 at 15:34 Comment(5)
Hi, with hibernate-core, do i need: hibernate-entitymanager, hibernate-validator, hibernate-validator-annotation-processor, hibernate-commons-annotations, hibernate-jpa-2.1-api and hibernate-ehcache dependencies ?Henceforward
Those dependencies are like plug-ins. They provide additional functionality.Rhinelandpalatinate
Thanks Vlad, how do i know if i need them or hibernate-core is enough?Henceforward
The Hibernate User Guide explains what's the goal of each module. Enjoy reading it as much as I enjoyed writing it.Rhinelandpalatinate
For Gradle users, use implementation instead of compile, beacause it is @Depricated.Windpipe
P
5
apply plugin: 'org.hibernate.orm'

The plugin code indicates what you got from the test is correct. What you may be missing is a repositories section inside your buildScript section, to fetch the plugin jar from.

Plentiful answered 22/2, 2016 at 12:38 Comment(4)
I've actually tried this as well and just got "Plugin with id... not found". Do you maybe see anything misconfigured in my gradle.build? I'll give it another go in the meantime.Healion
Where in your build.gradle are you putting apply plugin: 'org.hibernate.orm' ?Plentiful
It works if and only if I place the whole plugin config into sub-project's build.gradle. If the plugin config is in the root build.gradle under project(':xxx-model'), as exemplified in the question, it says the plugin is not found. Btw, I edited the question to show where I normally would put apply plugin: 'org.hibernate.orm'Healion
Note that we can use the newer plugins syntax when hibernate.atlassian.net/browse/HHH-13354 is resolved.Kerbela
C
4

The official documentation (starting in 5.1) for bytecode enhancement is actually the User Guide. That document still does not mention applying this plugin, as we assume some basic Gradle knowledge to use Gradle ;) But it probably is better for that User Guide section to specify how to apply the plugin also (I created an issue in Jira for that).

In the meantime, the name of the id of the plugin is org.hibernate.orm, so you'd add:

apply: 'org.hibernate.orm'
Calk answered 22/2, 2016 at 12:44 Comment(0)
B
0

A Gradle Kotlin DSL example

Specify the location of gradle plugins in settings.gradle.kts file.

pluginManagement {
    repositories {
        gradlePluginPortal()
    }
}

In your build.gradle file:

plugins {
    id("org.hibernate.orm") version "6.5.2.Final"
}

hibernate {
    enhancement {
        enableLazyInitialization = true
    }
}

This documentation from Gradle would be helpful if you'd apply those settings to a multi-build project

Beka answered 8/8, 2024 at 1:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.