Java Object/Relational Persistence with Hibernate. David Lucek 11 Jan 2005

Similar documents
Hibernate Overview. By Khader Shaik

Hibernate Interview Questions

indx.qxd 11/3/04 3:34 PM Page 339 Index

object/relational persistence What is persistence? 5

Lightweight J2EE Framework

POJOs to the rescue. Easier and faster development with POJOs and lightweight frameworks

International Journal of Advance Research in Engineering, Science & Technology HIBERNATE FRAMEWORK FOR ENTERPRISE APPLICATION

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

find() method, 178 forclass() method, 162 getcurrentsession(), 16 getexecutablecriteria() method, 162 get() method, 17, 177 getreference() method, 178

HIBERNATE MOCK TEST HIBERNATE MOCK TEST I

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Introduction to JPA. Fabio Falcinelli

POJOs in Action DEVELOPING ENTERPRISE APPLICATIONS WITH LIGHTWEIGHT FRAMEWORKS CHRIS RICHARDSON MANNING. Greenwich (74 w. long.)

O/R mapping with Hibernate

/ / JAVA TRAINING

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

Topics in Enterprise Information Management

Generating A Hibernate Mapping File And Java Classes From The Sql Schema

SDN Community Contribution

Setting Schema Name For Native Queries In. Hibernate >>>CLICK HERE<<<

Efficient Object-Relational Mapping for JAVA and J2EE Applications or the impact of J2EE on RDB. Marc Stampfli Oracle Software (Switzerland) Ltd.

Struts: Struts 1.x. Introduction. Enterprise Application

The Object-Oriented Paradigm. Employee Application Object. The Reality of DBMS. Employee Database Table. From Database to Application.

CO Java EE 6: Develop Database Applications with JPA

Table of Contents. I. Pre-Requisites A. Audience B. Pre-Requisites. II. Introduction A. The Problem B. Overview C. History

Chapter 2. Introduction to Mapping

Object Persistence and Object-Relational Mapping. James Brucker

Introduction to Session beans. EJB - continued

J2EE Persistence Options: JDO, Hibernate and EJB 3.0

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

Advanced Web Systems 9- Hibernate annotations, Spring integration, Aspect Oriented Programming. A. Venturini

Module 8 The Java Persistence API

Pro JPA 2. Mastering the Java Persistence API. Apress* Mike Keith and Merrick Schnicariol

Object-relational mapping EJB and Hibernate

Database Application Architectures

Supports 1-1, 1-many, and many to many relationships between objects

Unit 6 Hibernate. List the advantages of hibernate over JDBC

High Performance Persistence

Web Application Development Using Spring, Hibernate and JPA

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Fast Track to EJB 3.0 and the JPA Using JBoss

JPA The New Enterprise Persistence Standard

Architecture overview

Entities are classes that need to be persisted, usually in a relational database. In this chapter we cover the following topics:

Index. setmaxresults() method, 169 sorting, 170 SQL DISTINCT query, 171 uniqueresult() method, 169

Web Application Development Using Spring, Hibernate and JPA

Java EE Architecture, Part Three. Java EE architecture, part three 1(69)

Lightweight J2EE Framework

Enterprise Features & Requirements Analysis For EJB3 JPA & POJO Persistence. CocoBase Pure POJO

JAVA SYLLABUS FOR 6 MONTHS

Java EE Architecture, Part Three. Java EE architecture, part three 1(57)

Java and Databases. PPJDG, May 2004 Chris Smith, MindIQ

TOPLink for WebLogic. Whitepaper. The Challenge: The Solution:

