JPA java persistence. Notes from wikipedia Entities A persistence entity is a lightweight Java class that typically represents a table in a relational.

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

Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
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.
14-Jul-15 Tomcat. 2 The Apache Jakarta Project The Apache Jakarta Project “creates and maintains open source solutions on the Java platform for distribution.
Session-02.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
Session-01. Hibernate Framework ? Why we use Hibernate ?
An Introduction to Hibernate Matt Secoske
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.
CIS 764 Kansas State University
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.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian.
Agenda What is Hibernate Spring Integration Questions Overview
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.
EJB Entity Beans “Modeling your data”.
Session Beans Overview EJB container Remote Invocation Represent client's access to app Local / Remote speed / flexibility trade-off when to choose local.
Entity Beans BMP Celsina Bignoli
Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
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.
Introduction to Entities
Dali JPA Tools. About Dali Dali JPA Tools is an Eclipse Web Tools Platform sub-Project Dali 1.0 is a part of WTP 2.0 Europa coordinated release Goal -
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
Enterprise Java Java Persistence: EntityManager. Enterprise Java v070903Java Persistence: EntityManager2 Goals Become familiar with the Java Persistence.
Java Servlets & Java Server Pages Lecture July 2013.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
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 Apache TomEE // JavaEE Web Profile on Tomcat Jonathan #TomEE.
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006.
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.
Topic : Hibernate 2: Object Persistence and ORM Kaster Nurmukan.
Java Data Persistence Using Hibernate Jack Gardner October 2004.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
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.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
Java and the Web CSE 3330 Southern Methodist University.
Java Servlets and Java Server Pages
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.
JPA The New Enterprise Persistence Standard Mike Keith
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Google Code Libraries Dima Ionut Daniel. Contents What is Google Code? LDAPBeans Object-ldap-mapping Ldap-ODM Bug4j jOOR Rapa jongo Conclusion Bibliography.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
JAVA Persistence API(JPA) What is it? How would we use it in a project? Should we use it? -Parag Chaudhari.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Hibernate Java Persistence API. What is Persistence Persistence: The continued or prolonged existence of something. Most Applications Achieve Persistence.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Java Persistence Architecture (JPA)
Java Data Persistence Using Hibernate
In Class Assg 2 - solution
Knowledge Byte In this section, you will learn about:
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
Presentation transcript:

JPA java persistence

Notes from wikipedia Entities A persistence entity is a lightweight Java class that typically represents a table in a relational database. Entity instances correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. 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. relational databaseannotationsXML The Java Persistence Query Language The Java Persistence Query Language (JPQL) is used to make queries against entities stored in a relational database. Queries resemble SQL queries in syntax, but operate against entity objects rather than directly with database tables.SQL Relationship between Java Persistence API and Enterprise JavaBeans The Java Persistence API was defined as part of the EJB 3.0 specification, which is itself part of the Java EE 5 platform. You do not need an EJB container or a Java EE application server in order to run applications that use persistence, however. Future versions of the Java Persistence API will be defined in a separate JSR and specification rather than in the EJB JSR/specification.EJB 3.0JSR The Java Persistence API replaces the persistence solution of EJB 2.0 CMP.

JPA from wikipedia Relationship between Java Persistence API and Java Data Objects API The Java Persistence API was developed in part to unify the Java Data Objects API, and the EJB 2.0 Container Managed Persistence (CMP) API. This seems to have been successful as most products supporting each of those APIs now support the Java Persistence API. The Java Persistence API specifies only relational persistence (ORM) for Java (although there are providers that support other datastores). The Java Data Objects specification(s) provides relational persistence (ORM), as well as persistence to other types of datastores.Java Data Objects Relationship between Java Persistence API and Service Data Object API The Java Persistence API is designed for relational persistence, with many of the key areas taken from object-relational mapping tools such as Hibernate and TopLink. It is generally accepted that the Java Persistence API is a significant improvement on the EJB 2.0 specification. The Service Data Objects (SDO) API (JSR 235) has a very different objective to the Java Persistence API and is considered complementary. The SDO API is designed for service-oriented architectures, multiple data formats rather than only relational data, and multiple programming languages. The Java version of the SDO API is managed via the Java Community Process and the C++ version of the SDO API is managed via OASIS.object-relational mappingHibernateTopLinkService Data Objectsservice-oriented architecturesJava Community ProcessC++

More on JPA Motivation for creating Java Persistence API Many enterprise Java developers have been using lightweight persistent objects provided by open-source frameworks or Data Access Objects instead of entity beans because entity beans and enterprise beans were considered too heavyweight and complicated, and they could only be used in Java EE application servers. Many of the features of the third-party persistence frameworks were incorporated into the Java Persistence API, and projects like Hibernate and Open-Source Version TopLink Essentials are now implementations of the Java Persistence API.Data Access Objectsentity beansHibernateOpen-Source Version TopLink Essentials Relationship to Hibernate Hibernate is an Open source Object-relational mapping framework for Java. Versions 3.2 and later provide an implementation for the Java Persistence API[1].HibernateOpen sourceObject-relational mappingJava[1] Gavin King is the founder[2] of Hibernate. He represented JBoss on JSR220[3], the JCP expert group charged with developing JPA. This led to ongoing controversy and speculation centered around the relationship between JPA and Hibernate. Sun states [4] that ideas were drawn from several frameworks including Hibernate and JDO.Gavin King[2]JBoss[3]JCPSun[4]

Using jpa to persist entities

Some site info These sites have some jpql basics (java persistence query language) _overview_query.htmlhttp://edocs.bea.com/kodo/docs40/full/html/ejb3 _overview_query.html openjpa /docs/manual/jpa_langref.html#jpa_langref _bulk_opshttp://openjpa.apache.org/builds/1.0.2/apache- openjpa /docs/manual/jpa_langref.html#jpa_langref _bulk_ops 2EE/jpa/ 2EE/jpa/

Where this example came from The openejb examples come with a JPABean/JPAServlet example and I built this example by modifying theirs.

Directory structure WEB-INF –Web.xml –Classes META-INF –Persistence.xml Yourpackagename –Beanclass –servletclass

Running from a form

Minimal servlet package mycode; import java.util.*; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.persistence.EntityManager; import javax.persistence.EntityTransaction; import javax.persistence.Query; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class JpaServlet extends HttpServlet = "my-jpa-example") private EntityManagerFactory emf; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); ServletOutputStream out = response.getOutputStream(); String beanname = request.getParameter("name"); + emf); EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin(); JpaBean jpaBean = new JpaBean(); jpaBean.setName(beanname); em.persist(jpaBean); transaction.commit(); transaction.begin(); Query query = em.createQuery("SELECT j FROM JpaBean j"); List beans = query.getResultList(); for(int i=0;i<beans.size();i++){ beanname=((JpaBean)beans.get(i)).toString(); out.println(beanname);} }

Minimal bean package mycode; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import public class = = "id") private int = "name") private String name; public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "[ id=" + id + ", name=" + name + "]"; }

Web.xml – just jpa stuff is needed at bottom with servlet mapping <web-app xmlns=" xmlns:xsi=" xsi:schemaLocation=" metadata-complete="false" version="2.5"> JPA Servlet Example JpaServlet mycode.JpaServlet JpaServlet /jpa/* web.xml/PersistenceUnit my-jpa-example web.xml/PersistenceContext my-jpa-example Transactional

WEB-INF/classes/META- INF/Persistence.xml java:openejb/Connector/Default JDBC Database java:openejb/Connector/Default Unmanaged JDBC Database mycode.JpaBean

CRUD with JPA Servlet in notes I did not complete update for multiple fields Use previous persistence.xml and web.xml format

Student list

Delete action else if("delete".equals(action)&&beanname!=""){ EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin(); Query query = em.createQuery("delete from JpaBean j where j.name = :name"); query.setParameter("name", beanname); int deleted = query.executeUpdate(); out.println("deleted="+deleted); transaction.commit(); }

CRUD with java persistence: deleting all the Mary Sues

Add action if (action.equals("add")) { + emf); EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin(); jpaBean.setName(beanname); jpaBean.setYear(beanyear); jpaBean.setAge(Integer.parseInt(beanage)); jpaBean.setGpa(Double.parseDouble(beangpa)); em.persist(jpaBean); transaction.commit();}

add

Added Norman

List action EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); //then list transaction.begin(); Query query = em.createQuery("SELECT j FROM JpaBean j"); List beans = query.getResultList(); for(int i=0;i<beans.size();i++){ jpaBean=(JpaBean)beans.get(i); beanname=jpaBean.getName(); int age=jpaBean.getAge(); beanyear=jpaBean.getName(); double gpa=jpaBean.getGpa(); out.println(beanname+"\t"+age+'\t'+gpa+'\t'+beanyear);} transaction.commit();

update

Updating just the year

update

Updating all fields

A “workaround” Couldn’t get the prepared statement to work except for type= strings… So I collected results of query and updated them. They are persisted. else if("update".equals(action)){ EntityManager em = emf.createEntityManager(); EntityTransaction transaction = em.getTransaction(); transaction.begin(); Query query = em.createQuery("Select j from JpaBean as j where j.name=:name"); query.setParameter("name",beanname); List beans=query.getResultList(); for(int i=0;i<beans.size();i++){ jpaBean=(JpaBean)beans.get(i); double gpa=Double.parseDouble(beangpa); int age=Integer.parseInt(beanage); jpaBean.setAge(age); jpaBean.setYear(beanyear); }//for transaction.commit();}//update case

Params can have names or numbers Query query = em.createQuery("Update JpaBean as j set j.age=:age, j.gpa:gpa where j.name=:name"); int age=Integer.parseInt(beanage); double gpa=Double.parseDouble(beangpa); query.setParameter("gpa", gpa); query.setParameter(“age", age); query.setParameter("name", beanname); int updated=query.executeUpdate(); Or Query query = em.createQuery("Update JpaBean as j set j.age=’?1’, j.gpa=‘?2’ where j.name’?3"); Then query.setParameter(1, beanname); //etc Note… I did not complete the full update at the time I made this presentation