Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nfriaa/hibernate-tutorial3
Hibernate tutorial 3 : Hibernate Annotations
https://github.com/nfriaa/hibernate-tutorial3
annotations hibernate hibernate-annotations hibernate5 java maven mysql
Last synced: about 1 hour ago
JSON representation
Hibernate tutorial 3 : Hibernate Annotations
- Host: GitHub
- URL: https://github.com/nfriaa/hibernate-tutorial3
- Owner: nfriaa
- License: mit
- Created: 2017-10-02T23:02:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-18T17:40:29.000Z (about 7 years ago)
- Last Synced: 2024-11-15T19:42:13.024Z (2 months ago)
- Topics: annotations, hibernate, hibernate-annotations, hibernate5, java, maven, mysql
- Language: Java
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hibernate-tutorial3
Hibernate Annotations[![contributions welcome](https://img.shields.io/badge/contributions-welcome-orange.svg?style=flat)](https://github.com/nfriaa/hibernate-tutorial3/issues) [![Travis](https://img.shields.io/travis/rust-lang/rust.svg)](https://github.com/nfriaa/hibernate-tutorial3) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/nfriaa/hibernate-tutorial3/blob/master/LICENSE)
## Description
A sample code to use Hibernate in **Annotations** mode
* JavaSE 8
* Hibernate 5 / Annotations
* Maven 4
* MySQL 5## 1. Database (the same in Tutorial 2)
We will use the same database / table## 2. Maven "pom.xml" dependencies
```xml
mysql
mysql-connector-java
6.0.6
org.hibernate
hibernate-core
5.2.11.Final
```
## 3. Changes in "hibernate.cfg.xml"
```xml
org.hibernate.dialect.MySQL5InnoDBDialect
jdbc:mysql://localhost:3306/persist_db?useTimezone=true&serverTimezone=UTC
root
true
false
update
```
* hibernate.hbm2ddl.auto : update => update the database schema with defined entities
* hibernate.hbm2ddl.auto : create => creates the schema necessary for defined entities, destroying any previous data## 3. No "Product.hbm.xml" file
There is no "Product.hbm.xml" mapping file. To do mapping we will use Hibernate **Annotations** instead## 4. Annotations in POJO classes "Product.java"
```java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "product")
public class Product
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private int id;@Column(name = "name", length = 255, nullable = true)
private String name;@Column(name = "price", nullable = true)
private int price;// GETTERs and SETTERs here...
}
```
* @Entity : link to the "Product" class (Bean / POJO)
* @Table : table that will be used to persist the entity in the database
* @Id : primary key
* @GeneratedValue : generation type for the primary key (ex : AUTO_INCREMENT in MySQL)
* @Column : details of the column to which a property will be mapped## 5. Class "Application.java"
Use exactly "Application.java" in Tutorial 2 to test