1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.

Slides:



Advertisements
Similar presentations
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Advertisements

JBoss Seam: Contextual Components Jason Bechtel
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
1 Lecture 18 George Koutsogiannakis/Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
1 Lecture 22 George Koutsogiannakis Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Database Programming in Java Corresponds with Chapter 32, 33.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Comp2513 JavaBeans, EJB and J2EE Daniel L. Silver, Ph.D.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
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.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Object storage and object interoperability
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
JPA The New Enterprise Persistence Standard Mike Keith
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
©NIIT Introducing Enterprise JavaBeans (EJB) Lesson 1A / Slide 1 of 43J2EE Server Components Objectives In this lesson, you will learn about: The features.
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.
SQL Basics Review Reviewing what we’ve learned so far…….
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
Java Persistence Architecture (JPA)
Advanced Java Programming
JDBC.
JavaBeans and JSP CS-422.
ISC440: Web Programming 2 Server-side Scripting PHP 3
MSIS 655 Advanced Business Applications Programming
Database Applications (15-415) ORM - Part I Lecture 11, February 11, 2018 Mohammad Hammoud.
Objectives In this lesson, you will learn to:
Objectives In this lesson you will learn about: Need for servlets
Relational Database Design
Enterprise Java Beans.
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
Objectives In this lesson, you will learn about:
Presentation transcript:

1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES

Topics Persistence API for JAVA EE5 Java Transaction API Entity class. Entity Relationships. Annotating Persistence. – Persistent Fields. – Persistent Properties. – Primary Keys Customer example Composite Keys 2

EJBs Session Beans act as the intermediary between the client requests and the complexity of storing data or accessing data from a database. The storing and retrieval of data is handles by code attributed to the Java Persistence API (JPA). – Up toEE5 there was another category of beans called: Entity Beans. Comparing EE concepts with the following discreet APIs architecture: 3

4 INTERNET CLIENT GUI BROWSER WEB SERVER Customer Accounts Servlet, Users Servlet, Updated Selections Servlet e.t.c, Applet, html files, images RMI SERVER /JDBC RMI SERVER / JDBC DATA BASE PRESENTATION TIER WEB TIER AND BUSINESS RULES TIER DATA TIER

Architecture Using EE Client can stay the same. Remove some of the functionality from servlets and incorporate it in a session bean. – Session bean can handle all the invocations. – Session bean receive the data from the JPA code and process it to the servlet(s). The RMI server code can be absorbed by the JPA code. The project can be build using NetBeans and be deployed on Tomcat and GlassFish v2. – We will have a web client for the session bean – EJB Session Bean code – JPA code – Database 5

Persistence API The Java Persistence API (JPA) was introduced as part of the Java EE 5 platform. Its goal is to facilitate data acquisition from relational databases without the complexity of numerous sql statements. The idea is to use an object oriented approach to data storage. Data is handled without the need to create deployment descriptors. – Instead annotations are used which are read by the container. 6

Persistence API- Alternative implementations of JPA An implementor of the JPA is called : Persistence Provider (i.e. NetBeansGlassFish). Some Concepts are explained and demonstrated using OpenJPA, an open source JPA implementation from Apache. OpenJPA has been integrated with the Weblogic, WebSphere, and Geronimo application servers. The current version of OpenJPA at the time of writing is If you're going to use a different setup, obviously you'll need to check out the documentation first (although the approach is mostly the same in most servers). Note: Kodo was originally developed by SolarMetric, Inc in BEA Systems acquired SolarMetric in 2005, where Kodo was expanded to be an implementation of JPA (JSR 220) [2] specification. In 2006, BEA donated a large part of the Kodo source code to the Apache Software Foundation under the name OpenJPA. [2] 7

Java Transaction API-JTA The Java Transaction API (JTA) is part of the J2EE platform. The API gives you the ability to perform distributed transactions, that is, an application can use the API to perform transactions on more than one data store in the network at the same time. But to do this efficiently, it helps to have another component operating in the application server: a J2EE transaction manager. A transaction manager helps to efficiently schedule and execute the potentially large number of transactions coming in through the application server. 8

