object relational mapping contact: lalit.bhatt@gmail lalitbhatt

25
Object Relational Mapping Contact: [email protected] http://www.lalitbhatt.com 1

Upload: dima

Post on 18-Mar-2016

59 views

Category:

Documents


0 download

DESCRIPTION

Object Relational Mapping Contact: [email protected] http://www.lalitbhatt.com. Persistence. Persistence Capability to preserve data beyond the lifecycle of an application. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Object Relational Mapping

Contact: [email protected]://www.lalitbhatt.com

1

Page 2: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Persistence Capability to preserve data beyond the lifecycle of an

application. Relational concept of data management first introduced by E.F.

Codd in 1970 has proven to be the best way of managing persistence.

Persistence

http://www.lalitbhatt.com 2

Page 3: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Most of non trivial application needs a persistence mechanism to store the data. This is true for Java based systems also.

Java based systems follow OO paradigm and have to deal with Relational paradigm to interact with database.

At low level JDBC helps us in dealing with database however this is a non OO way of handling databases which brings mismatch also known as OO-Relational impedence.

Persistence using Java

http://www.lalitbhatt.com 3

Page 4: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Leakage of concern – Persistence is a cross cutting concern so should not leak into domain model.

ORM provides transparent and automated persistence. ORM does not require any interface to be implemented or any

super class to be extended. Persistent class can be reused outside persistence context as

they are simple POJO’s. This helps in moving data to and from UI layer and improves testability.

ORM provides transparent persistence. The persistent class are not responsible for the persistence behavior. There job is capture the domain data.

However ORM also do not provide complete transparent persistence, though the impact is minimal

Why ORM

http://www.lalitbhatt.com 4

Page 5: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Object Relational Mapping

ORM

RelationalDatabase

OO WorldORM

save objects

Sql for update/insert

Result Set

objects

http://www.lalitbhatt.com 5

Page 6: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Granularity Sub Types Polymorphism Inheritance

Object relational mismatch

http://www.lalitbhatt.com 6

Page 7: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Granularity

Object relational mismatch

User

Address

USER

NAMESTREET

CITY

http://www.lalitbhatt.com 7

Page 8: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

SubTypes Inheritance and Polymorphism

Object relational mismatch

User

Customer Employee

http://www.lalitbhatt.com 8

Page 9: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Identity In database the identity is maintained by primary key. In Java identity is maintained as equivalent to Object

equality or overriding equals method. Database has only one row at any point of time to

represent a record. In Java there might be multiple objects at the same time

representing the same notion of data.

Object relational mismatch

http://www.lalitbhatt.com 9

Page 10: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Associations : In OO-world association is maintained using references but in Relational world association is maintained using foreign key references.

In OO-world association are unidirectional. If bidirectional association needs to be maintained, than references need to be kept on both side.

Object relational mismatch

http://www.lalitbhatt.com 10

Page 11: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Data Navigation: In Java navigation can be done by navigating the relationships, whereas in relational world we need joins to reach to particular set of data.

Object relational mismatch

http://www.lalitbhatt.com 11

Page 12: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

ORM tools propose to solve this mismatch problem. They provide the OO-world semantics to develop and take

care of mapping the data to relational semantics. They provide flexibility to work with more than one

databases by taking care of vendor specific features.

ORM tools

http://www.lalitbhatt.com 12

Page 13: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Provide API for basic CRUD operation. Query language. Mapping mechanism between classes and tables and

relationships. Other features like dirty checking, lazy association fetching.

ORM tools Feature Set

http://www.lalitbhatt.com 13

Page 14: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

JPA stands for Java Persistence API Hibernate supports JPA and is an implementation provider Hibernate has its own set of api’s

Hibernate vs JPA

http://www.lalitbhatt.com 14

Page 15: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Handle relationships carefully: Build bidirectional if it is needed.

Have an eye on your session size.

Good Practices on Hibernate

http://www.lalitbhatt.com 15

Page 16: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Lazy loading Get vs Load

Good Practices on Hibernate

Persistence Context

Studentproxy

http://www.lalitbhatt.com 16

Page 17: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Lazy loading of collections By default all associated entities and collections aren’t

initialized. A collection can be eagerly loaded always

@OneToMany(fetch= FetchType.EAGER)public Collection<PhoneEntity> getPhoneEntityList() {

return phoneEntityList;}

Good Practices on Hibernate

http://www.lalitbhatt.com 17

Page 18: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Disabling Proxies Proxies can be disabled for a class

@Entity(name="Student")@org.hibernate.annotations.Proxy(lazy=false)public class Student {

Usually a bad idea if using hibernate JPA does not have any concept of proxies. Disabling proxies will make the collection of student

to also load eagerly.

Good Practices on Hibernate

http://www.lalitbhatt.com 18

Page 19: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Fetching Strategies

Fetching in batchesWill issue a query for each phoneEntity listBatch size can be used to reduce the number of hits to database

Specifying batch@[email protected](size=10)public class PhoneEntity {

Good Practices on Hibernate

http://www.lalitbhatt.com 19

Page 20: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Fetching Strategies

Subselect can force all the collections to load in one shot, whenever you initialize the first collection

@OneToMany( cascade={CascadeType.ALL})@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SUBSELECT)public Collection<PhoneEntity> getPhoneEntityList() {return phoneEntityList;}

Good Practices on Hibernate

http://www.lalitbhatt.com 20

Page 21: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Fetching Strategies

Select mode will bring all the data in one select.

@OneToMany( fetch = FetchType.EAGER)public Collection<PhoneEntity> getPhoneEntityList() {return phoneEntityList;

}

Uses outer joinIf we have another collection set at SELECT in PhoneEntity class it will lead to Cartesian problemThe depth of fetch is determined by max_fetch_depth

Good Practices on Hibernate

http://www.lalitbhatt.com 21

Page 22: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Fetching Strategies

Select mode will bring all the data in different select.

@OneToMany( cascade={CascadeType.ALL})@org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT

)public Collection<PhoneEntity> getPhoneEntityList() {return phoneEntityList;

}

Good Practices on Hibernate

http://www.lalitbhatt.com 22

Page 23: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Fetching Strategies

Collection can be fetched eagerly while there mapping is lazy = true

select s from Student s left join fetch s.phoneEntityList

This is the preferred way to handle collections

Good Practices on Hibernate

http://www.lalitbhatt.com 23

Page 24: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Caching

Good candidates for caching Data that changes rarely Non critical data Data that is local to the application and not

shared

Good Practices on Hibernate

http://www.lalitbhatt.com 24

Page 25: Object Relational Mapping Contact:  lalit.bhatt@gmail lalitbhatt

Thank you

http://www.lalitbhatt.com 25