Mappings and Queries. with. Hibernate

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

Chapter 9: Object-Based Databases

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

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

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

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

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

Object-Relational Mapping

JAVA. Duration: 2 Months

GORM Data Services. November ocitraining.com

Chapter 12 Object and Object Relational Databases

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

Object-relational mapping EJB and Hibernate

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

Java Persistence Query Language (JPQL)

Week. Lecture Topic day (including assignment/test) 1 st 1 st Introduction to Module 1 st. Practical

Unit 6 Hibernate. List the advantages of hibernate over JDBC

Midterm Review. Winter Lecture 13

Lightweight J2EE Framework

object/relational persistence What is persistence? 5

Relational Data Processing. Everything Data CompSci 216 Spring 2019

Lightweight J2EE Framework

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

SQL: Structured Query Language Nested Queries

AN ADVANCED COURSE IN DATABASE SYSTEMS: BEYOND RELATIONAL DATABASES

O/R mapping with Hibernate

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

Chapter 9. A Look at HQL

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

CS W Introduction to Databases Spring Computer Science Department Columbia University

Graphical Joins in More Detail

Group A: Assignment No 2

Lecture 8. Monday, September 29, 2014

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

Slicing and Dicing Data in CF and SQL: Part 2

T-SQL Training: T-SQL for SQL Server for Developers

XML JavaScript Object Notation JSON Cookies Miscellaneous What Javascript can t do. OOP Concepts of JS

Databases - Classic Models

Object-Relational Mapping Tools let s talk to each other!

HIBERNATE MOCK TEST HIBERNATE MOCK TEST IV

The Relational Model

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

HIBERNATE MOCK TEST HIBERNATE MOCK TEST I

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Database Management Systems,

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Overview. Structured Data. The Structure of Data. Semi-Structured Data Introduction to XML Querying XML Documents. CMPUT 391: XML and Querying XML

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

Ryan Stephens. Ron Plew Arie D. Jones. Sams Teach Yourself FIFTH EDITION. 800 East 96th Street, Indianapolis, Indiana, 46240

Assignment 6: SQL III Solution

/ / JAVA TRAINING

Oracle NoSQL Database Getting Started with SQL for Oracle NoSQL Database. Release 18.3

Database Management

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

Selecting a Primary Key - 2

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

CO Java EE 6: Develop Database Applications with JPA

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

EE221 Databases Practicals Manual

Oracle 9i Application Development and Tuning

Introductory SQL SQL Joins: Viewing Relationships Pg 1

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

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

CHAPTER 11. Data Normalization

Introduction to Computer Science and Business

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

Inheritance and Polymorphism

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Homework Assignment 3. November 2nd, 2016 Due on November 14th, 12:30pm (noon) CS425 - Database Organization Results

RESTRICTING AND SORTING DATA

Database Programming Style Guidelines

Microsoft Exam

INTERMEDIATE SQL GOING BEYOND THE SELECT. Created by Brian Duffey

Making MongoDB Accessible to All. Brody Messmer Product Owner DataDirect On-Premise Drivers Progress Software

PDA Database Programming in PL/SQL (Oracle PL/SQL Developer Certified Associate Certification Course)

Creating Other Schema Objects

UML Modeling I. Instructor: Yongjie Zheng September 3, CS 490MT/5555 Software Methods and Tools

Full Stack Java Developer Course

MIS2502: Data Analytics Relational Data Modeling (2) Alvin Zuyin Zheng

Web Application Development Using JEE, Enterprise JavaBeans and JPA

CERTIFICATE IN WEB PROGRAMMING

SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

Oracle NoSQL Database Getting Started with SQL for Oracle NoSQL Database. Release


Oracle. Getting Started with SQL For Oracle NoSQL Database. 12c Release 1

Hibernate Overview. By Khader Shaik

8) A top-to-bottom relationship among the items in a database is established by a

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

SQL functions fit into two broad categories: Data definition language Data manipulation language

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

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

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

SQL: Structured Query Language Nested Queries. Return Types. .. Cal Poly CPE/CSC 365: Introduction to Database Systems Alexander Dekhtyar..