9

Java Transaction API The data source must be registered on the server and is specified using the JNDI name. Transactions can be: – Container Managed – Application Managed. If the transactions are container-managed JTA transactions, the data source must be a JTA data source. This is the preferred way! – It must be specified in the decsriptor file persistence.xml 10

Java Transaction API If the transactions are application-managed, the data source is specified according to the JDBC database connection registered with the IDE. – You use this approach if you are not deploying in a EE container (like GlassFish). 11

Persistence API-Entities Entity – A table in a database. – An Entity is represented by a class – Instance of entity corresponds to a row in the database. – A number of rules cover the implementation of an entity class. Note: The database is sometimes referred as: Data Source or Data Store 12

Persistence API-Entity Relationships Relationships are the same as relationships between tables in a database: – One-To-One: Each instance of the entity (row) is related to single instance of another entity (to a single row of another table). – One-To-Many: An entity instance can be related to more than one instance of another entity. But an instance of the other Entity can only relate to one instance of the first. 13

Persistence API-Entity Relationships – Many-To-One: Multiple instances (rows) of an Entity can be related to one instance of another Entity. But an instance of the other Entity can relate to only one instance of the first Entity. – Many-To-Many: An instance of one Entity relates to many instances of another Entity and vice versa. For every relationship there is an owning side and inverse side. – The relationship can be unidirectional –it has only an owning side. – Or, bidirectional – it ha s both an owning side and an inverse side. 14

Example Relationships 15 Authors Titles Publishers Many To Many bidirectional Owner Inverse One-To-Many Bidirectional Owner Inverse Owner One-To-Many Bidirectional

Persistence API-Entity Class Entity Class – The class has instance variables – The class has accessor and mutator methods that follow the Java Beans convention for naming the methods. – The persistent state can be accessed either through the instance variables or through the getter methods (accessors). Persistent Fields (if fields are mapped with annotations) Persistent Properties (if getter methods are mapped with annotations) – This is done through annotations to one or the other but not both in a class. 16

Persistence API-Entity Class – The annotations are defined in javax.persistence, so you'll need to import that package. Annotations are handled by package javax.persistence – Fields annotated with Transient are not persisted (saved). – Annotations apply to other parts of the program i.e. The class needs to be annotated with annotation. The type of relationship needs to be annotated for example. Other annotations apply to different parts of the class. 17

Persistence API-Entity Class Every Entity must have a unique primary key field that enables clients to locate instances (rows) of an Entit. – Primary Keys can be: simple (consisting of a single field) or Composite (consting of one or moret persistent field)s and or properties) – Composite primary keys are represented by a primary key class. 18

Persistence API-Entity Class Entities support: – Inheritance. An Entity class can inherit a non Entity class and a non Entity class can inherit an Entity class. The non Entity classes can have persistent states information (annotations on their fields or properties). – If, however, the non Entity class is a super class, it has no persistent state as such. – Polymorphism. You can have an abstract Entity class and subclasses of the abstract Entity class that implement parts of the abstract class in different ways each. – Association (using another class by instantiating an object of that class). 19

Persistence API-Entity Inheritance Strategies Single Table of the database per class hierarchical inheritance. A Table of the database per concrete Entity class. A “Join” strategy – Fields/properties of a subclass are mapped to a different table than the super class. 20

Persistence API-Entity Management There is an Entity Manager object represented by an instance of: javax.persistence.EntityManager – Each manager object is associated with a persistent context (scope). – Each manager object: Removes persistent entity instances. Finds Entities by using the primary key. Allows queries to be run on Entities. 21

Persistence API-Entity Management – An Entity Manager can be: » Container-managed in which case it (persistence) propagates to all application components (EJBs) with a single Java Transaction Architecture (JTA) transaction via a special annotation. » Application-managed in which case the persistence does not propagate to other applications (EJBs). 22

