In this phase of the project, we convert the code from phase 2 to use a real database. We're going to use an in memory database to start with (H2), then switch to MySQL once your code is working with H2.
PetClinic Reference Application
- Implement an H2 database by completing the instructions below in the
What You Need To Complete
section.
- Implement an inner builder pattern for each of your model classes by completing bonus activity 1.
- Convert to a MySQL datasource by completing bonus activity 2.
- Implement integration tests for each of your controllers by completing bonus activity 3.
- Completed Phase 2 code.
- Completed code for Owner sequence.
Complete the following steps to add a real database.
- Delete the following classes from your code completely:
From the repositories package:
FakeDatabase
BasicRepository
CrudRepository
From the model package:
Modifiable
Leave all other classes.
- Modify your models and remove any references to the
Modifiable
interface, including the@Override
annotation from thegetId()
method.
The relationships between objects is managed in the models. Most of the changes happen there:
- Modify your models to use the proper JPA annotations.
- Use the Owner class and the resources listed in the
HELP.md
file as a guide. - Implement a One-To-Many relationship between
Owner
andPet
. - Implement a One-To-Many relationship between
Pet
andVisit
. - Implement a Many-To-Many relationship between
Visit
andVet
. - Use the UML design as a guide.
The repositories need to be modified as well:
- Remove ALL the methods from each of your repositories. You'll only have the class headers remaining.
- Change the classes to interfaces.
- Remove the now unused
extends
from the method header. - Have each repository extend the
CrudRepository
from the JPA library. - Be sure to set the proper generic type for the
CrudRepository
. CrudRepository<Owner, Long>
is an example.Owner
is the class you'll be accessing in the database andLong
is the type of the id field.
The applications.properties
file has been updated:
- Check the settings in the properties file. There are several JPA and Hibernate settings added. Google them to see what they do.
- Spring
- Spring Boot
- Spring Data JPA
- Hibernate
- @SpringBootApplication
- @Controller
- @Service
- @Entity
- @Table
- @Id
- @GeneratedValue
- @OneToMany
- @ManyToOne
- @JoinColumn
- Add an inner builder class to each of your models.
- Use an IntelliJ plugin to accomplish this.
- Goto Preferences -> Plugins -> Marketplace and search for Builder Generator.
- Install the plugin and restart IntelliJ.
- Generate a builder by navigating to the class you want to add the builder to and navigate to Code -> Generate.
- Select Builder.
- Select
Inner Builder
andSingle Field
then select OK. - Add a public static method called
builder()
just above the new inner class. - Use the builder class in the Owner class as a pattern to complete this activity for the other 3 models.
- Use the new builder in your driver to create some new objects.
- Switch the database to use your MySQL instance.
- In the properties file, comment out the H2 settings.
- In the properties file, uncomment the MySQL settings.
- In the build file (pom.xml), uncomment the dependency for the MySQL driver library (mysql-connector-java).
- This assumes you have the H2 database working.
- Implement integration tests for the
OwnerController
,PetController
,VisitController
andVetController
classes. - Uncomment each of the tests in the
OwnerController
and make sure they run. Once they do, use them as a pattern to implement tests for the other 3 classes. - The port for the tests is set to 9090. Check the application.properties file in the test -> resources folder for more test settings.
- Dependencies have already been added for you in the pom.xml file for this phase. You shouldn't have to modify or add anything to your build file (pom.xml), but feel free to as needed.
- We are NOT creating a front end for this application. The
PetClinicDriver
acts as the front end (or view) for this application. Feel free to add to the driver as needed. - In this phase, don't worry about handling errors.