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

https://github.com/vivek-dhamanemath/hibernate-jpa-demo

Demo on Java Persistence API
https://github.com/vivek-dhamanemath/hibernate-jpa-demo

Last synced: 3 months ago
JSON representation

Demo on Java Persistence API

Awesome Lists containing this project

README

        

# Hibernate JPA Project

## Overview
This project demonstrates the use of Java Persistence API (JPA) with Hibernate as the implementation. It includes basic CRUD operations (Create, Read, Update, Delete) on a `Player` entity.

## Project Structure
- `src/main/java/com/hibernate/JPA/App.java`: The main application file where the EntityManager is created and CRUD operations are performed.
- `src/main/resources/META-INF/persistence.xml`: Configuration file for JPA.

## Java Persistence API (JPA)
JPA is a specification for accessing, persisting, and managing data between Java objects and a relational database. It provides a platform-independent and standardized way to interact with databases.

### Why Use JPA?
- **Productivity**: Reduces boilerplate code and increases developer productivity.

### JPA in Hibernate
Hibernate is one of the most popular JPA implementations. It provides additional features and optimizations on top of the JPA specification.

## Usage in This Project
In this project, we use JPA to manage `Player` entities. The `App.java` file demonstrates how to:
- Create an `EntityManagerFactory` and `EntityManager`.
- Begin a transaction.
- Persist entities to the database.
- Commit the transaction.

### Example Code
```java
package com.hibernate.JPA;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class App
{
public static void main( String[] args )
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mysql-config");
EntityManager em = emf.createEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();

Player player1 = new Player();
player1.setPlayerId(1);
player1.setPlayerName("Virat");
player1.setPlayerAge(35);

Player player2 = new Player();
player2.setPlayerId(2);
player2.setPlayerName("Dhoni");
player2.setPlayerAge(42);

Player player3 = new Player();
player3.setPlayerId(3);
player3.setPlayerName("Rohit");
player3.setPlayerAge(39);

Player player4 = new Player();
player4.setPlayerId(4);
player4.setPlayerName("Rahul");
player4.setPlayerAge(29);

// Save
em.persist(player1);
em.persist(player2);
em.persist(player3);
em.persist(player4);

et.commit();
em.close();
emf.close();
}
}
```

## META-INF Directory and `persistence.xml`
The `META-INF` directory contains the `persistence.xml` file, which is the configuration file for JPA. It defines the persistence unit and database connection details.

### Example `persistence.xml`
```xml


org.hibernate.jpa.HibernatePersistenceProvider
com.hibernate.JPA.Player








```

### Explanation
- ``: Defines a persistence unit with the name `mysql-config`.
- ``: Specifies the JPA provider (Hibernate in this case).
- ``: Lists the entity classes managed by this persistence unit.
- ``: Contains database connection properties and Hibernate-specific settings.

## Conclusion
This project provides a basic example of using JPA with Hibernate to perform CRUD operations on a database. The `persistence.xml` file is crucial for configuring the database connection and JPA provider.