Persistence API-Persistence Units Persistence Unit defines all Entity classes that are managed by EntityManager instances within an application. – A name should be provided to describe the Persistence Unit in your application. – A Persistence Unit is defined in the persistence.xml configuration file. – Location of the file within the application in the server varies depending dependent on if the client is a client application or a web application. The IDE will place it in the proper directory. 23

Persistence API-Entity Class Assume you have a CUSTOMER table, whose schema is represented by the table in the next slide. 24

Customer Table Example NAMEPK?TYPENULL? CUST_IDYINTEGERNOT NULL FIRST_NAME VARCHAR(50)NOT NULL LAST_NAME VARCHAR(50) STREET VARCHAR(50) APPT VARCHAR(20)NOT NULL CITY VARCHAR(25) ZIP_CODE VARCHAR(10)NOT NULL CUST_TYPE VARCHAR(10)NOT NULL LAST_UPDATED_TI ME TIMESTAMPNOT NULL 25

Customer Entity Class import javax.persistence.*; import java.io.Serializable; import = "CUSTOMER") //Name of the entity if name is different than the class ‘ name then us annotation. public class Customer implements Serializable { private long custId; private String firstName; private String lastName; private String street; private String appt; private String city; private String zipCode; private String custType; private Date updatedTime; // Getters and setters go here } 26

Customer Entity Class The Customer entity needs to know how to map the attributes (or properties) to the CUSTOMER table. – Through JPA annotations. Let us use Field persistence: 27

Customer Entity Class import javax.persistence.*; import java.io.Serializable; import = "CUSTOMER") //Name of the entity public class Customer implements Serializable //signifies the primary = "CUST_ID", nullable = = GenerationType.AUTO) private long custId; // 3 annotations define the field custId as a primary key. signifies a strategy to assign a unique value to your identity fields automatically. //The types of strategies available are IDENTITY, SEQUENCE, TABLE, and AUTO. 28

Customer Entity = "FIRST_NAME", nullable = false,length = 50) private String = "LAST_NAME", length = 50) private String lastName; // By default column name is same as attribute name private String = "APPT",nullable = false) private String appt; // By default column name is same as attribute name private String = "ZIP_CODE",nullable = false) // Name of the corresponding database column private String zipCode; 29

Customer Entity = "CUST_TYPE", length = 10) private = "LAST_UPDATED_TIME") private Date updatedTime; signifies a version field in an entity. JPA uses a version field to detect concurrent modifications to a data store record. When the JPA runtime detects multiple attempts to concurrently modify the same record, it throws an exception to the transaction attempting to commit last. This prevents you from overwriting the previous commit with stale data. // Getters and setters go here } 30

Customer Entity Class-xml file An descriptor file called persistence.xml is needed to describe the persistence unit (database). org.apache.openjpa.persistence.PersistenceProviderImpl entity.Customer 31

Customer Entity Class-xml file Note: openjpa is proprietary to openSource version. 32

Customer Entity Class-xml file Another version of the.xml file that will apply to GlassFish is: jdbc/MyCustomer NameofJar.jar path1.path2.Customer The data source (data base uses JTA – Java Transaction API) Class describes the entity class (or classes if more than one) 33

Customer Entity Class-xml file Notice that: – persistence.xml can have multiple persistence units. Each unit can be used by different JPA vendor or can be used to persist to different databases. – The vendor-specific persistence provider name is specified in the tag. The persistence provider for OpenJPA is org.apache.openjpa.persistence.PersistenceProviderImpl. – The entity class names are specified in the tag. – The database connection properties can be specified within the tag. Note that the property name will differ for each vendor. 34

Customer Entity Class-xml file – In the openSource version of the persistence.xml the transaction was defined as an application managed transaction via : transaction-type="RESOURCE_LOCAL“ You can do the same in NetBeans if you want the application to manage the transaction. – In the NetBeans version we indicated that the transaction was container managed. jdbc/MyCustomer was the name of the JTA data source (same name was used in the openSource version). 35

