5

In gradle you can achieve it using:

apply plugin: 'enhance'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.hibernate:hibernate-gradle-plugin:VERSION'
    }
}
dependencies {
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-[SPEC-VERSION]-api', version: '[IMPL-VERSION]'
    compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: 'VERSION'
}

What if instead of running the project through Gradle, I want to run my main class directly through Intellij (shift-F10). Is it possible to also perform build-time bytecode instrumentation just before the application run? How should I achieve this?

Yudhistira Arya
  • 3,491
  • 6
  • 25
  • 42
  • I can't seem to setup the plugin at all... so in case you'd want to help, please check out my question: http://stackoverflow.com/questions/35552109/how-to-setup-hibernate-gradle-plugin-for-bytecode-enhancement – kaqqao Feb 22 '16 at 12:44
  • 1
    Not a Gradle expert, but in Maven you can achieve this by adding a *Run Maven Goal* task under the *Before launch* section (towards the bottom of the Run Configuration window); similarly I suppose if your enhance plugin is bound to a certain task, you can use *Run Gradle task* option to add it as a pre-launch action – Janaka Bandara Oct 21 '20 at 01:14

1 Answers1

0

Hibernate does "bytecode instrumentation" at runtime, so you don't have to do anything special for it to happen.

Actually it is not bytecode instrumentation, which means changing existing classes, but proxying, which means, the existing classes are used by classes that get generated on the fly.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Sorry for the confusion, what I mean is build-time bytecode enhancement as described in this section of the documentation: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#entityentry-lookup-buildtime-instrument – Yudhistira Arya Feb 23 '15 at 07:11
  • Interesting, didn't know this was possible. But you don't use that with your gradle file either ... – Jens Schauder Feb 23 '15 at 07:48
  • The gradle plugin mentioned in the docs will enhance the entity class during build time. Basically the question is that I want to achieve the same thing before Intellij run (just for fun only :)) – Yudhistira Arya Feb 24 '15 at 15:16
  • darn, completely missed that little plugin. Not an intellij expert here, but if you import the project based on the gradle plugin, it should actually use gradle to build everything, so this might just do what you want. – Jens Schauder Feb 24 '15 at 19:09
  • Yes it's doable that way, but in Intellij, running your program through gradle is a bit slow. – Yudhistira Arya Feb 25 '15 at 14:50
  • 3
    "byte code instrumentation" and "run time creation of proxies via reflection" are two completely different things and people should investigate resources in understanding the difference. Byte code enhancement offers many performance enhancing features (some would say mandatory performance features). Proxying really only provides lazy loading of relationships and it does that with many caveats that explain the large 'caveat' sections in the Hibernate docs. JDO/JPA implementations like DataNucleus have benefited from byte code enhancement since 2006 but it's less used in Hibernate circles. – Volksman Oct 23 '17 at 01:55