Hibernate online tutorials for beginers

47
QUONTRA SOLUTIONS http://www.quontrasolutions.com Call us : (404)-900-9988 Hibernate Tutorials

description

This Hadoop tutorial provides a short introduction into working with big data in Hadoop via the Hortonworks Sandbox, HCatalog, Pig and Hive. Hi,We offer online IT Trainings with Placements, Project Assistance in different platforms with real time Industry Consultants to provide quality training for all IT professionals, corporate clients and students etc.. Other Courses we offered: DATABASE: SQL/PL-SQL MICROSOFT: ADO .NET, ASP .NET, C# .NET, MSBI, SharePoint, Vb .NET PROGRAMMING: Core Java, Advanced Java, J2EE, Hibernates, Strutus, Java Scripting, Perl Scripting, Shell Scripting, Springs, Ruby on Rails Mobile Apps: Android, IOS Training, Cloud Computing, Networking, Unix Admin, Linux, Sun Solaris, Testing Tools: Manual Testing, QTP, Selenium Selenium: SalesForce Developer, SalesForce Administrator BUSINESS ANALYST, HADOOP QUONTRASOLUTIONS Email: [email protected] Call Now: US: +1 404-900-9988. UK: (20)3734 1498.

Transcript of Hibernate online tutorials for beginers

Page 1: Hibernate online tutorials for beginers

QUONTRA SOLUTIONS

http://www.quontrasolutions.com

Call us : (404)-900-9988

Hibernate Tutorials

Page 2: Hibernate online tutorials for beginers

1. Hibernate Introduction2. Why to use Hibernate3. Steps to use/configure hibernate in an application4. simple case study 5. Transaction in Hibernate6. Using Criteria in Hibernate 7. Caching in Hibernate

CONTENT

Page 3: Hibernate online tutorials for beginers

Hibernate Introduction:

Hibernate is a powerful, high performance object/relational persistence and query service. Hibernate lets you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.  Object/Relational Mapping Hibernate Dual-Layer Cache

Architecture Highly scalable architecture J2EE integration Object Relational tool ( JDO, Hibernate, and iBatis SQL Maps,

TopLink) Hibernate supports these following databases:

Page 4: Hibernate online tutorials for beginers

Why to use Hibernate

In traditional approach:

o Too many SQL statements

o Manually handled associations

o Database dependent

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 5: Hibernate online tutorials for beginers

Hibernate architecture

Page 6: Hibernate online tutorials for beginers

Steps to use/configure hibernate in an application:

A. Installing the Hibernate core and support JAR libraries into your project

B. Creating a Hibernate.cfg.xml file to describe how to access your database

C. Selecting appropriate SQL Dialect for the database.

D. Creating individual mapping descriptor files for each persistable Java classes

Page 7: Hibernate online tutorials for beginers

A. Install the Hibernate core and JAR libraries

Page 8: Hibernate online tutorials for beginers

B. Creating a Hibernate.cfg.xml :

Before Hibernate can retrieve and persist objects for us, we need to tell it the settings about our application. For example,

which kind of objects are persistent objects? Which kind of database are we using? How to connect to the database? What is the size of Connection pool?

There are three ways to configure Hibernate in total:

1.      XML configuration

2.       programmatic configuration

3.       properties file configuration.

Page 9: Hibernate online tutorials for beginers

XML Configuration

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 10: Hibernate online tutorials for beginers

C. Selecting appropriate SQL Dialect for the database. The dialect property determines the dialect to be used when generating queries. Hibernate supports dialects for all popular Relational Database Management Systems (RDBMS), such as DB2 or Oracle™. Therefore, for example, if you use Oracle during development and want to move to DB2 in production, changes will only be required in the hibernate.cfg.xml file.

Page 11: Hibernate online tutorials for beginers

A.    Creating individual mapping/descriptor files for each persistable Java classes/Table in DB

JAVA XML RDBMS POJO Class HBM File A Table

