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: 4 months ago
JSON representation
Hibernate tutorial 1 : Basic Configuration
- Host: GitHub
- URL: https://github.com/nfriaa/hibernate-tutorial1
- Owner: nfriaa
- License: mit
- Created: 2017-09-28T15:34:27.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-10-18T17:38:58.000Z (over 7 years ago)
- Last Synced: 2025-01-16T07:55:46.990Z (6 months ago)
- Topics: configuration, hibernate, hibernate5, java, maven, mysql
- Language: Java
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hibernate-tutorial1
Hibernate Basic configuration[](https://github.com/nfriaa/hibernate-tutorial1/issues) [](https://github.com/nfriaa/hibernate-tutorial1) [](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
```xmlmysql
mysql-connector-java
6.0.6org.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