# Hibernate: How to Launch

Launching Hibernate Framework 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 \h2\bin\h2-$version.jar 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

* Download H2 zip here: https://www.h2database.com/html/download.html
* Extract it to ~\AppData\Local\Programs
* Create a shortcut for \h2\bin\h2.bat
* Place it to ~\AppData\Roaming\Microsoft\Windows\Start Menu. Now you can easily start H2 from Start menu.

## Usage

* Start H2 .bat or shortcut, web page will open with Generic H2 (Embedded) preset active;
* Use JDBC url jdbc:h2:mem:test for in-memory or jdbc:h2:~/test for local storage based db;
** If local storage is used, ~/test.mv.db file will be created and will stay there until you’ll delete it;
** ~/.h2.server.properties file will be created either way;
* Press Connect to start server and use database;
* In CMD window press CTRL+C several times to stop server.
** 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.

## Without Maven
### persistence.xml

This a proof of concept, it is recommended to use Maven.
* 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;
* Download hibernate-release-6.0.0.CR2.zip
* Extract it’s .\required internal directory.
* Into .\required\ directory copy a h2-2.2.220.jar file as well, it is in the same directory as h2.bat file from above section. It contains H2 JDBC driver.
* Create .\META-INF\persistence.xml file:
* Create .\example\entity\Person.java class:
* Create .\example\Start.java class:
* Examine file structure of a project:
* Compile and run it from Powershell:

### hibernate.properties

* Create Person and Start classes as in the previous section;
* Create .\hibernate.properties:
* Replace main method with this:
* Compile and run it as in previous section.
hibernate.properties is more concise than persistence.xml, but now you must manually list all your annotated classes while creating Configuration.

### hibernate.cfg.xml

* Create Person and Start classes as in the previous section;
* Create .\hibernate.cfg.xml:
* Use this main method:
* Compile and run it as in previous section. Usage of hibernate.cfg.xml is considered to be legacy and is not recommended for new projects.

### 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:
* Open project directory through File -> Open Folder...;
* In Explorer -> Java Projects -> Referenced Libraries press + sign and choose all .jar files from .\required\ directory;
* Now you can run this project through Ctrl+F5. In Run -> Open Configurations you will find new run configuration:

## With Maven
### Add Maven to existing project

Use any of the above non-maven combinations of java classes and configuration files, but also:
* Instead of placing .jar files into .\required directory, create this .\pom.xml:
* Place all your .java files with package-defined directories into .\src\main\java\.
* If there is a configuration .xml or .properties file in your project, move it into .\src\main\resources\. If this file by convention should itself be located in some kind of a directory, like /META-INF/, move it with it's parent directory, so new location will be: .\src\main\resources\META-INF\persistence.xml.
* Examine project file structure: .\pom.xml .\src\main\java\example\Start.java .\src\main\java\example\entity\Person.java .\src\main\resources\META-INF\persistence.xml
* Run your application with mvn clean compile exec:java command.

### 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 java in CMD, characters are displayed properly. To fix this issue add this method to your main class and call it from main:

### Spring Standalone

You can write a standalone application which uses Spring's Inversion of Control and Dependency Injection capabilities but doesn't use MVC web module.
* Create .\pom.xml:
* Create .\src\main\java\myapp\App.java:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\resources\META-INF\persistence.xml:
* Create .\src\main\java\myapp\ApplicationConfig.java:
* Create .\src\main\java\myapp\Printer.java:
* Run your app with mvn clean compile exec:java. Basic read/write operations will be performed.

### Spring Boot Standalone

You can write a standalone application with Spring Boot. It provides you Inversion of Control and Dependency Injection capabilities but doesn't use MVC web module.
* Create .\pom.xml:
* Create .\src\main\resources\application.properties:
* Create .\src\main\java\myapp\DemoApplication.java:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\Printer.java:
* Run your app with mvn clean compile spring-boot:run. Basic read/write operations will be performed.

# Spring Web

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

## Spring Boot Rest

* Create .\pom.xml:
* Create .\src\main\java\myapp\Application.java:
* Create .\src\main\resources\application.properties:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\rest\PersonController.java:
* Create .\src\main\java\myapp\dao\PersonDao.java:
* Run your application: mvn clean spring-boot:run
* Create new person by accessing REST endpoint with CURL or Powershell:
* Get a list of all persons by accessing REST endpoint with CURL or Powershell (or open this link in a browser):
* You will see output:

## 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.
* Create .\pom.xml:
* Create .\src\main\webapp\WEB-INF\web.xml:
* Create .\src\main\webapp\WEB-INF\dispatcher-servlet.xml:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\rest\PersonResource.java:
* Create .\src\main\java\myapp\dao\PersonDao.java:
* Launch server: mvn clean jetty:run:
* Get all people from database, add new one and get all once again:

## Spring Java-config Rest

* Create .\pom.xml:
* Create .\src\main\java\myapp\config\WebInit.java:
* Create .\src\main\java\myapp\config\WebConfig.java:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\rest\PersonResource.java:
* Create .\src\main\java\myapp\dao\PersonDao.java:
* Launch web application and perform read/write operations through Rest endpoint:

# 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:
* Create .\pom.xml:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\rest\DemoApplication.java:
* Create .\src\main\java\myapp\rest\PersonResource.java:
* Create .\src\main\resources\META-INF\persistence.xml:
* Create .\src\main\java\myapp\dao\PersonDao.java:
* Create .\src\main\webapp\WEB-INF\glassfish-resources.xml:
* Start MySQL server, start embedded Glassfish with your webapp deployed:
* Access REST endpoint to perform read/write operations:
* To stop Glassfish, send stop-domain command via web-request, after that press Ctrl+C in appropriate terminal window.

## Glassfish H2-Embedded Rest

* Create .\pom.xml:
* Create .\src\main\java\myapp\dao\PersonDao.java:
* Create .\src\main\java\myapp\model\Person.java:
* Create .\src\main\java\myapp\rest\DemoApplication.java:
* Create .\src\main\java\myapp\rest\PersonResource.java:
* Create .\src\main\resources\META-INF\persistence.xml:
* Create .\src\main\webapp\WEB-INF\glassfish-resources.xml:
* Start webapp in embedded Glassfish server:
* Access REST endpoint to perform read/write operations:
* To stop server, send stop-domain command via web-request, after that press Ctrl+C in appropriate terminal window.