Learn how to map the problem domain entities to Java JPA entities.
- [Instructor] Now we are ready to start implementing the solution for an Explore California RESTful web service. The first step is to create the domain model from the artifacts identified from the ExploreCalifornia.org/tours page. Explore California exported the data about the tours to a file. After examining the fields, I defined the following classes, tour, tour package, region, and difficulty. Here is a class diagram to show the attributes of each class and how they relate.
The central artifact is a tour. A tour has a title, a description, which is a short explanation, a blurb, which is a longer explanation, a price, a duration, comma-separated bullet points and keywords used for searching tours. We see that a tour also has a region and a difficulty. Region is modeled as an enumerated type where the possible values are Northern_California, Central_Coast, Southern_California and Varies.
Difficulty is also modeled as an enumeration whose possible values are Easy, Medium, Difficult, and Varies. Tour is associated with one tour package, but a tour package is associated with several possible tours. Let's look at the tour package called Backpack Cali. Backpack Cali is associated with five tours, Big Sur Retreat, Channel Island Excursions, The Death Valley Survivor's Trek, and the Steps of John Muir and the Mt. Whitney Climbers Tour.
Now that we've established the business objects in the domain model, let's see the associated code. First let's look at the Tour JPA entity. As I said before, this tutorial is for developers with previous knowledge of the Java Persistent API. If you do know JPA, then you will recognize the at entity, @Id, @GeneratedValue, @Column and @ManyToOne annotations in this Java class.
All the attributes in the class diagram are present here, plus the generated ID field. In addition to the typical field getter and setters, there is a constructor, which takes all the fields and initializes the attributes of those fields. I make the default constructor protected so consumers of this class are only able to construct a fully initialized Java object. Now let's look at Region. Region is an enumerated type with values Central_Coast, Southern_California, Northern_California and Varies.
Difficulty is also an enumerated type with values Easy, Medium, Difficult and Varies. The TourPackage entity has a code attribute as its ID, which is a string, and a name, also a string. Before Spring Data JPA, the next step would be creating data access objects and data services which invoke the entity manager to create, read, update and delete Persistence objects.
Spring Data JPA eliminates all of that repetitive, error-prone boilerplate code.
- Setting up the project
- Building, deploying, and launch the microservice
- Declaring Spring Data JPA repository interfaces
- Invoking repositories
- Using Spring Data query methods
- Exposing RESTful APIs with Spring Data REST
- Using the /search resource to invoke query methods
- Paging and sorting
- Declaring a new REST controller
- Creating HTTP methods for creating, reading, updating and deleting persistent data.