|
| 1 | +<!-- |
| 2 | + ~ Copyright 2004 - 2025 Red Hat, Inc. |
| 3 | + ~ |
| 4 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + ~ you may not use this file except in compliance with the License. |
| 6 | + ~ You may obtain a copy of the License at |
| 7 | + ~ |
| 8 | + ~ http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + ~ |
| 10 | + ~ Unless required by applicable law or agreed to in writing, software |
| 11 | + ~ distributed under the License is distributed on an "AS IS" basis, |
| 12 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + ~ See the License for the specific language governing permissions and |
| 14 | + ~ limitations under the License. |
| 15 | + --> |
| 16 | + |
| 17 | +[](https://tools.hibernate.org) |
| 18 | + |
| 19 | +# Hibernate Tools Gradle : 5 Minute Tutorial |
| 20 | + |
| 21 | +The best way to get to know the Hibernate Tools Gradle plugin is to start to use it. |
| 22 | +Hence we will provide a quick tutorial that gives you the first taste of it. |
| 23 | +Before tackling this tutorial, make sure you have the [Gradle](https://gradle.org) build tool |
| 24 | +[installed](https://gradle.org/install/) and available on your machine. |
| 25 | + |
| 26 | +## Create a Gradle Java Project |
| 27 | + |
| 28 | +Let’s assume in this case that we start off with a very simple default Gradle Java application |
| 29 | +that we create from a command-line window with the instruction below. |
| 30 | + |
| 31 | +```shell |
| 32 | +gradle init --type java-application --dsl groovy |
| 33 | +``` |
| 34 | + |
| 35 | +Gradle will ask you some details about your application. The conversation is shown below |
| 36 | +for completenes but of course you can make your own choices. |
| 37 | + |
| 38 | +```shell |
| 39 | +Enter target Java version (min: 7, default: 21): |
| 40 | + |
| 41 | +Project name (default: 5-minute-tutorial): |
| 42 | + |
| 43 | +Select application structure: |
| 44 | + 1: Single application project |
| 45 | + 2: Application and library project |
| 46 | +Enter selection (default: Single application project) [1..2] 1 |
| 47 | + |
| 48 | +Select test framework: |
| 49 | + 1: JUnit 4 |
| 50 | + 2: TestNG |
| 51 | + 3: Spock |
| 52 | + 4: JUnit Jupiter |
| 53 | +Enter selection (default: JUnit Jupiter) [1..4] 4 |
| 54 | + |
| 55 | +Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] |
| 56 | + |
| 57 | + |
| 58 | +> Task :init |
| 59 | +Learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.13/samples/sample_building_java_applications.html |
| 60 | + |
| 61 | +BUILD SUCCESSFUL in 19s |
| 62 | +1 actionable task: 1 executed |
| 63 | +``` |
| 64 | + |
| 65 | +Now you should see two folders along with a number of Gradle specific files that have |
| 66 | +been created. It is beyond the scope of this short tutorial to explain all these artefacts. |
| 67 | +We will focus mainly on the `build.gradle` file in the `app` folder. But to make the plugin work |
| 68 | +you will need to modify the file `gradle.properties` that was generated in the root folder. |
| 69 | + |
| 70 | +## Disable the Configuration Cache |
| 71 | + |
| 72 | +Currently the Hibernate Tools Gradle plugin does not support the configuration cache. |
| 73 | +As the Gradle init task generates a `gradle.properties` file in the root folder that |
| 74 | +explicitly enables the use of the configuration cache, you will need to comment out |
| 75 | +or delete the line where this happens. |
| 76 | + |
| 77 | +```properties |
| 78 | +#org.gradle.configuration-cache=true |
| 79 | +``` |
| 80 | + |
| 81 | +You could also get rid of the entire `gradle.properties` file as we don't need it for the purpose of this |
| 82 | +tutorial. Now we can tackle the `build.gradle` file. |
| 83 | + |
| 84 | +## Modify the generated `app\build.gradle` file |
| 85 | + |
| 86 | +We have to specify the use of the Gradle plugin in the `plugin` section of the `build.gradle` file. |
| 87 | +So we add `id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final'` to that section. |
| 88 | + |
| 89 | +```groovy |
| 90 | +... |
| 91 | +plugins { |
| 92 | + ... |
| 93 | + id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final' |
| 94 | +} |
| 95 | +... |
| 96 | +``` |
| 97 | + |
| 98 | +Also we need to depend on the java library containing the [H2 database]() drivers. |
| 99 | +This is done in the `dependencies` section of the `gradle.build` file, |
| 100 | +to which we add `implementation 'com.h2database:h2:2.3.232'`. |
| 101 | + |
| 102 | +```groovy |
| 103 | +... |
| 104 | +dependencies { |
| 105 | + ... |
| 106 | + implementation 'com.h2database:h2:2.3.232' |
| 107 | +} |
| 108 | +... |
| 109 | +``` |
| 110 | + |
| 111 | +You could as an alternative also replace the entire `gradle.build` file |
| 112 | +with the contents as shown below. |
| 113 | + |
| 114 | +```groovy |
| 115 | +plugins { |
| 116 | + id 'application' |
| 117 | + id 'org.hibernate.tool.hibernate-tools-gradle' version '6.6.20.Final' |
| 118 | +} |
| 119 | +
|
| 120 | +repositories { |
| 121 | + mavenCentral() |
| 122 | +} |
| 123 | +
|
| 124 | +dependencies { |
| 125 | + implementation('com.h2database:h2:2.3.232') |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +With this in place, we need to make sure that the Hibernate Tools Gradle plugin knows where |
| 130 | +to find the database from which to generate the artefacts. This is done by spefifying the |
| 131 | +Hibernate properties in the file `hibernate.properties`. |
| 132 | + |
| 133 | +## Specify the Hibernate Properties |
| 134 | + |
| 135 | +For the purpose of this tutorial introduction, let's assume that you have a database running, e.g. |
| 136 | +[H2 Sakila database](https://github.com/hibernate/sakila-h2) reacheable at the following JDBC URL: |
| 137 | +`jdbc:h2:tcp://localhost/./sakila`. |
| 138 | + |
| 139 | +With this set up, the `hibernate.properties` file should contain the properties as specified below. |
| 140 | + |
| 141 | +```properties |
| 142 | +hibernate.connection.driver_class=org.h2.Driver |
| 143 | +hibernate.connection.url=jdbc:h2:tcp://localhost/./sakila |
| 144 | +hibernate.connection.username=sa |
| 145 | +hibernate.default_catalog=SAKILA |
| 146 | +hibernate.default_schema=PUBLIC |
| 147 | +``` |
| 148 | + |
| 149 | +For the file to be found by the plugin, add it as a resource to the project in the |
| 150 | +`app/src/main/resources` subfolder. |
| 151 | + |
| 152 | +## Run the Reverse Engineering |
| 153 | + |
| 154 | +With all the previous elements in place, generating the Java classes from the Sakila database |
| 155 | +becomes as simple as issuing `./gradlew generateJava` in your command line window. |
| 156 | + |
| 157 | +```shell |
| 158 | +me@machine 5-minute-tutorial % ./gradlew generateJava |
| 159 | + |
| 160 | +> Task :app:generateJava |
| 161 | +Starting Task 'generateJava' |
| 162 | +Creating Java exporter |
| 163 | +Loading the properties file : path/to/5-minute-tutorial/app/src/main/resources/hibernate.properties |
| 164 | +Properties file is loaded |
| 165 | +Starting Java export to directory: path/to/5-minute-tutorial/app/generated-sources... |
| 166 | +... |
| 167 | +Java export finished |
| 168 | +Ending Task 'generateJava' |
| 169 | +``` |
| 170 | + |
| 171 | +By default, you will find the files in the folder `app/generated-sources`. |
| 172 | + |
| 173 | +``` |
| 174 | +koen@Lateralus generated-sources % ls |
| 175 | +Actor.java City.java Film.java FilmCategory.java Inventory.java Rental.java |
| 176 | +Address.java Country.java FilmActor.java FilmCategoryId.java Language.java Staff.java |
| 177 | +Category.java Customer.java FilmActorId.java FilmText.java Payment.java Store.java |
| 178 | +``` |
| 179 | + |
| 180 | +Congratulations! You have succesfully created Java classes for the Sakila database... Now it's |
| 181 | +probably time to dive somewhat deeper in the available functionality. |
0 commit comments