Hibernate: How to Launch

Launching can be a tricky task. This page provides you with multiple ways to do it, it can be useful to you if you're trying to run your first application with Hibernate and it just crashes, or if you need Proof-of-Concept or an archetype of project with specific configuration. Every sample application on this page maintains a simple goal - to read something from database, which is a proof of Hibernate up and running.

All projects on this page use H2 database.

Just pick a desired configuration option, create specified files and you're good to go.

= H2 Database =

Ignore this section if you're going to use Maven. Otherwise, download H2 zip, find  file in it and proceed to one of the Standalone non-Maven sections on this page.

If you want to know how to use H2 database without writing an application, this section shows how to do it.

Installation

 * 1) Download H2 zip here: https://www.h2database.com/html/download.html
 * 2) Extract it to
 * 3) Create a shortcut for
 * 4) Place it to  . Now you can easily start H2 from Start menu.

Usage

 * 1) Start H2 .bat or shortcut, web page will open with   preset active;
 * 2) Use JDBC url   for in-memory or   for local storage based db;
 * 3) * If local storage is used,  file will be created and will stay there until you’ll delete it;
 * 4) *  file will be created either way;
 * 5) Press Connect to start server and use database;
 * 6) In CMD window press CTRL+C several times to stop server.
 * 7) * You don't have to run/stop H2 server manually if it's library is in classpath of your application.

= Standalone =

You can launch Hibernate in offline desktop application. Maybe it is a CLI app, or maybe it uses GUI frameworks like Swing or JavaFX, it doesn't matter - you can bootstrap Hibernate into it. This section shows how to do it.

Also Hibernate itself can be configured with persistence.xml, hibernate.properties or hibernate.cfg.xml file. So, we will cover every of this configuration options.

persistence.xml
This a proof of concept, it is recommended to use Maven.

 Create  file:
 * 1) Choose a 6.0.0.CR2 version here: https://sourceforge.net/projects/hibernate/files/hibernate-orm/; - Since then Hibernate release bundle download is no longer provided, so this is the latest Hibernate version you can download as a bundle;
 * 2) Download
 * 3) Extract it’s   internal directory.
 * 4) Into   directory copy a   file as well, it is in the same directory as   file from above section. It contains H2 JDBC driver.

Create  class:

Create  class:

Examine file structure of a project: .\example\Start.java .\example\entity\Person.java .\META-INF\persistence.xml .\required\antlr4-runtime-4.9.1.jar .\required\byte-buddy-1.12.7.jar .\required\classmate-1.5.1.jar .\required\h2-2.2.220.jar .\required\hibernate-commons-annotations-6.0.0.CR1.jar .\required\hibernate-core-6.0.0.CR2.jar .\required\istack-commons-runtime-4.0.1.jar .\required\jakarta.activation-2.0.1.jar .\required\jakarta.activation-api-2.0.1.jar .\required\jakarta.inject-api-2.0.0.jar .\required\jakarta.persistence-api-3.0.0.jar .\required\jakarta.transaction-api-2.0.0.jar .\required\jakarta.xml.bind-api-3.0.1.jar .\required\jandex-2.4.2.Final.jar .\required\jaxb-core-3.0.2.jar .\required\jaxb-runtime-3.0.2.jar .\required\jboss-logging-3.4.3.Final.jar .\required\txw2-3.0.2.jar Compile and run it from Powershell:

javac --% -cp .;required\*;.\classes -d classes example\entity\*.java example\*.java java --% -cp .;required\*;.\classes example.Start 
 * is needed to prevent Powershell from interpreting arguments to  and   executables.

hibernate.properties

 * 1) Create   and   classes as in the previous section;
 * 2) Create  :

 Replace  method with this:

<ol start="5" style="list-style-type: decimal;"> <li>Compile and run it as in previous section.</li></ol>

is more concise than, but now you must manually list all your annotated classes while creating Configuration.

hibernate.cfg.xml

 * 1) Create   and   classes as in the previous section;
 * 2) Create  :

<ol start="3" style="list-style-type: decimal;"> <li>Use this  method:</li></ol>

