kaqqao
kaqqao

Reputation: 15429

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.

Upvotes: 10

Views: 10371

Answers (4)

Zephaniah Irvine
Zephaniah Irvine

Reputation: 540

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

Upvotes: 0

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153700

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.

Upvotes: 18

RaGe
RaGe

Reputation: 23657

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.

Upvotes: 5

Steve Ebersole
Steve Ebersole

Reputation: 9443

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'

Upvotes: 4

Related Questions