Hibernate in Action CHRISTIAN BAUER GAVIN KING MANNING. Greenwich (74" w. long.)

Web Application Development Using Spring, Hibernate and JPA

Page 1

Enterprise JavaBeans, Version 3 (EJB3) Programming

By Philip Japikse MVP, MCSD.NET, MCDBA, CSM, CSP Principal Consultant Pinnacle Solutions Group

com Spring + Spring-MVC + Spring-Boot + Design Pattern + XML + JMS Hibernate + Struts + Web Services = 8000/-

Hibernate Quickly by Patrick Peak and Nick Heudecker Chapter 3

Installing MySQL. Hibernate: Setup, Use, and Mapping file. Setup Hibernate in IDE. Starting WAMP server. phpmyadmin web console

Understanding Impact of J2EE Applications On Relational Databases. Dennis Leung, VP Development Oracle9iAS TopLink Oracle Corporation

Java Enterprise Edition

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Wake up to Hibernate. John Mallonee Highmark, Inc.

ADVANCED JAVA TRAINING IN BANGALORE

TopLink Grid: Scaling JPA applications with Coherence

Apache OpenJPA User's Guide

Building Java Persistence API Applications with Dali 1.0 Shaun Smith

Apache OpenJPA User's Guide

DATA ACCESS TECHNOLOGIES FOR JAVA GENERAL STUDY

Spring Persistence. with Hibernate PAUL TEPPER FISHER BRIAN D. MURPHY

Table of Contents EJB 3.1 and JPA 2

Using Caché Java with Third-party Persistence Frameworks

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

.NET Database Technologies. Entity Framework: Queries and Transactions

1Z Oracle. Java Enterprise Edition 5 Enterprise Architect Certified Master

Object-Relational Mapping

Entity LifeCycle Callback Methods Srikanth Technologies Page : 1

Spring Framework 2.0 New Persistence Features. Thomas Risberg

Chapter 3. Harnessing Hibernate

Step By Step Guideline for Building & Running HelloWorld Hibernate Application

SUN Sun Certified Enterprise Architect for J2EE 5. Download Full Version :

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13

Java Persistence API (JPA) Entities

Development of E-Institute Management System Based on Integrated SSH Framework

CORE JAVA 1. INTRODUCATION

Java- EE Web Application Development with Enterprise JavaBeans and Web Services

The dialog boxes Import Database Schema, Import Hibernate Mappings and Import Entity EJBs are used to create annotated Java classes and persistence.

Hibernate in close action. INF5750/ Lecture 3 (Part III)

Not just an App. Server

Improve and Expand JavaServer Faces Technology with JBoss Seam

Migrating a Classic Hibernate Application to Use the WebSphere JPA 2.0 Feature Pack

purequery Deep Dive Part 2: Data Access Development Dan Galvin Galvin Consulting, Inc.

Migrating traditional Java EE applications to mobile

J2EE Access of Relational Data

Polyglot Persistence. EclipseLink JPA for NoSQL, Relational, and Beyond. Shaun Smith Gunnar Wagenknecht

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Erik Dörnenburg JAOO 2003

Sybase. Strategic Developer. Karl Reti & John Graham February 28, 2005

Transcription:

Java Object/Relational Persistence with Hibernate David Lucek 11 Jan 2005

Object Relational Persistence Maps objects in your Model to a datastore, normally a relational database. Why? EJB Container Managed Persistence (CMP) is Dead. The business logic and domain are normally represented as an object model. The developer should concentrate and work with the object model Persistence code is complex to write in-house Java types vs SQL datatype mismatch Java Collections versus SQL Joins

Object Relational Mapping (ORM) Tools A good ORM tools should provide Transparent Persistence Transitive Persistence Automatic dirty checking Support for Inheritance Efficient fetching and caching Transparent Persistence means any class can be persisted without: Any implementation of specific interfaces No required persistence superclass A good ORM should be able to be run outside of a container. Good for testing.

Popular Object Relational Mapping Tools TopLink, Oracle Tool JDO, POJO persistence, Official POJO persistence standard Kodo JDO from SolarMetric is popular There are many JDO vendors Castor Open Source Hibernate Open Source EJB 3.0 will support POJO, probably too late?

The Transparent Persistence Goal The goal is to write a POJO and let a toolkit provide the persistence. We are not there yet, but close JDO and Hibernate provide transparent persistence They are competing toolkits in base functionality Hibernate at this point has more advanced query capability and raw SQL support. But, the JDO 2.0 Spec is coming out soon, which addresses these concerns

Why Hibernate Its Open Source (LGPL) Mature toolkit, supported by JBoss Very popular Web Site: www.hibernate.org Latest Stable Version: 2.1.7 Hibernate 3.0 Beta coming out soon. Lots of resources on the web

Main Hibernate Features POJO persistence Support for fine grained objects Flexible Mapping (table to object mapping) Two layer Cache Architecture Support for Detached Objects High Performance Queries Eclipse IDE Plug-in Round trip toolkits 3 Query Options

Basic Hibernate Usage Work Flow 1)Create Java Object Model 2)Create mapping file, hibernate.cfg.xml 1)Mappings POJO to a SQL Table 2)Add your JDBC connection settings here or in a separate hibernate.properties file 3)Use the persistence manager API to save/delete or update your object. XDoclet Tags and Ant can be used to automate. This is the most common approach

