The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Fall 2001Arthur Keller – CS 18011–1 Schedule Oct. 30 (T) Embedded SQL. u Read Section 8.1. u Assignment 5 due. Not accepted late. u Project Part 4 due.
Distributed Application Development B. Ramamurthy.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
From Class Diagrams to Databases. So far we have considered “objects” Objects have attributes Objects have operations Attributes are the things you record.
Designing a Database Unleashing the Power of Relational Database Design.
EmbeddedSQL: 1 Impedance Mismatch Problem Problem : How to connect SQL statements with conventional programming languages Different models of language.
Winter 2002Arthur Keller – CS 18011–1 Schedule Today: Feb. 7 (TH) u PL/SQL, Embedded SQL, CLI, JDBC. u Read Sections 8.1, Feb. 12 (T) Advising.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Session-02.
SQLLite and Java CS-328 Dick Steflik. SQLLite Embedded RDBMS ACID Compliant Size – about 257 Kbytes Not a client/server architecture –Accessed via function.
1 Lecture 18 George Koutsogiannakis/Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 Lecture 17 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 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
CHAPTER 14 USING RELATIONAL DATABASES TO PROVIDE OBJECT PERSISTENCE (ONLINE) © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database.
Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 38 Advanced Java Database.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
Creating and using Persistent Data From before – Where does the data come from? – Why is it kept? – How is it used? Affects design and implementation choices.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
Dr Gordon Russell, Napier University Unit Embedded SQL - V3.0 1 Embedded SQL Unit 5.1.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 33 Advanced Java.
Winter 2006 Keller, Ullman, Cushing 11–1 Embedded SQL Add to a conventional programming language (C in our examples) certain statements that represent.
DAT602 Database Application Development Lecture 2 Review of Relational Database.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
Topic : Hibernate 3:Advanced ORM Kaster Nurmukan.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
Dr Gordon Russell, Napier University Unit Embedde SQL - V2.0 1 Embedded SQL Unit 5.1.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
Object storage and object interoperability
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Basics of JDBC Session 14.
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.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Introduction – ORM, Helloworld Application
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
JPA in Vaadin CSCI 3130 Winter What is JPA?  Java Persistence API  Allows for “easy” storage of Java Objects  Is a type of Object Relational.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
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.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Database: JDBC Overview
Don't Know Jack About Object-Relational Mapping?
Chengyu Sun California State University, Los Angeles
JDBC 15-Apr-18.
Database JDBC Overview CS Programming Languages for Web Applications
Advanced Java Programming
Java Database Connectivity
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
Presentation transcript:

The Java Persistence API Edel Sherratt

Contents Revisit applications programming Using Java Persistence API

Applications Programming Alternatives Extend a high level language by embedding SQL statements in it Extend SQL with programming language constructs Provide a call level interface (CLI) from a programming language

Applications Programming with a Call Level Interface Obtain a handle on the database Send SQL queries to the database management system using query functions Process the results of those queries – Results are tables and must be transformed into types that the application program can use – Use a cursor to access rows of the result set – Fetch each row in turn; represent as an array or associative array or other suitable structure

PHP connection to a database Obtain a database handle: e.g., pg_connect, mysql_connect, sqlite_open Execute queries: e.g. pg_query, sqlite_query, mysql_query Process results of the query: e.g. mysql_fetch_array, pg_fetch_array, sqlite_fetch_array, and many others

Java JDBC Driver manager provides implementations of Connection, Statement and ResultSet Connection acts as database handle Statement enables creation and execution of SQL queries ResultSet maintains a cursor, enabling access to current row of data returned by query

Example from the Java Dungeon this.connection = DriverManager.getConnection("jdbc:sqlite:"+dbn ame); statement.executeUpdate( "create table character (" + "name varchar(20) primary key," + "description text," + "kind varchar(20)," + "location varchar(20) references location(name));");

Processing the result set ResultSet things = whats_at(my_location); if (things.next()) { System.out.println ("\nYou consider taking:"); do { System.out.println( things.getString("name") ); } while (things.next()); }

Java Persistence With a CLI, there is a clear and explicit translation between database tables and programming language constructs The database and the application program are designed separately and made to work together. The Java Persistence API allows us to design with objects, and have the library functions deal with the translation to and from tables.

From CS12220 – Contacts.java ng/CourseNotes/current/CS12230/code/1- week1-in-lab/0-simple-examples/ ng/CourseNotes/current/CS12230/code/1- week1-in-lab/0-simple-examples/ A simple example, but wouldn’t it be good to have contacts persist in a database

Java Persistence API – main elements Entity – like Contact; instances are represented as rows in a table And an EntityManager – to interact with the database And a Persistence Unit – to group related entities together

Files, Directories and Jars Contact.java – a class definition with some extra annotations AddContacts.java – includes an EntityManager that allows me to place contacts in the database META-INF – a directory containing MANIFEST.MF and persistence.xml lib – a directory containing necessary jar files

Contact.java Original example by Lynda, "Contact") public class Contact // the primary (name = "name", nullable = false) private String name; //name of contact public String getName() { return this.name; (name="phone") private String phone; //phone of contact … etc. The annotations are defined in javax.persistence

AddContact.java EntityManagerFactory – creates an entity manager factory for the persistence unit – must match the persistence unit named in META-INF/persistence.xml EntityManager – interacts with the database A loop that reads in names and numbers and stores them in the database Notice how transactions are defined and used

META-INF/persistence.xml Names the persistence unit And the persistence provider And the class to be persisted And various properties like those we saw in connection strings previously

META-INF/MANIFEST.MF The class path The main class

Compiling, Packaging and Running the Application java -cp jar cvmf META-INF/MANIFEST.MF META-INF java –jar NB: you can run the jar anywhere, but do make sure the library jars are where MANIFEST/META-INF says they will be!

Changing to another database The java sources stay the same The Class Path entry in META-INF/MANIFEST.MF changes to reflect the new database connection jar Some properties in META-INF/persistence.xml are changed to reflect the new database

Using an IDE Normally, you would use an IDE like Netbeans or Eclipse to build your JPA application Netbeans will create a directory called dist containing your executable jar and a lib directory You can zip the jar and the lib into a single file that can be run anywhere

None of these is bidirectional

Bidirectional Relationships Every relationship has an owning side and an inverse side that maps to the owning side Annotate both sides to form a bidirectional relationship Design options like those for object-oriented database systems

A more complete example with relationships Neil Taylor Department-Employee Run as a Netbeans project Or zip the distribution to run standalone

Online tutorials /jw-01-jpa1.html /jw-01-jpa1.html /jw-01-jpa2.html /jw-01-jpa2.html

In Summary There are many ways to construct database applications The Java Persistence API allows us to focus on object oriented design Modern IDE’s automate much of the process of creating database applications