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.
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'
}
<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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With