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

Slides:



Advertisements
Similar presentations
Copyright ©2010 Oracle Corporation Made available under Creative Commons Attribution-Share Alike 3.0 Unported EclipseLink Runtime Architecture Data Source.
Advertisements

5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)
Connecting to Databases. connecting to DB DB server typically a standalone application Server runs on localhost for smaller sites –i.e. Same machine as.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
WaveMaker Visual AJAX Studio 4.0 Training
Fundamentals, Design, and Implementation, 9/e Chapter 14 JDBC, Java Server Pages, and MySQL.
JDBC Dr Jim Briggs. WEBP JDBC2 JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets) to databases Largely.
Fast Track to ColdFusion 9. Getting Started with ColdFusion Understanding Dynamic Web Pages ColdFusion Benchmark Introducing the ColdFusion Language Introducing.
Java 2 – Enterprise Edition Kevin J. LaFata April 21, 2003 UM – St. Louis.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Session-02.
Session-01. Hibernate Framework ? Why we use Hibernate ?
Emmanuel Cecchet et al.  Performance Scalability of J2EE application servers.  Test effect of: ◦ Application Implementation Methods ◦ Container Design.
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.
Object Persistence and Object-Relational Mapping James Brucker.
Introduction to JPA Java Persistence API Introduction to JPA.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
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.
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Database Technical Session By: Prof. Adarsh Patel.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
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.
TDDD05 EJB Lab (Part of slides reused from Mikhail’s) Lu Li
Standalone Java Application vs. Java Web Application
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 14 Using Relational Databases to Provide Object Persistence (Overview) Modern Database.
Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.
Topic : Hibernate 2: Object Persistence and ORM Kaster Nurmukan.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
1 Nov 29, 2005 Object Relational Mapping Frameworks Wiene Höweler.
Hibernate Basics. Basics What is persistence? Persistence in Object Oriented Applications? Paradigm Mismatch.  The Problem of Granularity.  The Problem.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Fundamentals of MyBATIS
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
JDBC Java and Databases. SWC – JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
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.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Don't Know Jack About Object-Relational Mapping?
CS3220 Web and Internet Programming Database Access with JDBC
Chengyu Sun California State University, Los Angeles
A very brief introduction
Advanced Java Programming
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Hibernate Bayu Priyambadha, S.Kom.
CS5220 Advanced Topics in Web Programming Spring – Web MVC
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
Developing and testing enterprise Java applications
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Persistence Jim Briggs 1

