Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic."— Presentation transcript:

1 Hibernate Thuy, Le Huu. Pentalog VN

2 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

3 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

4 Annotations @Entity @Table(name = "stock") public class Stock implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "STOCK_ID") public Integer getStockId() { return this.stockId; }

5 Annotations package com.pentalog.ginet.domain; import javax.persistence.*; @Entity @Table(name = "EMPLOYEE") public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = "id") private int id; @Column(name = "first_name") private String firstName; @Column(name = "last_name") private String lastName; @Column(name = "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.* package @Entity Annotation: – Marks this class as an entity bean, so it must have a no-argument constructor that is visible with at least protected scope @Table Annotation: – allows you to specify the details of the table that will be used to persist the entity in the database @Id and @GeneratedValue Annotations: – Each entity bean will have a primary key @Column Annotation: – The @Column annotation is used to specify the details of the column to which a field or property will be mapped.

6 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

7 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

8 Lazy loading @OneToMany(fetch = FetchType.LAZY, mappedBy = "stock") – fetch = FetchType.LAZY – fetch = FetchType.EAGER @LazyCollection(LazyCollectionOption.FALSE) – LazyCollectionOption.FALSE – LazyCollectionOption.TRUE – LazyCollectionOption.EXTRA

9 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. – @Fetch(FetchMode.SELECT) – @BatchSize(size=6)

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

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

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

13 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.

14 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

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


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

Similar presentations


Ads by Google