Customer Entity Class-A class that inserts a record public static void main(String[] args) { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testjpa"); EntityManager em = entityManagerFactory.createEntityManager(); EntityTransaction userTransaction = em.getTransaction(); userTransaction.begin(); Customer customer = new Customer(); customer.setFirstName("Charles"); customer.setLastName("Dickens"); customer.setCustType("RETAIL"); 36

Customer Entity Class-A class that inserts a record customer.setStreet("10 Downing Street"); customer.setAppt("1"); customer.setCity("NewYork"); customer.setZipCode("12345"); em.persist(customer); userTransaction.commit(); em.close(); entityManagerFactory.close(); } 37

Customer Entity Class-A class that inserts a record The work of the Persistence class is pretty simple: Persistence is a bootstrap class that is used to obtain an EntityManagerFactory In the classpath resources, the Persistence class searches for javax.persistence.spi.PersistenceProvider files in META- INF/services/directory. It reads the PersistenceProvider implementation class names from each file. It then calls createEntityManagerFactory() on each PersistenceProvider with the persistenceUnitName until it gets an EntityManagerFactory back that isn't null. The provider name for OpenJPA is org.apache.openjpa.persistence.PersistenceProviderImpl. 38

Customer Entity Class-A class that inserts a record EntityManagerFactory is a factory for creating an EntityManager. EntityManagerFactory should be cached and should ideally be called once for each persistence unit name in the whole application. EntityManager manages entities; it is responsible for their addition, updating, and deletion. The transaction is committed when the transaction object userTransaction invokes comit. 39

Customer Entity Class-finding a record Finding a record with a primary key is as simple as OpenJPAEntityManager oem = OpenJPAPersistence.cast(em); Object objId = oem.getObjectId(customer); Customer cust = em.find(Customer.class, objId); //where em is the EnityManager object //customer is the Customer object //Because the primary key is unknown up front, the application must cast EntityManager to OpenJPAEntityManager to get the primary key object by passing the customer object that was persisted earlier. This logic might differ for other vendors. 40

Customer Entity Class-finding a record A more generic approach that can relate to GlassFish better is EntityManager em; //instead of OpenJPAEntityManager public void findCustomer(int custID) { Customer cust=em.find(Customer.class, custID); ………………………… 41

Customer Entity Class-A Composite Primary Key Assume that the CUSTOMER table doesn't have a CUST_ID field, but that FIRST_NAME and LAST_NAME together make up the primary key. – You need to create a separate class, generally called an identity class, with attributes the same as the IDs; then you reference the identity class in the entity class. 42

Customer Entity Class-A Composite Primary Key public final class CustomerId implements serializable{ public String firstName; public String lastName; //default constructor //accessor/mutator methods // override equal() method //override hascode() method } The identity class can be a separate class or an inner class. If the class is an inner class, it must be static and should be referenced in the entity class. 43

Customer Entity Class-A Composite public class Customer = "FIRST_NAME", nullable = false, length = 50) private = "LAST_NAME", length = 50) private String lastName; private String = "APPT",nullable = false) private String appt;

Customer Entity Class-A Composite Primary Key The entity class is annotated with the to specify the primary key class (identity class) in the entity class Another possible annotations Which will be used on the identity class. 45

Difference Annotations Notice that when annotations is used the fields in the identity class match the fields in the entity class. is used as the annotation in the identity class class we have public final class CustomerId implements serializable{ public String firstName; public String lastName; //accessor/mutator methods // override equal() method //override hascode() method } 46

Difference Annotations The entity class does not have to be annotated public class Customer implements Serializable{ CustomerId primaryKey; public Customer() public CustomerId getPrimaryKey() { return primaryKey; } public void setPrimaryKey(CustomerId pk) { primaryKey = pk; …………………………………………………………… } 47

Study Guide Chapters 19, 20 of EE6 Tutorial. 48