Chapter 18: Persistence

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

Web Application Development Using JEE, Enterprise JavaBeans and JPA

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming with SQL

Transcription:

Mappings and Queries with Hibernate

Mappings Collection mapping Mapping collection of values e.g. holidays, months Association mapping Mapping of relationships between two objects e.g. Account and AccountOwner Component mapping mapping of has a relationships e.g. Person has an Address

Revision Hibernate is an object-relational mapping framework Maps persistence operations between object models to relational databases Core elements in a Hibernate application are: Your Java objects The Hibernate object mapping files (Event.hbm.xml) The Hibernate configuration file (Hibernate.cfg.xml) Classes working with the Hibernate API (Session, Transaction) public class Event private String title; private Date date; private Set<Person> persons;

Example: The EventManager public class Event private String title; N public class Person private int age; private Date date; private Set<Person> persons; N private String firstname; private String lastname; private Set<String> emails; public class Address private String street; 1 1 private List<String> phonenumbers; private Address address; private int postalcode; private String city;

Collection mapping Collection properties must be declared as an interface type (Set, not HashSet) Hibernate provides built-in mapping for Set, Map, List, and more May contain basic types, custom types and references to other Hibernate objects (entities) Collections are represented by a collection table in the database Collection key: foreign key of owning object Collection element: object in the collection

Collection mapping Java object public class Person Collection declared as interface type // other properties private Set<String> emails; // get and set methods Hibernate mapping <class name= no.uio.inf5750.example.model.person table= persons > Refers to Java property Foreign key to owner Actual content of set <!-- other properties --> <set name= emails table= person_emails > <key column= person_id /> <element column= email type= string /> </set> </class>

Indexed collections All ordered collection mappings need an index column in the collection table to persist the sequence Index of List is always of type Integer, index of Map can be of any type

Indexed collection mapping public class Person List is an ordered type of collection // other properties private List<String> phonenumbers; // get and set methods List mapped to table Required mapping of index column <class name= no.uio.inf5750.example.model.person table= persons > <!-- other properties --> <list name= phonenumbers table= phone_numbers > <key column= person_id /> <list-index column= sort_order base= 0 /> <element column= phone_number type= string /> </list> </class>

Association mapping Hibernate lets you easily specify all kinds of associations between objects Uni/bidirectional one-to-many Uni/bidirectional many-to-many Representing associations with join tables makes the database schema cleaner Other alternatives Denormalized tables; Foreign Keys; Joins

Unidirectional one-to-many Set of Persons public class Event private Set<Person> persons; 1 N public class Person // other properties <class name= no.uio.inf5750.example.model.event table= events > Foreign key Event Foreign key Person The unique attribute ensures one-to-many relationship // id and other properties <set name= persons table= events_persons > <key column= event_id /> <many-to-many column= person_id class= no.uio.inf5750.example.model.person unique= true /> </set> </class> <class name= no.uio.inf5750.example.model.person table= persons > // id and other properties </class>

Unidirectional many-to-many Set of Persons public class Event private Set<Person> persons; N N public class Person // other properties Foreign key Event Foreign key Person Absence of unique attribute ensures many-to-many relationship <class name= no.uio.inf5750.example.model.event table= events > // id and other properties <set name= persons table= events_persons > <key column= event_id /> <many-to-many column= person_id class= no.uio.inf5750.example.model.person /> </set> </class> <class name= no.uio.inf5750.example.model.person table= persons > // id and other properties </class>

Bidirectional one-to-many Event reference... Set of Persons public class Event private Set<Person> persons; 1 N public class Person private Event event; The unique attribute ensures one-to-many relationship Specifies which join table to use for the association Refers to property in Java class <class name= no.uio.inf5750.example.model.event table= events > <set name= persons table= events_persons > <key column= event_id /> <many-to-many column= person_id class= no.uio.inf5750.example.model.person unique= true /> </set> </class> <class name= no.uio.inf5750.example.model.person table= persons > <join table= events_persons inverse= true > <key column= person_id /> <many-to-one column= event_id name= event /> </join> </class>

