8

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.

Community
  • 1
  • 1
kaqqao
  • 10,809
  • 4
  • 50
  • 101

3 Answers3

16

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.

Vlad Mihalcea
  • 103,297
  • 39
  • 432
  • 788
  • 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 ? – Arash Jan 04 '21 at 07:11
  • Those dependencies are like plug-ins. They provide additional functionality. – Vlad Mihalcea Jan 04 '21 at 07:36
  • Thanks Vlad, how do i know if i need them or `hibernate-core` is enough? – Arash Jan 04 '21 at 10:34
  • The [Hibernate User Guide](https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html) explains what's the goal of each module. Enjoy reading it as much as I enjoyed writing it. – Vlad Mihalcea Jan 04 '21 at 13:50
  • For Gradle users, use `implementation` instead of `compile`, beacause it is `@Depricated`. – stefan-dan May 04 '21 at 08:43
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.

RaGe
  • 19,186
  • 10
  • 54
  • 86
  • 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. – kaqqao Feb 22 '16 at 15:07
  • Where in your build.gradle are you putting `apply plugin: 'org.hibernate.orm'` ? – RaGe Feb 22 '16 at 17:13
  • 1
    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'` – kaqqao Feb 22 '16 at 22:43
  • Note that we can use the newer plugins syntax when https://hibernate.atlassian.net/browse/HHH-13354 is resolved. – debuglevel Aug 21 '19 at 15:18
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'
Steve Ebersole
  • 8,735
  • 2
  • 42
  • 44