(Optional) Lab 5.1: Integrating Spring and Jpa
(Optional) Lab 5.1: Integrating Spring and Jpa
S
Overview: In this (optional) lab, we will work with the
Spring/JPA integration, including
– Setting up a
LocalContainerEntityManagerFactoryBean
– Finishing a JPA-based Repository class that injects an
EM
Future Proof!
ApplicationContext
PARTS
You will focus on the following classes in this lab
– MusicItem : Our entity class - now persisted to the DB
– ItemRepository : API to define data access
• JpaItemRepository : Concrete implementation that uses JPA
– Catalog : Client API for services provided
• CatalogImpl : Concrete implementation of Catalog
CONFIGURATION
We use @Configuration metadata in this lab
CONFIGURATION Tasks to Perform
Open MusicItem for review (there is nothing to do here)
– In com.javatunes.domain in the src folder
– Note how the table name is specified (since it doesn't match the class)
– Note the id and generation strategy
– Note that the names of most persisted fields match the column names
of the table, and therefore need no annotations (1)
– Note that MusicCategory is Enumerated, and persisted as a string
– Note that the DateTimeFormatter is transient
• It it won't be persisted (used internally for formatting only)
•bean
Return type: Catalog, Actual object: CatalogImpl
• Remember to set the repository for the CatalogImpl
Open SpringConfig.java for review (There is nothing to do here)
– Note how it imports the @Configuration metadata
M Tasks to Perform
STOP
Lab 5.1: Spring and JPA 11
Lab 5.2: Spring Data
S
Overview: In this short lab, we will work with Spring Data
– We will create a Spring Data JPA interface that gives us the
standard Spring Data querying capabilities
– We'll use those methods to query the database.
Spring Data
– No more is NICE queries
boilerplate !! :-)
STOP
Lab 5.2: Spring Data 16
LAB 5.3: WRITING QUERY
METHODSIn this lab, we create queries in our interface using the
Spring Data Naming Conventions
LAB
SYNOPSI Lab
S
Overview: In this short lab, we will define our own query
method in our repository interface
– We'll use to to query the database.