https://github.com/codecshekhar/spring-boot-hibernate-jpa-example
This repository contains spring-boot-hibernate-jpa concepts to illustrate its advantages
https://github.com/codecshekhar/spring-boot-hibernate-jpa-example
h2-database hibernate-jpa lombok spring-boot
Last synced: 10 months ago
JSON representation
This repository contains spring-boot-hibernate-jpa concepts to illustrate its advantages
- Host: GitHub
- URL: https://github.com/codecshekhar/spring-boot-hibernate-jpa-example
- Owner: CodeCshekhar
- Created: 2024-09-19T10:47:53.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-09-20T15:12:40.000Z (over 1 year ago)
- Last Synced: 2025-03-27T12:19:02.035Z (about 1 year ago)
- Topics: h2-database, hibernate-jpa, lombok, spring-boot
- Language: Java
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring Boot Hibernate JPA Example
This Example demonstrates the use of Spring Boot with Hibernate JPA, Lombok, and H2 database. It also includes information about using MySQL as an alternative database.
## Table of Contents
- [Technologies Used](#technologies-used)
- [Project Setup](#project-setup)
- [Configuration](#configuration)
- [Running the Application](#running-the-application)
- [H2 Database](#h2-database)
- [Using MySQL](#using-mysql)
- [Lombok](#lombok)
## Technologies Used
- Spring Boot
- Hibernate JPA
- Lombok
- H2 Database
- MySQL (optional)
## Project Setup
1. Ensure you have Java JDK 11 or later installed.
2. Clone this repository: `git clone https://github.com/Chandrashekharwagh/spring-boot-hibernate-jpa-example.git`
3. Navigate to the project directory: `cd spring-boot-hibernate-jpa-example`
4. Build the project: `./mvnw clean install` (or `mvnw clean install` on Windows)
## Project Structure
```
src
├── main
├── java
│ └── com
│ └── example
│ ├── SpringbootJpaApplication.java
│ ├── controller
│ ├── model
│ ├── repository
│ └── service
└── resources
└── application.properties
```
## Configuration
The project uses `application.properties` for configuration. Here's a sample configuration:
```properties
# H2 Database Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# H2 Console Configuration
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
```
## Running the Application
To run the application, use the following command:
```
./mvnw spring-boot:run
```
The application will start on `http://localhost:8080`.
## H2 Database
This project uses H2 as the default in-memory database. You can access the H2 console at `http://localhost:8080/h2-console`. Use the following details to log in:
- JDBC URL: `jdbc:h2:mem:testdb`
- Username: `sa`
- Password: `password`
## Using MySQL
To use MySQL instead of H2, follow these steps:
1. Add the MySQL dependency to your `pom.xml`:
```xml
mysql
mysql-connector-java
runtime
```
2. Update `application.properties`:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
```
3. Create the MySQL database:
```sql
CREATE DATABASE your_database_name;
```
4. Restart your application.
## Lombok
This project uses Lombok to reduce boilerplate code. Ensure your IDE has Lombok plugin installed for proper support.
To use Lombok in your entities:
```java
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class YourEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Other fields...
}
```
This setup allows you to use Lombok annotations like `@Data`, `@NoArgsConstructor`, and `@AllArgsConstructor` to automatically generate getters, setters, constructors, and other common methods.
For more information on Lombok, visit [Project Lombok](https://projectlombok.org/).
## Usage
1. Define your entities in the `model` package using JPA annotations and Lombok annotations.
2. Create repositories in the `repository` package by extending `JpaRepository`.
3. Implement your business logic in the `service` package.
4. Create REST endpoints in the `controller` package.
## License
This project is licensed under the MIT License - see the `LICENSE.md` file for details.