CS6320 – Java Persistence API

Slides:



Advertisements
Similar presentations
V 6, Mats Strandberg ORM With Hibernate.
Advertisements

Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
JBoss Seam: Contextual Components Jason Bechtel
Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
Session-02.
An Introduction to Hibernate Matt Secoske
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
CIS 764 Kansas State University
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Agenda What is Hibernate Spring Integration Questions Overview
Maven for building Java applications By Nalin De Zoysa
1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
EJB 3.0 Persistence Sang Shin Java Technology Architect
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
Hibernate Query Language Make SQL be object oriented – Classes and properties instead of tables and columns – Polymorphism – Associations – Much less verbose.
Object/Relational Mapping with Hibernate Practical ORM.
Professional Open Source © JBoss Group, October 2015 EJB 3.0 Ease of use.
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
1 Part 2: EJB Persistency Jianguo Lu. 2 Object Persistency A persistent object is one that can automatically store and retrieve itself in permanent storage.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
OK lets start, who am I ?. Nayden Gochev (a.k.a. JOKe) Java Spring Android Hybris GWT EJB JSF RMI JAX-RS Struts JMS JPA Hibernate C# ASP.NET TestStudio.
1 Mapping Persistent Objects Entities represent data in the database, so changes to an entity bean result in changes to the database. That's ultimately.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Hibernate Introduction - 1 Present by Eric Yu. BeanSoft | 2 Content Understanding object/relational persistence Introduction Architecture The core interfaces.
An Introduction to Object/Relational Persistence and Hibernate Yi Li
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Java Persistence API v1.0 a standard for ORM frameworks and POJO based Persistence Magnus Larsson
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Object storage and object interoperability
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
By Srinivas Mahakud Java Persistence API JPA. Review Topics: Introduction what is the JPA? What is the JPA contains of? Why developers should Create the.
Java Persistence API (JPA) Relationships. Kinds of relationships UML associations and aggregations (ER non- identifying relationships, shared semantics)
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
JPA The New Enterprise Persistence Standard Mike Keith
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Hibernate Annotation 李日貴 (jini) jakarta99 AT gmail.com SoftLeader Tech. Corp. Taiwan Java Annotation Lesson 1.
1 EJB 3.0: There’s Something Different About You Martin Jäkle, TSBU Middleware.
Object-Relational Mapping. Contents  Default Mapping Rules  Elementary Mapping  Embeddables  Relationship Mapping  Inheritance Mapping.
Chengyu Sun California State University, Los Angeles
Java Persistence Architecture (JPA)
J2EE Lecture 8: Hibernate
Chengyu Sun California State University, Los Angeles
Advanced Java Programming
Object/Relational Mapping with Hibernate
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
Knowledge Byte In this section, you will learn about:
Understanding and Designing with EJB
Chengyu Sun California State University, Los Angeles
Developing and testing enterprise Java applications
CS4961 Software Design Laboratory Understand Aquila Backend
Topics Persistence API for JAVA EE5 Java Transaction API Entity class.
Chengyu Sun California State University, Los Angeles
Knowledge Byte In this section, you will learn about:
Presentation transcript:

CS6320 – Java Persistence API L. Grewe

Java Persistence API Replaces Entity Beans in Java EE API the API itself, defined in the javax.persistence package the Java Persistence Query Language (JPQL)

An Entity A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. correspond to individual rows in the table. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

Advantage over previous Entity Beans DO NOT need an EJB container or a Java EE application server in order to run applications that use persistence Entity Beans perceived as too complication and programmers had turned to third-party non- entity bean solutions

Full Object/Relational Database Mapping Defining Entity Beans Full Object/Relational Database Mapping

EJB 3.0 Entity Beans O/R Mapping Metadata as annotations Table mappings, @Table, @SecondaryTable Column mappings, @Column, @JoinColumn Relationships, @ManyToOne, @OneToOne, @OneToMany, @ManyToMany Multi-Table mappings, @SecondaryTable Embedded objects, @Embedded Inheritance, @Inheritance, @DiscriminatorColumn Identifier + Version properties, @Id, @Version