Page 12: Hibernate online tutorials for beginers

A Table in Database

   

CREATE TABLE BOOK ( 

ISBN VARCHAR(50) NOT NULL, 

NAME VARCHAR(100) NOT NULL, 

PRICE INT NOT NULL, 

PRIMARY KEY (ISBN) );

Page 13: Hibernate online tutorials for beginers

Java (POJO):

Page 14: Hibernate online tutorials for beginers

Hibernate-mapping: (*.hbm.xml)

0

Page 15: Hibernate online tutorials for beginers

4 simple case study

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 16: Hibernate online tutorials for beginers

simple case study

Creating global session factory:For an application using Hibernate as O/R mapping framework, a global session factory should be created and accessed through a particular interface. Here we use a static variable for storing the session factory. It is initialized in a static block when this class is loaded for the first time.

Page 17: Hibernate online tutorials for beginers

Using the SessionFactory in Application: SessionFactory factory = HibernateUtil.getSessionFactory(); Session session = factory.openSession();  

try { //…………………..// Using the session to retrieve objects

//………………………………. } finally {

session.close(); }

SessionFactorys are immutable. The behaviour of a SessionFactory is controlled by properties supplied at configuration time.

Page 18: Hibernate online tutorials for beginers

Retrieving objects 

Book book = (Book) session.load(Book.class, isbn);orBook book = (Book) session.get(Book.class, isbn); 

What’s the difference between load() and get() ?

The first difference is that when the given ID could not be found, load() will throw an exception “org.hibernate.ObjectNotFoundException”, while get() will return a null object.

The second difference is that load() just returns a proxy by default and database won’t be hit until the proxy is first invoked. The get() will hit the database immediately.

Page 19: Hibernate online tutorials for beginers

Using HQL (Hibernate Query Language):

If you are sure that there will be only one object matching, you can use the uniqueResult() method to retrieve the unique result object.

So, the code would appear as:

Page 20: Hibernate online tutorials for beginers

persisting Objects:

 For saving a newly created object, we can use the save() method. Hibernate will issue an INSERT statement. session.save(book);

 For updating an existing object, we can use the update() method. Hibernate will issue an UPDATE statement. session.update(book);

 For deleting an existing object, we can use the delete() method. Hibernate will issue a DELETE statement. session.delete(book);

Page 21: Hibernate online tutorials for beginers

Id Generation in Hibernate

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 22: Hibernate online tutorials for beginers

ID generation in Hibernate There are three approaches to set ID: 

a. Sequence

b. Identity

c. Native

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 23: Hibernate online tutorials for beginers

ID generation in Hibernate There are three approaches to set ID: a.Sequence

To generate an ID is to use an auto-incremented sequence number. For some kinds of databases (including HSQLDB), we can use a sequence/generator to generate this sequence number:

Page 24: Hibernate online tutorials for beginers

b. Identity:

To generate an auto-incremented sequence number is to use an identity column of a table.

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 25: Hibernate online tutorials for beginers

c. Native:

most suitable strategy to use for your database

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 26: Hibernate online tutorials for beginers

primary key generation using multiple columns:

Table:

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 27: Hibernate online tutorials for beginers

Java (POJO Class)

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 28: Hibernate online tutorials for beginers

Mapping (an HBM file)

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 29: Hibernate online tutorials for beginers

5. Transaction in Hibernate:

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 30: Hibernate online tutorials for beginers

Transaction in Hibernate:

Page 31: Hibernate online tutorials for beginers

Example: select

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 32: Hibernate online tutorials for beginers

Example: update

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 33: Hibernate online tutorials for beginers

Example: delete

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 34: Hibernate online tutorials for beginers

6 Using Criteria in Hibernate

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 35: Hibernate online tutorials for beginers

