Persistence Models Margaret Smith. Overview Java Data Objects (JDO) Enterprise Java Beans (EJB) 3.0 Demos and code.

Slides:



Advertisements
Similar presentations
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
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
Introduction to Enterprise JavaBeans. Integrating Software Development Server-side Component Model Distributed Object Architecture –CORBA –DCOM –Java.
Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
Emmanuel Cecchet et al.  Performance Scalability of J2EE application servers.  Test effect of: ◦ Application Implementation Methods ◦ Container Design.
1 Lecture 18 George Koutsogiannakis/Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
CIS 764 Kansas State University
Introduction to JPA Java Persistence API Introduction to JPA.
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
1 The JNDI ENC and Injection Every EJB container that is deployed in an application server has its own personal internal registry called the Enterprise.
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.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Enterprise JavaBeans EJB Container Services. EJB container Enterprise JavaBeans are deployed in an EJB container within the application server EJB container.
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
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Entity Java Beans Jorg Janke Open Source ERP & CRM.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
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.
Java Transaction API Sean C. Sullivan
JBoss at Work JAW Motors Application Chapter 1-3 Jeff Schmitt October 9, 2006.
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
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.
Hitachi Consulting IOUG Collaborate ‘08 EJB 3.0 Java Persistence API (JPA) with Oracle TopLink Bill Lyons Systems Architect Hitachi Consulting
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.
Fall CIS 764 Database Systems Engineering L7. EJB’s.
Spring Database Access Ojitha Kumanayaka Copyright © Virtusa, 2007.
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
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.
Fall CIS 764 Database Systems Engineering L7. ADF + EJB Context: Oracle Application Development Framework …but … only EJB.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
MCS 270 Spring 2014 Object-Oriented Software Development.
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.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
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 Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
D Copyright © 2004, Oracle. All rights reserved. BMP Entity EJBs J2EE Connector Architecture.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Enterprise Java v121030Java EE Transactions1. Enterprise Java v121030Java EE Transactions2 Goals Understand the basic concepts behind a transaction Be.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
EJB Enterprise Java Beans JAVA Enterprise Edition
Java Object-Relational Layer Sharon Diskin GUS 3.0 Workshop June 18-21, 2002.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
Using Datastore with JDO 1. Setting up DataNucleus Access Platform 2. JDO class enhancement 3. POJOs and JDO Annotations 4. PersistencyManager and its.
1 EJB 3.0: There’s Something Different About You Martin Jäkle, TSBU Middleware.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
CS6320 – Java Persistence API
Objectives In this lesson, you will learn to:
Developing and testing enterprise Java applications
Enterprise Java Beans.
Objectives In this lesson, you will learn about:
Presentation transcript:

Persistence Models Margaret Smith

Overview Java Data Objects (JDO) Enterprise Java Beans (EJB) 3.0 Demos and code

What is JDO? The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence developed as Java Specification Request 12 (JSR 12) under the Java Community Process.

What is EJB 3.0? Enterprise Java Beans (EJB) 3.0 is a deep overhaul and simplification of the EJB specification. EJB 3.0's goals are to simplify development and focus more on writing plain old java objects (POJOs) rather than on complex EJB APIs. JSR-220 of the Java Community Process.

How are EJB and JDO related? Java EE or SE EJB 3.0 Hibernate TopLink JDO Other technologies… Plain old Java objects (POJOs) and dependency injection

Earlier EJB Versions vs EJB 3.0 Focus on plain Java classes Strong emphasis on Java metadata annotations eliminates need for deployment descriptor Annotations take the form and are used to express meta-data Specification of programmatic defaults Provides default database Less transactional code

Step 1: Design your domain/model classes as you would do normally Step 2: Define their persistence definition using Meta-Data Step 3: Compile your classes, and instrument them (using a JDO enhancer) Step 4: Generate the database tables where your classes are to be persisted Step 5: Write your code to persist your objects within the DAO layer Step 6: Run your application Step 1: Design your domain/model classes as you would do normally Step 2: Define their persistence definition using annotations as Meta-Data Step 3: Compile your classes Step 4: Run your application Optional step: specify a database other than the default Hsqldb database The JDO Process Creating an application The EJB 3.0 Process

Usage Models User interface input HSQLDB User interface input MySQL JDO ModelEJB 3.0 Model Server Database Driver Enhanced POJOs Annotated POJOs

Demos and code Store application where objects are Product and Book Plain java objects PersistenceManager object More error checking Investment calculator application where objects are Fund and EntityCalculator Plain annotated java objects EntityManager object JDOEJB 3.0

