Skip to content

Manwatkar27/Hibernate-Example-using-JPA-and-MySQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hibernate Example Using JPA and MySQL 🎵

A simple console application demonstrating how to use Hibernate and JPA to save Java objects to a MySQL database. This project is a beginner-friendly example of basic Object-Relational Mapping (ORM).

🛠️ Technologies Used

Java 17

Hibernate ORM 6.2

JPA (Jakarta Persistence)

MySQL 8.0

Maven

✨ Features

JPA Annotations: Uses @Entity, @Table, @Id, and @Column to map a Song POJO to a database table.

Automatic ID Generation: Uses @GeneratedValue to let MySQL handle primary key creation (auto-increment).

Configuration: Connects to the database using the hibernate.cfg.xml configuration file.

CRUD Operation: Demonstrates the Create operation by persisting Song objects to the database.

Transaction Management: Correctly wraps database operations in a single Session transaction.

🚀 How to Run This Project

Prerequisites

Before you begin, ensure you have the following installed:

Java JDK 17 or newer.

Apache Maven.

MySQL Server (running on localhost:3306).

1. Clone the Repository

Replace with your actual repository URL

git clone https://github.com/your-username/your-repository-name.git cd your-repository-name

2. Create the Database

Open MySQL Workbench or your preferred SQL client and run the following command to create the database schema:

CREATE DATABASE hibernatedemo;

3. Configure Your Database Connection

Open the src/main/resources/hibernate.cfg.xml file.

Find these lines and update the password to match your local MySQL root password. You may also need to change the username if it's not root.

root your_password

4. Build and Run the Application

This project is set to hibernate.hbm2ddl.auto=create, so the first time you run it, Hibernate will automatically create the song table for you.

Let Maven build the project (your IDE usually does this automatically).

Run the main() method in the src/main/java/org/example/App.java file.

After running, you can check your MySQL hibernate_demo database. You will see a song table with the new records in it!

SELECT * FROM hibernatedemo.song;

✍️ Author

Aman Manwatkar

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages