This project is the practice activity for Lesson 12: Introduction to Jakarta Persistence.
The starter project already contains a working Jakarta Faces TaskManager application that uses an in-memory service implementation.
Your goal is to upgrade the application so it uses Jakarta Persistence (JPA) and a relational database instead of memory storage.
This practice prepares you for Assignment 3, where you will apply the same pattern to the CampusEvent domain model.
This repository is a public starter project for the Lesson 12 TaskManager JPA practice activity.
Do not clone the original repository directly unless you only want a read-only copy. If you clone the original repository, you will not be able to push your changes back to GitHub.
Use the following workflow:
- Click Fork on the GitHub repository page.
- Create the fork under your own GitHub account.
- Clone your fork into IntelliJ IDEA.
- Complete the TaskManager JPA practice activity.
- Commit and push your changes to your fork.
- Use what you learned to complete Assignment 3 in your separate Assignment 3 GitHub Classroom repository.
Important:
- This practice repository is not your Assignment 3 submission.
- Assignment 3 must be completed in your separate GitHub Classroom Assignment 3 repository.
- Do not submit your Lesson 12 practice fork for Assignment 3.
This is a practice project only.
Do not submit this repository for Assignment 3.
Assignment 3 must be completed in your separate GitHub Classroom Assignment 3 repository.
The starter project includes:
pom.xmlTask.javaTaskPriority.javaTaskService.javaMemoryTaskService.javaTaskCrudView.javamanage-tasks.xhtmlbeans.xmlweb.xmllayout.xhtmlindex.xhtml
The application currently works with MemoryTaskService.
That means tasks are stored only in memory and will be lost when the application restarts.
By the end of this practice activity, your project should use a JPA service implementation instead of the memory service.
The final architecture should be:
TaskCrudView
-> TaskService interface
-> TaskJpaService
-> EntityManager
-> H2 relational database
Complete the following steps.
Create the following file using the provided IntelliJ IDEA file template:
src/main/java/dmit2015/config/ApplicationConfig.java
Use the file template:
DMIT2015 Jakarta Persistence ApplicationConfig
This file should define a JTA datasource for the H2 database.
For this practice project, use a file-based H2 database stored in the project data folder.
Example database URL:
jdbc:h2:file:./data/lesson12-taskmanager-jpa-db
Create the following file using the provided IntelliJ IDEA file template:
src/main/resources/META-INF/persistence.xml
Use the file template:
DMIT2015 Jakarta Persistence persistence.xml
The persistence unit should use the JTA datasource defined in ApplicationConfig.java.
During early development, it is acceptable to use:
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>This setting recreates the database tables each time the application starts.
After the entity is stable, change the value to:
<property name="jakarta.persistence.schema-generation.database.action" value="none"/>This prevents the file-based H2 database from being deleted and recreated on every restart.
Update Task.java so it is mapped as a JPA entity.
Your entity should include:
@Entity@Id@GeneratedValue- validation annotations for required fields
@VersioncreateTimeupdateTime@PrePersist@PreUpdate
Recommended audit timestamp behavior:
@PrePersist
void onCreate() {
LocalDateTime now = LocalDateTime.now();
createTime = now;
updateTime = now;
}
@PreUpdate
void onUpdate() {
updateTime = LocalDateTime.now();
}Create a new service implementation using the provided IntelliJ IDEA file template:
src/main/java/dmit2015/service/TaskJpaService.java
Use the file template:
DMIT2015 Model Service Interface Jakarta Persistence Implementation
TaskJpaService must implement TaskService.
The service should use:
@PersistenceContext
private EntityManager entityManager;Implement the basic CRUD methods using EntityManager.
The starter project currently uses MemoryTaskService.
Update the JSF View/backing bean so it uses the JPA implementation.
The JSF page should not contain database logic.
Database logic belongs in:
TaskJpaService.java
not in:
TaskCrudView.java
Create a database initializer class using the provided IntelliJ IDEA file template:
DMIT2015 Jakarta Persistence Entity Class Initializer
Recommended file location:
src/main/java/dmit2015/config/TaskDataInitializer.java
The initializer should add sample tasks only when the database is empty.
Do not place seed data logic in the JSF View class.
Start the application with:
mvn wildfly:devOpen the application in your browser:
http://localhost:8080/
Open the task page and verify that you can create, view, update, and delete tasks using the JPA service.
The project includes the H2 Console for local development and troubleshooting.
Open:
http://localhost:8080/h2-console
Use the same JDBC URL, username, and password configured in ApplicationConfig.java.
Example:
JDBC URL: jdbc:h2:file:./data/lesson12-taskmanager-jpa-db
User Name: user2015
Password: Password2015
The H2 Console is for debugging only.
Do not use it as a replacement for the JSF page or service layer.
Before asking for Assignment 3 troubleshooting help, be prepared to show your Lesson 12 practice progress.
Minimum checkpoint:
ApplicationConfig.javacreatedpersistence.xmlcreatedTask.javamapped as a JPA entityTaskJpaService.javacreatedTaskCrudView.javauses the JPA servicemanage-tasks.xhtmlcan create and display tasks from the database
- Do not keep using
MemoryTaskServiceafter creatingTaskJpaService. - Do not put
EntityManagercode insideTaskCrudView. - Do not forget to add
Taskto the persistence unit if yourpersistence.xmlrequires explicit entity classes. - Do not run with
drop-and-createand expect file-based H2 data to survive a restart. - Do not create duplicate service classes in different packages.
- Do not manually create required demo data only through the H2 Console.
Answer the following after completing the practice activity.
Briefly describe how the application changed when you replaced memory storage with JPA storage.
Write one or two sentences.
Write one or two sentences.
Write one or two sentences.
Briefly describe one issue you encountered and how you solved it.