Exploring the Java Persistence API

Exploring the Java Persistence API
LexiWills Profile Pic
LexiWills,United Kingdom,Professional
Published Date:31-07-2017
Your Website URL(Optional)
Exploring the Java Persistence API By Bob McCuneAgenda JPA Overview Annotating Persistent Entities Understanding the EntityManager JPQL and the Query Interface Spring and JPA DemoJPA OverviewWhat is the JPA? Developed under JSR-220 Initial goal was to simplify EJB CMP JSR-220 segmented into two specifications: EJB 3.0 Java Persistence API Complete, standalone ORM solution for both Java EE and Java SE environments Significant Community Involvement: Leverages best ideas from Hibernate, Toplink, and JDOWhy should I care? Why not just use JDBC? Low level API Simple to use, but can be error prone Why not just use INSERT ORM HERE? Standardized API leveraging best ideas from ORM community Better for developers - one API to learn and use Can choose between competing implementations Vendor independenceGoals Provide complete ORM solution for Java SE and Java EE environments Easy to use Standard POJOs - no framework interfaces or classes to implement or extend Facilitate test-driven development Annotation driven, no XML mappings required. Configuration By Exception Sensible defaultsJPA Features Simple POJO Persistence No vendor-specific interfaces or classes Supports rich domain models No more anemic domain models Multiple inheritance strategies Polymorphic Queries Lazy loading of associations Rich Annotation Support Pluggable persistence providersPersistent POJOsPOJO Requirements Annotated with Entity Contains a persistent Id field No argument constructor (public or protected) Not marked final Class, method, or persistent field level Top level class Can’t be inner class, interface, or enum Must implement Serializable to be remotely passed by value as a detached instancePersistent Entity Example Entity public class AppUser AppUser Id id username password private Long id; private String username; private String password; Annotating EntitiesJPA Annotations JPA annotations are defined in the javax.persistence package: http://java.sun.com/javaee/5/docs/api/javax/persistence/package- summary.html Annotations can be placed on fields or properties Field level access is preferred to prevent executing logic Property-level annotations are applied to "getter" method Can’t mix style in inheritance hierarchy Must decide on field OR propertyPersistent Identifiers Entities must define an id field/fields corresponding the the database primary key The id can either be simple or composite value Strategies: Id: single valued type - most common IdClass: map multiple fields to table PK EmbeddedId map PK class to table PK Composite PK classes must: implement Serializable override equals() and hashCode()IdClass Maps multiple fields of persistent entity to PK class Entity IdClass(ArtistPK.class) public class ArtistPK public class Artist implements Serializable Id private Long idOne; private Long idOne; private Long idTwo; Id private Long idTwo; public boolean equals(Object obj); public int hashCode(); EmbeddedId Primary key is formal member of persistent entity Embedded Entity public class ArtistPK public class Artist implements Serializable EmbeddedId private Long id1; private ArtistPK key; private Long id2; public boolean equals(Object obj); public int hashCode(); GeneratedValue Supports auto-generated primary key values Strategies defined by GenerationType enum: GenerationType.AUTO (preferred) GenerationType.IDENTITY GenerationType.SEQUENCE GenerationType.TABLE Id GeneratedValue(strategy = GenerationType.AUTO) private Long id;Table and Column Used to define name mappings between Java object and database table/columns Table applied at the persistent class level Column applied at the persistent field/property level Entity Table(name = "TBL_ARTIST") public class Artist TBL_ARTIST Id ARTIST_ID NUMERIC ARTIST_NAME VARCHAR(50) Column(name = "ARTIST_ID") private Long id; Column(name = "ARTIST_NAME") private String name; Temporal Used with java.util.Date or java.util.Calendar to determine how value is persisted Values defined by TemporalType: TemporalType.DATE (java.sql.Date) TemporalType.TIME (java.sql.Time) TemporalType.TIMESTAMP (java.sql.Timestamp) ... TBL_ARTIST Temporal(value=TemporalType.DATE) ARTIST_ID NUMERIC Column(name="BIO_DATE") BIO_DATE DATE private Date bioDate; ...Enumerated Used to determine strategy for persisting Java enum values to database Values defined by EnumType: EnumType.ORDINAL (default) EnumType.STRING Entity public class Album ALBUM ... ALBUM_ID NUMERIC Enumerated(EnumType.STRING) RATING VARCHAR(10) private Rating rating; ... Lob Used to persist values to BLOB/CLOB fields Often used with Basic to lazy load value Entity public class Album ... ALBUM Lob ALBUM_ID NUMERIC Basic (fetch = FetchType.LAZY) ALBUM_ART BLOB Column(name = "ALBUM_ART") private byte artwork; ...

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.