relational table declaration Entity Annotations @Entity @Table(name=“AUCTION_ITEM”) public class Item { private long id; private String description; private String productName; private Set<Bid> bids = new HashSet(); private User seller; @Id @GeneratedValue @Column(name=“ITEM_ID”) public long getId() { return id; } public void setId(long id) { this.id = id; relational table declaration auto-key generation

Entity Annotations @Entity @Table(name=“AUCTION_ITEM”) public class Item { private long id; private String description; private String productName; private Set<Bid> bids = new HashSet(); private User seller; @Id @GeneratedValue @Column(name=“ITEM_ID”) public long getId() { return id; } public void setId(long id) { this.id = id; create table AUCTION_ITEM ( ITEM_ID Number, DESC varchar(255), ProductName varchar(255), USER_ID Number );

Entity Annotations … @Column(name=“DESC”, length=500) public String getDescription() { return description; } public void setDescription(String desc) { this.description = desc; public String getProductName() { return productName; protected void setProductName(String name) { this.productName = name; column mapping intuitive defaults

Relationships Relationships, @ManyToOne, @OneToOne, @OneToMany, @ManyToMany Supports lazy and eager loading of relationships Cascades: delete, create, and merge No CMR: You must manage your relationships.

Entity Relationships @OneToOne(fetch=LAZY) @JoinColumn(name=“OWNER_ID”) public Owner getOwner() { return owner; } public void setOwner(Owner owner) { this.owner = owner; @OneToMany(cascade=ALL) @JoinColumn(name=“ITEM_ID”) public Set<Bid> getBids() { return bids; public void setBids(Set<Bid> bids) { this.bids = bids; lazy/eager loading various cascade types

Entity Relationships @OneToOne(fetch=LAZY) @JoinColumn(name=“OWNER_ID”) public Owner getOwner() { return owner; } public void setOwner(Owner user) { this.owner = owner; @OneToMany(cascade=ALL) @JoinColumn(name=“ITEM_ID”) public Set<Bid> getBids() { return bids; public void setBids(Set<Bid> bids) { this.bids = bids; create table AUCTION_ITEM ( ITEM_ID Number, DESC varchar(255), ProductName varchar(255), OWNER_ID Number ); create table BID … ITEM_ID Number

No CMR @OneToMany(cascade=ALL) @JoinColumn(name=“ITEM_ID”) public Set<Bid> getBids() { return bids; } public void addBid(Bid bid) { getBids().add(bid); bid.setItem(this);

Multi-Table Multi-Table Mappings, Entity can be stored in one or more tables @SecondaryTables, @SecondaryTable

Multi-table Mappings @Entity @SecondaryTable(name=“ADDRESS”, pkJoinColumns= @PrimaryKeyJoinColumn(name=“ADDR_ID”)) public class Owner { private long id; private String name; private String street; private String city; private String state; @Id @GeneratedValue public long getId() { return id; } public void setId(long id) { this.id = id; create table OWNER ( ID Number, NAME varchar(255), ); create table ADDRESS ADDR_ID Number, STREET varchar(255), CITY varchar(255), STATE varchar(255) );z

Multi-Table Mappings @Column(name=“STREET”, table=“ADDRESS”) public String getStreet() { return street; } public void setStreet(String street) { this.street = street; @Column(name=“CITY”, table=“ADDRESS”) public String getCity() { return city; protected void setCity(String city) { this.city = city; create table OWNER ( ID Number, NAME varchar(255), ); create table ADDRESS ADDR_ID Number, STREET varchar(255), CITY varchar(255), STATE varchar(255)

Embedded Objects Embedded Objects Aggregated objects whose properties can be mapped @Embedded, @Embeddable

Embedded Objects @Entity public class Owner { private long id; private String name; private Address address; @Id @GeneratedValue public long getId() { return id; } public void setId(long id) { this.id = id; public String getName() { @Embeddable public class Address { private String street; private String city; private String state; @Column(“STREET”) public String getStreet() { return street; } public void setStreet(String street) { this.street = street; public String getCity() { return city;

Embedded … @Embedded({ @AttributeOverride(name=“street”, @Column(name=“STREET”)), @AttributeOverride(name=“city”, @Column(name=“CITY”)), @AttributeOverride(name=“state”, @Column(name=“STATE”))}) public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address;

Composite Keys/Primary Key Classes EJB 2.1 style primary keys Same idea as @Embedded @EmbeddedId, @Embeddable, @IdClass

Composite Keys @Entity public class Owner { private OwnerPK pk; private Address address; @EmbeddedId public OwnerPK getId() { return id; } public void setId(OwnerPK id) { this.id = id; @Embeddable public class OwnerPK { private String name; private long ssn; @Column(“NAME”) public String getName() { return street; } public long getSSN() { return sn; …

Composite Keys @Entity @IdClass(OwnerPK.class) public class Owner { @Id String name; @Id long ssn; @Embedded Address address; … } @Embeddable public class OwnerPK { private String name; private long ssn; @Column(“NAME”) public String getName() { return street; } public long getSSN() { return sn; …

Inheritance Persistence mapping supports inheritance Single table per hierarchy – SINGLE_TABLE Join table per subclass – JOINED Distinct table per subclass – TABLE_PER_CLASS Queries on class hierarchy are polymorphic

Inheritance – SINGLE_TABLE @Entity @Table(name="Animal") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="TYPE") public class Animal { @Id private int id; @Column(name="AVG_WEIGHT") private int averageWeight; ... } public class Dog extends Animal{ @Column(name="BREED") private String breed;

Inheritance – SINGLE_TABLE create table Animal ( ID Number, TYPE varchar(255), AVG_WEIGHT Number, BREED varchar(255) );

Inheritance – JOINED @Entity @Inheritance(strategy=InheritanceType.JOINED) public class Animal{ @Id private int id; @Column(name="AVG_WEIGHT") private int averageWeight; ... } @InheritanceJoinColumn(name="DOGGY_ID") public class Dog extends Animal{ @Column(name="BREED") private String breed;

Inheritance – JOINED create table Animal ( ID Number, TYPE varchar(255), AVG_WEIGHT Number ); create table Doggy DOGGY_ID Number, BREED varchar(255)

Inheritance – TABLE_PER_CLASS create table Kitty ( ID Number, TYPE varchar(255), AVG_WEIGHT Number BREED varchar(255), LIVES Number ); create table Doggy DOGGY_ID Number, BREED varchar(255)

Interacting With Entity Bean Plain Java Objects

Entity Manager Entities created as any plain Java object Customer cust = new Customer(); Plain Java objects and homeless Can be detached and reattached to container Can be serialized to remote client Remote client can perform updates on local copy Copy can be sent back to server and merged back in Persisted by the EntityManager service All access through this service Creation, retrieval, removal, and merging Analogous to Hibernate Session

Create the objects Create the entities like you would any other object Allocate entire object graph like any other Java code Item item = new Item(); item.setDescription(“O’reilly’s EJB 3.0 5th Edition”); item.setProductName(“EJB 3.0 Book”); … Owner bill = new Owner(); bill.setName(“Bill”); item.setOwner(bill); Bid bid = new Bid(); HashSet<Bid> bids = new HashSet(); bids.add(bid); item.setBids(bids);

Entity Manager All entities persisted by the EntityManager service All access through this service Creation, retrieval, removal, and merging Analogous to Hibernate Session Injected with dependency injection

Inject the EntityManager service @Stateless public class ItemDAOImpl implements ItemDAORemote { @PersistenceContext(unitName=“Auction”) private EntityManager em; public long create(Item item) { em.persist(item); return item.getId(); } public Item findById(long id) { return em.find(Item.class, id); public void merge(Item item) { em.merge(item); Inject the EntityManager service

EntityManager @Stateless public class ItemDAOImpl implements ItemDAORemote { @PersistenceContext(unitName=“Auction”) private EntityManager em; public long create(Item item) { em.persist(item); return item.getId(); } public Item findById(long id) { return em.find(Item.class, id); public void merge(Item item) { em.merge(item); Item allocated remotely If cascade PERSIST, entire object graph inserted into storage

EntityManager @Stateless public class ItemDAOImpl implements ItemDAORemote { @PersistenceContext(unitName=“Auction”) private EntityManager em; public long create(Item item) { em.persist(item); return item.getId(); } public Item findById(long id) { return em.find(Item.class, id); public void merge(Item item) { em.merge(item); Item found with primary key Detached from persistent storage at tx completion Can be serialized like any other object

EntityManager @Stateless public class ItemDAOImpl implements ItemDAORemote { @PersistenceContext(unitName=“Auction”) private EntityManager em; public long create(Item item) { em.persist(item); return item.getId(); } public Item findById(long id) { return em.find(Item.class, id); public void merge(Item item) { em.merge(item); Item can be updated remotely and changes merged back to persistent storage merge() returns a managed copy of Item

Query API Queries may be expressed as EJBQL strings Embedded in code Externalized to metadata (named queries) Invoke via Query interface Named parameter binding Pagination control @Stateless public class ItemDAOImpl { … public List findByDescription(String description, int page) { return em.createQuery(“from Item i where i.description like :d”) .setParameter(“d”, description) .setMaxResults(50) .setFirstResult(page*50) .getResultList(); }

EJB QL 3.0 EJBQL 3.0 is very similar to HQL (Hibernate Query Language) Aggregation, projection select max(b.amount) from Bid b where b.item = :id select new Name(c.first, c.last) from Customer c Fetching from Item i left join fetch i.bids Subselects from Item i join i.bids bid where bid.amount = (select max(b.amount) from i.bids b) Group By, Having, Joins