Bidirectional many-to-many Set of Events... Set of Persons public class Event private Set<Person> persons; N N public class Person private Set<Event> events; Absence of unique attribute ensures many-to-many relationship <class name= no.uio.inf5750.example.model.event table= events > <set name= persons table= events_persons > <key column= event_id /> <many-to-many column= person_id class= no.uio.inf5750.example.model.person /> </set> </class> <class name= no.uio.inf5750.example.model.person table= persons > Both sides can be inverse in many-to-many associations <set name= events table= events_persons inverse= true > <key column= person_id /> <many-to-many column= event_id class= no.uio.inf5750.example.model.event /> </set> </class>

The inverse property explained Bidirectional associations should be updated on both sides in the Java code! Hibernate maps many-relationships with a join table Hibernate must ignore one side to avoid constraint violations! Must be many-side on one-to-many, doesn t matter on many-to-many public class Event int id; Set<Person> persons; N N public class Person int id; Set<Event> events; events event_id events_persons event_id person_id persons person_id

Component mapping A component is an object saved as a value, not as a entity reference Doesn t have a table or identifier of its own Useful in UML modeling Composition combining simple objects/datatypes into complex ones Components do not support shared references Properties can be of any Hibernate type even nested components

Component mapping Component public class Address private String street; private int postalcode; private String city; public class Person private String firstname; private Address address; // get and set methods // get and set methods Class attribute is optional <class name= no.uio.inf5750.example.model.person table= persons > <property name= firstname /> Component mapping Property mapping <component name= address > <property name= street /> <property name= postalcode /> <property name= city /> </component> </class>

Queries The Query interface The Hibernate Query Language (HQL)

The Query interface You need a query when you don t know the identifiers of the objects you are looking for Used mainly to execute Hibernate Query Language queries Obtained from a Hibernate Session instance Provides functionality for: Parameter binding to named query parameters Retrieving lists of objects or unique objects Limiting the number of retrieved objects Query query = session.createquery( some_hql_query );

The Hibernate Query Language HQL is an object-oriented query language Syntax has similarities to SQL Not working agains tables and columns, but objects! Understands object-oriented concepts like inheritance Has advanced features like: Associations and joins Polymorphic queries Subqueries Expressions Reduces the size of queries

The from clause Simplest possible query, qualified class name auto-imported, will return all Person instances: from Person Convenient to assign an alias to refer to in other parts of the query: from Person as p Multiple classes may be desired. The alias keyword is optional: from Person p, Event e

The where clause Allows you to narrow the returned list, properties can be referred to by name: from Person where firstname= John If there is an alias, use a qualified property name: from Person p where p.lastname= Doe Compound path expressions are powerful: from Person p where p.address.city= Boston

Expressions In clause: from Person p where p.firstname in ( John, Tom, Greg ) Between and not clause: from Person p where p.lastname not between D and F Size clause: from Person p where size ( p.address ) > 2

Query examples HQL query with named query parameter (age) Query obtained from Session public Collection<Person> getpersonsbyage( int age, int maxresults ) Session session = sessionfactory.getcurrentsession(); String hql = "from Person where age = :age"; Query query = session.createquery( hql ); Age parameter binding query.setinteger( "age", age ); Max nr of objects restriction query.setmaxresults( maxresults ); return query.list(); Returns the result as a List

Query examples HQL query with named query parameters Create query and pass in HQL string as parameter public Person getperson( String firstname, String lastname ) Session session = sessionfactory.getcurrentsession(); String hql = "from Person where firstname = :firstname + and lastname = :lastname"; Query query = session.createquery( hql ); Parameter binding with the setstring methods uniqueresult offers a shortcut if you know a single object will be returned query.setstring( "firstname", firstname ); query.setstring( "lastname", lastname ); return (Person) query.uniqueresult();

Resources Books on Hibernate Christian Bauer and Gavin King: Java Persistence with Hibernate Christian Bauer and Gavin King: Hibernate in Action The Hibernate reference documentation www.hibernate.org