Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nfriaa/hibernate-tutorial1

Hibernate tutorial 1 : Basic Configuration
https://github.com/nfriaa/hibernate-tutorial1

configuration hibernate hibernate5 java maven mysql

Last synced: about 1 hour ago
JSON representation

Hibernate tutorial 1 : Basic Configuration

Awesome Lists containing this project

README

        

# hibernate-tutorial1
Hibernate Basic configuration

[![contributions welcome](https://img.shields.io/badge/contributions-welcome-orange.svg?style=flat)](https://github.com/nfriaa/hibernate-tutorial1/issues) [![Travis](https://img.shields.io/travis/rust-lang/rust.svg)](https://github.com/nfriaa/hibernate-tutorial1) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/nfriaa/hibernate-tutorial1/blob/master/LICENSE)

## Description
A sample code to configure maven project and test connection to MySQL database and execute some queries using Hibernate.
* JavaSE 8
* Hibernate 5
* Maven 4
* MySQL 5

## 1. Create database
```sql
CREATE DATABASE `persist_db` /*!40100 DEFAULT CHARACTER SET utf8 */
```

## 2. Create tables
```sql
CREATE TABLE `persist_db`.`product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `persist_db`.`test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

## 3. Create maven project and add dependencies
```xml

mysql
mysql-connector-java
6.0.6

org.hibernate
hibernate-core
5.2.11.Final

```

## 4. Create the hibernate config file 'hibernate.cfg.xml'
```xml


org.hibernate.dialect.MySQLDialect
jdbc:mysql://localhost:3306/persist_db?useTimezone=true&serverTimezone=UTC
root

```

## 5. Create HibernateUtil.java
Hibernate Utility class with a convenient method to get Session Factory.

## 6. Create main Application class
* a class whith main method to test connection
* execute some queries in SQL mode (NativeQuery) : session.createNativeQuery() method
* select, insert... and read results