JDO: Schema file <!DOCTYPE jdo PUBLIC "-//Sun Microsystems, Inc.//DTD Java Data Objects Metadata 2.0//EN" " <class name="Book" identity-type="datastore" persistence-capable-superclass="org.jpox.tutorial.Product">

JDO: Persistence Manager private static PersistenceManager createPersistenceManager() throws IOException { Properties properties = new Properties(); InputStream is=Main.class.getClassLoader().getResourceAsStream("jpox.properties"); if (is == null) { throw new FileNotFoundException("Could not find jpox.properties file that defines the JPOX persistence setup."); } properties.load(is); PersistenceManagerFactory pmfactory = JDOHelper.getPersistenceManagerFactory(properties); PersistenceManager pm = pmfactory.getPersistenceManager(); return pm; }

JDO: Database interaction …. PersistenceManager pm = createPersistenceManager(); // Persistence of a Product and a Book. Transaction tx=pm.currentTransaction(); try{ tx.begin(); Product product = new Product("Sony Discman","A standard discman from Sony",49.99); Book book = new Book("Lord of the Rings by Tolkien","The classic story",49.99,"JRR Tolkien", " ", "MyBooks Factory"); pm.makePersistent(product); pm.makePersistent(book); tx.commit(); System.out.println("Product and Book have been persisted"); } finally{ if (tx.isActive()) { tx.rollback(); } pm.close(); } ….

EJB: Fund.java import javax.persistence.*; = "fund") public class Fund implements Serializable { private int id; private String name; private double growthrate; public Fund () { } public Fund (String name, double growthrate) { this.name = name; this.growthrate = public int getId () { return id; } public void setId (int id) { this.id = id; } EJB 3.0 requires a default constructor Denotes a primary key automatically generated by the server

EJB: EntityCalculator.java import trail.entity.beans.*; import javax.ejb.*; import javax.persistence.*; import java.sql.Timestamp; import public class EntityCalculator implements Calculator // (unitName="cal") protected EntityManager em; public void addInvestor (String name, int start, int end) { Investor investor = new Investor (name, start, end); em.persist (investor); } public void addFund (String name, double growthrate) { Fund fund = new Fund (name, growthrate); em.persist (fund); } …..Continued next slide Identifies class as a stateless session bean Injects an EntityManager object Saves entity bean instance to database as a new row

EJB: EntityCalculator.java public double calculate (int fundId, int investorId, double saving) { Investor investor = em.find(Investor.class, Integer.valueOf(investorId)); Fund fund = em.find(Fund.class, Integer.valueOf(fundId)); int start = investor.getStartAge(); int end = investor.getEndAge(); double growthrate = fund.getGrowthrate(); double tmp = Math.pow(1. + growthrate / 12., 12. * (end - start) + 1); double result = saving * 12. * (tmp - 1) / growthrate; Timestamp ts = new Timestamp (System.currentTimeMillis()); TimedRecord rec = new TimedRecord (fund, investor, saving, result, ts); em.persist (rec); return result; } …Continued next slide Find and retrive entity bean by using entity name and entity id

EJB: EntityCalculator.java public Collection getFunds () { return em.createQuery("from Fund f").getResultList(); } public Collection getInvestors () { return em.createQuery("from Investor p").getResultList(); } public Collection getRecords () { return em.createQuery("from TimedRecord r order by r.ts desc").getResultList(); } EJB QL statement; returns a collection of entity beans matched by query

EJB: Client code private EntityCalculator cal = null; public void jspInit () { try { InitialContext ctx = new InitialContext(); cal = (EntityCalculator) ctx.lookup ("EJB3Trail/EntityCalculator/local"); } catch (Exception e) { e.printStackTrace (); } public void service (Request req, Response rep) { // double res = cal.calculate(fund, investID, saving); } If the application is deployed in a EAR file, the default JNDI name is the EAR-FILE- BASE-NAME/EJB- CLASS-NAME/local for the stub for local interface; same for local and remote interfaces

Conclusion JDO EJB 3.0 Questions

System Requirements JDO Demo JPOX: Java Persistent Objects MySQL database MySQL Connector/J database driver EJB 3.0 Demo EJB 3.0 Trailblazer from JBoss JBoss EJB 3.0 module from JBoss (may be downloaded together with JBoss 4.0.4)

References EJB/JDO Persistence FAQ. Sun Developer Network (SDN). Version 2h 23-Feb JBoss Trailblazers and Demo Applications. JBoss: The Professional Open Source Company.. EJB 3 Trailblazer. JPOX rc-1 Out Now. JPOX: Java Persistent Objects. JPOX – Tutorial. JPOX: Java Persistent Objects. MySQL Connector/j. MySQL.