https://github.com/programmersteve/javaspringbootmanytomanyexample
Spring boot application with a Many-To-Many Example, join table has its own columns. Uses PostgreSQL as a database.
https://github.com/programmersteve/javaspringbootmanytomanyexample
java postgresql spring-boot spring-data spring-security
Last synced: 3 months ago
JSON representation
Spring boot application with a Many-To-Many Example, join table has its own columns. Uses PostgreSQL as a database.
- Host: GitHub
- URL: https://github.com/programmersteve/javaspringbootmanytomanyexample
- Owner: ProgrammerSteve
- Created: 2024-12-07T03:42:12.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-23T04:57:45.000Z (about 1 year ago)
- Last Synced: 2025-02-04T09:33:34.585Z (about 1 year ago)
- Topics: java, postgresql, spring-boot, spring-data, spring-security
- Language: Java
- Homepage:
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Setting up postgresql database
Make a volume then a PostgreSQL database on docker
```docker volume create VOLUME_NAME```
```
docker run --name CONTAINER_NAME -e POSTGRES_PASSWORD=YOUR_PASSWORD -d -p 5431:5432 -v VOLUME_NAME:/var/lib/postgresql/data postgres
```
Enter into the database
```
docker exec -it CONTAINER_NAME psql -U postgres
```
Create a database and connect to it
```
CREATE DATABASE yourdbname;
\c yourdbname;
```
Create the students table
```
CREATE TABLE students (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(50) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
academic_probation BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
Create the courses table
```
CREATE TABLE courses (
id SERIAL PRIMARY KEY,
course_name VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
Create the enrollments table
```
CREATE TABLE enrollments (
student_id INTEGER NOT NULL,
course_id INTEGER NOT NULL,
enrollment_date DATE,
grade REAL,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students (id),
FOREIGN KEY (course_id) REFERENCES courses (id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```
run `\dt` to see if all tables were created
Set up .env file
- The last part of JDBC_URL will be the database name
- JDBC_USERNAME will be postgres
```
JDBC_URL=jdbc:postgresql://localhost:5431/XXXXXXX
JDBC_USERNAME=XXXXXXX
JDBC_PASSWORD=XXXXXXX
```
Set up `application.properties`:
```
spring.application.name=ManyToMany
spring.datasource.url=${JDBC_URL}
spring.datasource.username=${JDBC_USERNAME}
spring.datasource.password=${JDBC_PASSWORD}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
```