CodeLab
This CodeLab exercise is designed to help you practice the concepts you have learned in the JPA 1 module on day 1. You will be working on a series of tasks that involve Java Persistence API, with Entities and DAOs. We will also practice pair programming if possible.
- Help each other working together in pairs
- Create a new Maven project getting ready for JPA with Hibernate. Use the JPA setup in IntelliJ guide to help you - or use your own starter project / template.
- Make sure your
.gitignore
file is set up correctly and then initialize a new Git repository in your project folder. Add and commit your files. - Create a new repository on GitHub, link them up, and push your project to the repository.
Each person should create a database in your docker environment with Postgres called
jpademo
.Set up Hibernate to connect to the database. Use the
config.properties
file to store the database name - and credentials.Create a new Entity class called
Person
with the following fields:- id (int)
- name (String)
- age (int)
Create a new DAO class called
PersonDAO
with the following methods:createPerson(Person person)
Create a new Main class and test that you can add a new person to your database.
You are going to create a new student management system. We need to be able to persist data about students and their courses. We need to have information about each student such as their name, phone number, email, address, status, date of birth, date of enrollment and whatever you think is relevant. We also need to have information about the courses they are taking such as the name of the course, the teacher, the semester, the classroom, the time of the course and whatever you think is relevant. Since we have not yet learned how to create relationships/references between entities, you can choose to just store the id of the courses in the student entity for now (we will learn how to do it the right way in JPA week 2).
The following features are suggestions You can add more tasks as you go along.
- Create new student
- Create new course
- Update student information
- Update course information
- Delete student
- Delete course
- List all students
- List all courses
- List all courses for a specific student
- List all students for a specific course (use streams and filters for this one)
- Come up with more tasks as you go along …
- Create a feature branch off the
main
branch for each task - Implement one feature at a time
- After completing a feature, add, commit, and push your changes to the repository
- Switch to the main branch - and merge the latest changes from the feature branch.
- Identify the next features
- Repeat steps 6-7 until done
classDiagram direction LR class Person { -int id -String name -int age } class PersonDAO { +Person createPerson(Person p) +Optional~Person~ findById(int id) +List~Person~ findAll() +Person update(Person p) +boolean deleteById(int id) } class Student { -int id -String name -String phone -String email -String address -StudentStatus status -LocalDate dateOfBirth -LocalDate dateOfEnrollment -Set~Integer~ courseIds // temporary “relation” by IDs +int getId() +String getName() +Set~Integer~ getCourseIds() } class Course { -int id -String name -String teacher -String semester -String classroom -String timeOfCourse // keep simple for now (could be LocalTime) +int getId() +String getName() } class StudentDAO { +Student create(Student s) +Optional~Student~ findById(int id) +List~Student~ findAll() +Student update(Student s) +boolean deleteById(int id) +List~Student~ findByCourseId(int courseId) // filter by courseIds contains } class CourseDAO { +Course create(Course c) +Optional~Course~ findById(int id) +List~Course~ findAll() +Course update(Course c) +boolean deleteById(int id) } class StudentStatus { <> ACTIVE INACTIVE GRADUATED SUSPENDED } class Main { +void main(String[] args) } %% Dependencies / “uses” PersonDAO --> Person : manages StudentDAO --> Student : manages CourseDAO --> Course : manages %% App entry uses DAOs and entities Main --> PersonDAO : uses Main --> StudentDAO : uses Main --> CourseDAO : uses Main --> Student : creates/uses Main --> Course : creates/uses