Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Hibernate Gradle plugin for bytecode enhancement?

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.

like image 212
kaqqao Avatar asked Feb 22 '16 11:02

kaqqao


3 Answers

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.

like image 175
Vlad Mihalcea Avatar answered Nov 20 '22 20:11

Vlad Mihalcea


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.

like image 31
RaGe Avatar answered Nov 20 '22 19:11

RaGe


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'
like image 4
Steve Ebersole Avatar answered Nov 20 '22 19:11

Steve Ebersole