Basic Hibernate POJO Must use JavaBean Specification with Accessor methods No-arg Constructor Collections must use an Interface Add XDoclet tags to get methods for persistence. Add class level tag for table name. Can use sequence, either from database or Hibernate generated.

POJO Example /** * @hibernate.class table="dvds" */ public class DVD implements Serializable { private Long id; private String title; private int type; private Account account; // Parent Object Dvds() {} /** * @hibernate.id generator-class="sequence" type="long" column="id" * @hibernate.generatorparam name="sequence" value="dvdseq" */ public Long getid() { return this.id; } /** * @hibernate.property column="title" */ public String gettitle() { return this.title; } /** @hibernate.property column="type" */ public int gettype() { return this.type; } }

Example Mapping Document <hibernate-mapping> <class name="dvd" table="dvds"> <id name="id" column="id" type="long" > <generator class="sequence"/> <param name="sequence">dvdseq</param> </generator> </id> <property name="title" column="title" type="string" length="15" not-null="true"/> <property name="type" column="type" type="short" notnull="true"/> </class> </hibernate-mapping>

Persistence API Usage API Order Get Session Begin Transaction Save your object by calling save(), saveorupdate() Commit and close transaction Normally this should be wrapped in a DAO layer

API Usage Example - Saving // Create business Object DVD dvd = new DVD(); dvd.settitle( Shrek 2 ); dvd.settype(1); // Single layer Session session = getsessionfactory().opensession(); Transaction txt = session.begintransaction(); // Saves Object to database session.save(dvd); tx.commit(); session.close();

API Usage Loading/Retrieving Can load by Key or Query. Loading by Key: Long id = xxx; DVD dvd = session.get(dvd.class,id); Hibernate Query Language (HQL) Native Hibernate Language Can also use native SQL if desired List dvdslist = session.find( from DVD ); // Walk through list as required

Hibernate Associations Defines relationships in the object model Relationship are automatically saved. (Transitive Persistence) Example: An Account can have many DVDs An Account can have 1..* DVDs

Association Example class Account { private Long id; private Set dvds = new HashSet(); Account() {} public Set getdvds() { return this.dvds; } public adddvd(dvd dvd) { // Set the Parent Reference dvd.setaccount(this); // Add to list dvds.add(dvd): } }

Association Mappings Account Mapping <class name= Account table= account >... <set name= dvds > <key column= id /> <one-to-many class= DVD /> </set> </class> DVD Mapping <class name= DVD table= dvds >...... <many-to-one name=account column= account class = Account not-null= true /> </class>

API Example of Transitive Persistence DVD dvd = new DVD(); dvd.settitle( Kill Bill ); Transaction tx = session.gettransaction(); Long id = xxx; Account acct = (Account) session.get(account.class,id); acct.adddvd(dvd); // This will auto-magically save the DVD object. tx.commit(); session.close();

Hibernate Query's Three types 1) Hibernate Query Language (HQL) Object orientated version of SQL Has full support for inner/outer joins Support for aggregation and grouping Supports sub queries 2)Criteria queries (QBC) Allows creation and execution of object orientated criteria queries. 3) SQL queries Direct SQL pass through to the database Can name the SQL queries in the meta data

Other Hibernate Features Detached Objects Get an object, close database Session Serialize Object to Web Tier Web Tier sends back the Object and it is reattached to it parent and saved. Eliminates DTOs Automatic Versioning Provides optimistic locking based on timestamp or number.