Presentation on theme: "Rice KRAD Data Layer JPA Design Eric Westfall July 2013."— Presentation transcript:
rice KRAD Data Layer JPA Design Eric Westfall July 2013
What is JPA? “Java Persistence API” Framework for managing relational data from Java applications Standardized approach developed through the Java Community Process (JCP) Three main areas: – API for storage/retrieval of entities – JPQL (query language) – Object/Relational Metadata
JPA Versions JPA has had three major version releases – 1.0, 2.0, 2.1 We are targeting JPA version 2.1 Fairly recent, finalized April 2013
JPA 2.1 Features Converters Bulk Operations Stored Procedure support Database Schema Generation Entity Graphs JPQL/Criteria enhancements Unsynchronized Persistence Contexts
Vendor Neutrality Ideally we would like to stay vendor-neutral and use only JPA features But unfortunately we can’t! Two gaps: – Lack of programmatic access to mapping metadata – Lack of support for custom ID generation strategies Fortunately, this list is fairly small.
Vendor Selection Team working on JPA support in KRAD are recommending the use of EclipseLink as the default JPA implementation – Lazy loading semantics are the same as OJB, therefore eases the transition – Is the reference implementation of JPA – Provides us an easy method for programmatic read/write access to metadata – Support for custom identifier generation
So why use JPA then? Why not just use EclipseLink-specific annotations and functionality instead of JPA?
Database Plugging JPA into KRAD Data Layer KRAD Data API JPA Persistence Provider JPA Metadata Provider JPA Metadata Provider EclipseLink Client Application Code
EntityManager Configuration Spring provides LocalContainerEntityManagerFactoryBean But we’ve created a simpler version to handle setting up KRAD for JPA with EclipseLink
JpaPersistenceProvider save – verify the data object is valid and writable – perform "reference linking" (see KRAD Data Layer Design document for information on linking) – execute an EntityManager.merge(...) – return the merged object find – execute an EntityManager.find(...) – return the result findMatching – translate from the Rice QueryByCriteria to a JPA Query and execute it translate the result of the query into Rice QueryResults and return it delete – verify the data object is valid and writable – execute an EntityManager.delete(...)
SharedEntityManager Provided by Spring Allows us to inject an EntityManager that is shared across multiple classes Example usage with JpaPersistenceProvider
JPA Metadata Provider Provides metadata about Entities managed by the persistence unit Relationships, collections, keys, etc. Pulls what it can from the pure JPA “Metamodel” Gets the rest from EclipseLink-specific APIs
Sequences and Generated IDs For Oracle we use native sequences For MySQL we use “_S” tables to simulate sequences Can implement similar behavior using an EclipseLink “SessionCustomizer”
Converters Supported natively as of JPA 2.1! Allows for conversion between database and java types For example, storing booleans as “Y” and “N” character data in the database Boolean, KualiDecimal, KualiPercent, KualiInteger, Encrypted values
Extension Framework In KNS, allowed for institutions to customize database by adding tables with additional columns and “linking” them to the delivered parent table Required manual copy and modification of OJB mapping files Using programattic metadata modification, can do this programatically now with JPA + EclipseLink
Custom DAO’s and Data Access It is still very likely that applications will continue to need custom DAOs for certain cases In these cases, simply create the DAO and inject the shared EntityManager into it and use the JPA apis directly – JPQL – Crteria Query – Etc.
Database JPA with krad-data, services, and DAOs KRAD Data API JPA Persistence Provider JPA Metadata Provider JPA Metadata Provider EclipseLink Client Application Code Service API Service Implementation Data Access Object
Backend Development 1.Create JPA entities, map with annotations 2.Wire EntityManagerFactory bean in Spring 3.Wire SharedEntityManager bean ins Spring 4.Inject into a JpaPersistenceProvider 5.Inject into a JpaMetadataProvider 6.Register both providers with ModuleConfiguration 7.Use the KRAD DataObjectService!