Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.

Slides:



Advertisements
Similar presentations
Introduction to NHibernate By Andrew Smith. The Basics Object Relation Mapper Maps POCOs to database tables Based on Java Hibernate. V stable Generates.
Advertisements

Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
Session-02.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
An Introduction to Hibernate Matt Secoske
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
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
Maven for building Java applications By Nalin De Zoysa
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.
Javax.sql and java.sql. java.sql Interface Connection public interface Connection extends WrapperWrapper A connection (session) with a specific database.
IS-907 Java EE JPA: Simple Object-Relational Mapping.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
ALMA Integrated Computing Team Coordination & Planning Meeting #1 Santiago, April 2013 Relational APDM & Relational ASDM models effort done in online.
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
SQLite Supported by BlackBerry OS 5.0 Using SQLite.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1 Mapping Persistent Objects Entities represent data in the database, so changes to an entity bean result in changes to the database. That's ultimately.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
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.
Database structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
Session 1 Module 1: Introduction to Data Integrity
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.
Programmation des Applications Internet Internet Application Programming © - Last update: Friday, 05 February
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
Chapter 3: Relational Databases
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Ch 7. Working with relational data. Transactions Group of statements executed as a group. If all statements execute successfully, changes are committed.
Neo.NET Entity Objects Architecture and Implementation Copyright © Erik Dörnenburg – Last updated: December 2004.
Hibernates - Many to One Association. May 12, 2011 What is Association? Association is the relation between two objects. One class holds a reference of.
MANY-TO-MANY MAPPING. May 12, 2011  Implemented using a Set java collection  Mediator table is mandatory – Join table  Join table contains foreign.
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
In this session, you will learn to: Manage databases Manage tables Objectives.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
OpenAccess ORM Advanced Topics Kevin Babcock April 9, 2009.
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
“Manipulating Data” Lecture 6.
CIS 136 Building Mobile Apps
Structured Query Language
SQL DATA CONSTRAINTS.
“Manipulating Data” Lecture 6.
JPA: Mapping to an existing database
Topics Persistence API for JAVA EE5 Java Transaction API Entity class.
Presentation transcript:

Hibernate Thuy, Le Huu. Pentalog VN

Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic update – Hibernate mutable – Different between session.get() and session.load() – Caching in hibernate Integrate Hibernate with Other Frameworks

Annotations So far you have seen how Hibernate uses XML mapping file for the transformation of data from POJO to database tables Hibernate annotations is the newest way to define mappings without a use of XML file

Annotations = "stock") public class Stock implements = = "STOCK_ID") public Integer getStockId() { return this.stockId; }

Annotations package com.pentalog.ginet.domain; = "EMPLOYEE") public class @Column(name = "id") private int = "first_name") private String = "last_name") private String = "salary") private int salary; public Employee() {} public int getId() { return id; } public void setId( int id ) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName( String first_name ) { this.firstName = first_name; } public String getLastName() { return lastName; } public void setLastName( String last_name ) { this.lastName = last_name; } public int getSalary() { return salary; } public void setSalary( int salary ) { this.salary = salary; } JPA annotations in javax.persistence.* Annotation: – Marks this class as an entity bean, so it must have a no-argument constructor that is visible with at least protected Annotation: – allows you to specify the details of the table that will be used to persist the entity in the Annotations: – Each entity bean will have a primary Annotation: – annotation is used to specify the details of the column to which a field or property will be mapped.

Lazy loading Lazy setting decides whether to load child objects while loading the Parent Object – Address child of User class can be made lazy if it is not required – May need to load the Author object for Book parent whenever you deal with the book for online bookshop – Default lazy loading is TRUE

Lazy loading This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling getChild() method on parent In this case hibernate issues a fresh database call to load the child when getChild() is actually called on the Parent object

Lazy = FetchType.LAZY, mappedBy = "stock") – fetch = FetchType.LAZY – fetch = – LazyCollectionOption.FALSE – LazyCollectionOption.TRUE – LazyCollectionOption.EXTRA

Fetching Strategies Hibernate fetching strategies are used to optimize the Hibernate generated select statement – fetch-”join” = Disable the lazy loading, always load all the collections and entities. – fetch-”select” (default) = Lazy load all the collections and entities. – batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*. – fetch-”subselect” = Group its collection into a sub select statement.

Dynamic insert Using dynamic-insert to avoid the include unmodified properties in the SQL INSERT statement Default: dynamic-insert=false

Dynamic update Using dynamic-insert to avoid the include unmodified properties in the SQL UPDATE statement Default: dynamic-update=false

Hibernate mutable Using mutable keyword to avoid the generate unnecessary SQL statements mutable = “false” is declared in class – In Class with mutable=”false” – insert=allow, delete=allow, update=not allow mutable = “false” is declared in collection – In Collection with mutable=”false” – insert=not allow, delete-orphan=not allow, delete=allow, update=allow

session.get() and session.load() Understanding when should use get or load to retrieve the object in order to avoid unnecessary hit to the database. Session.load() – It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. – If no row found, it will throws an ObjectNotFoundException Session.get() – It always hit the database and return the real object, an object that represent the database row, not proxy. – If no row found, it return null.

Caching The first-level cache is the Session cache and is a mandatory cache through which all requests must pass – Objects are cached within the current session and they are only alive until the session is closed Second level cache is an optional cache and first-level cache will always be consulted before any attempt is made to locate an object in the second-level cache – Query-level cache

Caching Concurrency strategies – Read-only – Nonstrict-read-write – Read-write – Transactional