2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets) to databases Somewhat database independent – Requires SQL (which isn't completely standard)

3 JDBC architecture

4 Connection URLs JDBC has a special form of URL Define location of database and access parameters in a driver-specific way Examples: – – jdbc:mysql://localhost:3306/JIM – jdbc:derby://localhost:1527/jim Need to specify username and password

5 Doing a query (raw JDBC) Statement stmt = conn.createStatement(); ResultSet result = stmt.executeQuery( "SELECT Id, CustomerName, Cups, Type " + "FROM Customers " + "ORDER BY Cups DESC“ ); while(result.next()) { out.println(result.getString(1)); out.println(result.getInt(“Cups”); }

6 JDBC and web applications Special considerations – Connection latency Connection pooling – Multiple connections One connection per thread (request) – Transactions One transaction per request (normally)

Issues with JDBC Still rather database dependent Still requires good knowledge of SQL Tends to result in lots of hand-written, similar code to: – Establish database connection – Create query – Run query – Convert results into Java objects 7

Solution: automate it! Translation of database rows into Java objects can be automated (if given hints) – each row represents an object This is called: – Object Relational Mapping (ORM) – Object Relational Bridge (ORB) Basically: – a database table maps onto a Java class – a database column maps onto a property of a class Relationships (via foreign key constraints) can be managed automatically (if given hints) 8

Persistence frameworks List from /objectrelationalbridge.html /objectrelationalbridge.html – Castor Cayenne Databind Hibernate Ibatis Jakarta Torque: now part of database project Java DAO (Data Access Objects) JDBC JDO JDO (Java Data Objects) JGrinder JORM JRF (JRelational Framework) MappingObjects ObjectBridge ObjectMatter PersistenceLayer POD SimpleORM SQL SQL2Java Torque Castor Cayenne Databind Hibernate Ibatis Jakarta Torque Java DAO JDBC JDO JGrinder JORM JRF MappingObjects ObjectBridge ObjectMatter PersistenceLayer POD SimpleORM SQL SQL2Java Torque – javax.persistence API JPA TopLink EclipseLink javax.persistence API JPA 9

What comprises a JPA application? a set of entity classes (one per business object type) a set of data access objects (DAO) or facades (one per entity class) – the functionality for storing and fetching objects of the entity classes to/from the database a persistence unit (one per database accessed - usually one) – stored in the file persistence.xml, this contains configuration about the entities being managed and the database being used an entity manager object (one per thread/HTTP request) – this object (provided by the JPA implementation) manages the storing and fetching of managed objects to/from the database a entity manager factory (one per persistence unit) – this is the object that uses the configuration information in the persistence unit to create new entity managers 10

Typical steps to store data 1.Create a new object and populate its properties (e.g. using data received from the user interface) – don't forget to validate it! 2.Obtain an appropriate data access object and ask that to store the object 3.The DAO would obtain an entity manager and use its functionality to mark the object as persistent 4.The entity manager would interact with the database to store the object in an appropriate place and at an appropriate time 11

Typical steps to fetch data 1.Obtain an appropriate data access object and ask it to fetch the required data 2.The DAO would obtain an entity manager and use its functionality to fetch one or more objects from the database 3.Your application can then use the fetched object(s) as it wishes 4.If it changes it, no need to store it again (unless it becomes detached) 12

JPA: what you need to know Entity classes – Entity classes; annotation – How Java classes map on to database tables (the default and how you can specify it explicitly) – How Java properties map on to database columns (the default and how you can specify it explicitly) – How you specify the property(ies) that constitute the primary key of an object – How you can get JPA to assign primary key values automatically – Particular Entity relationships – Relationship mapping – Mapping one-to-one, one-to-many and many-to-many relationships between objects – Eager and lazy relationships – Ordering relationships – Mapping inheritance relationships. The 3 inheritance strategies (single table, joined and table-per-class) Managing persistent objects – The Entity Manager interface – Obtaining an entity manager (in a container-managed environment and in a non-container-managed environment) – The entity life cycle (new, managed, detached, removed) and how objects can be moved from one state to another – Persisting, merging and removing entities – Cascading events on persistent objects – Queries expressed in JPQL – JPQL: syntax (SELECT, FROM, WHERE, ORDER, GROUP); binding parameters – Queries expressed by criteria – Bulk update or deletion – Named queries Related issues – Concurrency – Versioning – Transaction management (in a container-managed environment and in a non-container-managed environment) – Optimistic and pessimistic locking Configuration – The EntityManagerFactory interface – Format of persistence.xml – Adding JPA libraries to your application – Adding JPA implementation libraries (e.g. Hibernate or TopLink) to your application – Setting up a container-managed web application using Glassfish – Setting up a non-container-managed web application using Tomcat 13

JPA: what you really need to know #1 Entity classes annotation annotation = GenerationType.AUTO) annotation – Mapping properties to database columns Configuration and set up – Persistence unit – persistence.xml – JPA libraries 14

JPA: what you really need to know #2 Managing persistent objects – Entity Name = "enterprise1PU") private EntityManager em; – Entity lifecycle new managed detached removed Common operations – em.persist (C) – em.find (R) – em.merge (U) – em.remove (D) 15

JPA: what you really need to know #3 Queries – JPQL – Criteria Relationships between entities – One to one – One to many – Many to one – Many to many 16

Structure17 Implementation architecture: single server Application User Web server JSPs = Views Servlets = Controllers Business task classes Business object classes Persistence classes Database

Integrating with EJBs Façade classes / Data Access Object classes – NetBeans AbstractFacade Types of session bean: Container managed injection 18