Sometimes we need to build up a query dynamically in our application, e.g. for an advanced search function.“Criteria Queries” is an alternative way of HQL query.The traditional method of doing this is to generate a HQL statement, or SQL statement if not using Hibernate, by string concatenation. The problem for this method is making your code hard to maintain because of the hard reading statement fragments.

Traditinal approach:

if (startDate != null) { if (firstClause) {

query = query + " where ";} else {

query = query + " and "; query += " s.date >= '" + startDate + "'";}// And so on... Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 36: Hibernate online tutorials for beginers

Hibernate Criteria:

Criteria criteria = session.createCriteria(Book.class);

if (startDate != null) {criteria.add(Expression.ge("date",startDate);

}

if (endDate != null) {criteria.add(Expression.le("date",endDate);

}

List results = criteria.list();

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 37: Hibernate online tutorials for beginers

SELECT * FROM ORDERS WHERE ORDER_ID=’1092’;

In Cretira it would become: List orders= session.createCriteria(Order.class) .add(Restrictions.eq(“orderId”,”1092”)) .list();

SELECT O.*, P.* FROM ORDERS O, PRODUCT P WHERE O.ORDER_ID=P.ORDER_ID AND P.ID=’1111’;Would become

List orders = session.createCriteria(Order.class) .setFetchMode(“products”,FetchMode.JOIN) .add(Restrictions.eq(“id”,”1111”)) .list();

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 38: Hibernate online tutorials for beginers

This criteria query corresponds to the following HQL query:

from Book bookwhere book.name = 'Hibernate for Beginners'

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 39: Hibernate online tutorials for beginers

• Restriction.between is used to apply a "between" constraint to the field.

• Restriction.eq is used to apply an "equal" constraint to the field.

• Restriction.ge is used to apply a "greater than or equal" constraint to the field.

• Restriction.gt is used to apply a "greater than" constraint to the field.

• Restriction.idEq is used to apply an "equal" constraint to the identifier property.

• Restriction.in is used to apply an "in" constraint to the field.

• Restriction.isNotNull is used to apply an "is not null" constraint to the field.

• Restriction.isNull is used to apply an "is null" constraint to the field.

• Restriction.ne is used to apply a "not equal" constraint to the field.

IN:

criteria.add(Restrictions.in("newCourseID", courseIDs));

Page 40: Hibernate online tutorials for beginers

  7. Caching in Hibernate:

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 41: Hibernate online tutorials for beginers

Hibernate supports the caching of persistent objects at different levels:

1st level of caching

2nd Level of caching

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 42: Hibernate online tutorials for beginers

1. 1st level of caching:  Suppose we get an object with same

identifier for two times within a session, will Hibernate query the database for two times? 

Call us : (404)-900-9988www.quontrasolutions.com

Page 43: Hibernate online tutorials for beginers

If we inspect the SQL statements executed by Hibernate, we will find that only one database query is made. That means Hibernate is caching our objects in the same session. This kind of caching is called “first level caching”, whose caching scope is a session.

 But how about getting an object with same identifier for two times in two different sessions? Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 44: Hibernate online tutorials for beginers

2nd Level of caching:

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 45: Hibernate online tutorials for beginers

 We will find that two database queries are made. That means Hibernate is not caching the persistent objects across different sessions by default.

We need to turn on this “second level caching” whose caching scope is a session factory.

Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

Page 46: Hibernate online tutorials for beginers

To enable 2nd level of caching, update the ‘hibernate.cfg.xml’

To monitor the caching activities of Hibernate at runtime, we can add the following line to the log4j configuration file “log4j.properties”.  log4j.logger.org.hibernate.cache=debug

Page 47: Hibernate online tutorials for beginers

[email protected]

www.quontrasolutions.com Call us : (404)-900-9988

[email protected]

www.quontrasolutions.com

TO KNOW MORE IN-DEPTH KNOWLEDGE CONTACT US

Call Now 404)-900-9988

 Email: [email protected]

http://www.quontrasolutions.com

QUONTRA SOLUTIONS