<ol start="4" style="list-style-type: decimal;"> <li>Compile and run it as in previous section. Usage of  is considered to be legacy and is not recommended for new projects.</li></ol>

Open in VSCode
It's simple to run a maven project in IDE, but if you'll want to run one of above combinations, here is what you need to do:
 * 1) Open project directory through  ;
 * 2) In   press   sign and choose all   files from   directory;
 * 3) Now you can run this project through  . In   you will find new run configuration:

Add Maven to existing project
Use any of the above non-maven combinations of java classes and configuration files, but also: <ol> <li>Instead of placing  files into   directory, create this  :</li>

<li>Place all your .java files with package-defined directories into. </li> <li>If there is a configuration  or   file in your project, move it into. If this file by convention should itself be located in some kind of a directory, like, move it with it's parent directory, so new location will be:  .</li> <li>Examine project file structure:

</li> <li>Run your application with  command.</li>
 * If you will need to run this goal in VSCode, add following plugin section after  tag, otherwise you may be not able to choose exec:java goal:

</ol>

Maven Encoding Issue
You may encounter your application fails to output some unicode characters to console under Windows platform when you run it with Maven, even though when you execute your app with VSCode internal functionality or manually with  in CMD, characters are displayed properly. To fix this issue add this method to your main class and call it from :

Spring Standalone
You can write a standalone application which uses Spring's  and  capabilities but doesn't use  web module.

<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li> Create :</li>

<li>Create :</li> <li> Run your app with. Basic read/write operations will be performed.</li> </ol>

Spring Boot Standalone
You can write a standalone application with. It provides you  and  capabilities but doesn't use  web module.

<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Run your app with. Basic read/write operations will be performed.</li> </ol>

= Spring Web =

Projects in this category are web applications built with Spring Framework.

Spring Boot Rest
<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li> <li>Run your application: </li> <li>Create new person by accessing REST endpoint with CURL or Powershell:</li> curl.exe -X POST -H "Content-Type: application/json" -d '{"name":"Вася Петечкин"}' http://localhost:8080/person/new Invoke-WebRequest -Uri "http://localhost:8080/person/new" -Method POST -Body '{"name":"Вася Петечкин"}' -ContentType "application/json; charset=UTF-8" <li>Get a list of all persons by accessing REST endpoint with CURL or Powershell (or open this link in a browser):</li> curl.exe -X GET http://localhost:8080/person/all Invoke-WebRequest -Uri "http://localhost:8080/person/all" -Method GET | select -Expand Content <li>You will see output:</li> [{"id":1,"name":"Вася Петечкин"},{"id":2,"name":"Петя Васечкин"}] </ol>
 * Note how you'll need to explicitly state encoding if using Powershell.

Spring MVC XML-config Rest
Spring Framework can be used without Spring Boot and with XML-configuration. This project uses REST endpoint to read/write data with Hibernate.

<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li> <li>Launch server: :</li> <li>Get all people from database, add new one and get all once again:</li> You will see read/write operations work as expected. </ol>

Spring Java-config Rest
<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Launch web application and perform read/write operations through Rest endpoint:</li>

</ol>

= Jakarta EE =

Glassfish MySQL Rest
Prerequisites: standalone MySQL server.

This project differs from other sample applications on this page: instead of using embedded database, it needs separate MySQL server instance up and running. If you don't have standalone MySQL server installed, you wouldn't be able to launch this application, otherwise follow these instructions:

<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li> <li>Start MySQL server, start embedded Glassfish with your webapp deployed:</li>

<li>Access REST endpoint to perform read/write operations:</li>

<li>To stop Glassfish, send stop-domain command via web-request, after that press Ctrl+C in appropriate terminal window.</li>

</ol>

Glassfish H2-Embedded Rest
<ol> <li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Create :</li>

<li>Start webapp in embedded Glassfish server:</li>

<li>Access REST endpoint to perform read/write operations:</li>

<li>To stop server, send stop-domain command via web-request, after that press Ctrl+C in appropriate terminal window.</li>

</ol>