hibernate_query_operations
DESCRIPTION
Hibernate_Query_OperationsTRANSCRIPT
![Page 1: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/1.jpg)
1
![Page 2: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/2.jpg)
2
![Page 3: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/3.jpg)
3
![Page 4: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/4.jpg)
4
![Page 5: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/5.jpg)
Note: a) HQL keywords are case-insensitive(prefer lowercase).
Example : SELECT is same as Select or SeLeCt
b) Class names and properties are case-sensitive
Example: RegistrationNumber is not the same as registrationNumber
The from clause returns instances of a class
The select clause picks which objects and properties to return in the query result set.
Results:
1) HQL: from Vehicle
1,KA-04-Z-1234,1200.99,Diesel,14.0,Toyota,1
2,AP-08-XY-9822,800.88,Diesel,21.0,Tata,1
3,JK-02-AA-120,450.0,Petrol,16.0,Maruthi,1
4,TN-09-EF-3411,3200.0,Diesel,5.0,Ashok Leyland,3
5,KA-08-AE-672,4000.0,Diesel,6.0,Tata,2
6,KA-02-GH-788,900.0,Petrol,15.0,Hyundai,1
2) HQL:from Vehicle v where v.fuelType = ‘Petrol’
3,JK-02-AA-120,450.0,Petrol,16.0,Maruthi,1
6,KA-02-GH-788,900.0,Petrol,15.0,Hyundai,1
3) select registrationNumber, mileagePerLiter from Vehicle
KA-04-Z-1234,14.0
AP-08-XY-9822,21.0
JK-02-AA-120,16.0
TN-09-EF-3411,5.05
![Page 6: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/6.jpg)
Note: The result of the query will be loaded completely into a collection in memory.
Entity instances retrieved by a query are in a persistent state.
The uniqueResult() method offers a shortcut if you know your query will only return a single object.
Object query.uniqueResult();
6
![Page 7: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/7.jpg)
7
![Page 8: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/8.jpg)
8
![Page 9: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/9.jpg)
The advantages of named parameters are as follows:
•named parameters are insensitive to the order they occur I the query string
•they can occur multiple times in the same query
•they are self-documenting
Note: The setEntity() method of Query interface lets you bind a persistent entity as listed below:
String HQL = "from Vehicle v where v.category = :cat";
Query query = session.createQuery(HQL);
Category category = new Category();
category.setCategoryId(1);
category.setCategoryName("Car");
query.setEntity("cat", category);
9
![Page 10: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/10.jpg)
10
![Page 11: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/11.jpg)
11
![Page 12: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/12.jpg)
Pagination usage: If user needs to view a subset of data that is not easily displayed on one page. Provide pagination control to browse from page to page. Let the user browse to the previous and next pages by providing links to such actions. Also, provide links to the absolute start and end of the dataset (first and last).
Note: There is no standard way to express pagination in SQL. Hibernate knows the tricks to make this work efficiently on your particular database.
12
![Page 13: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/13.jpg)
Note: The HQL is same as the following SQL.
select
vehicle0_.REG_NO as col_0_0_,
vehicle0_.MILEAGE as col_1_0_,
vehicle0_.FUEL_TYPE as col_2_0_,
category1_.CATEGORY_NAME as col_3_0_
from
VEHICLE vehicle0_
inner join
CATEGORY category1_
on vehicle0_.CATEGORY_ID=category1_.CATEGORY_ID
13
![Page 14: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/14.jpg)
Note: Hibernate join queries return tuples of objects.
14
![Page 15: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/15.jpg)
An inner join is the most common join operation used in applications and can be regarded as the default join-type.
Note: cat.vehicles ---> vehicles is a collection property of type Set<Vehicle> declared in Category class.
Set<Vehicle> vehicles;
Complete Code listing:
String hql = "from Category cat left outer join cat.vehicles as vehicle";
Query query = session.createQuery(hql);
List<Object[]> objects = query.list();
for(int i = 0; i < objects.size(); i++) {
Object[] entities = objects.get(i);
if( entities[0] != null) {
Category category = (Category) entities[0];
System.out.print(category.getCategoryId()
+"," + category.getCategoryName());
}
if( entities[1] != null ) {
Vehicle vehicle = (Vehicle) entities[1];
System.out.print(vehicle.getRegistrationNumber()
+"," + vehicle.getManufacturer() );
}15
![Page 16: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/16.jpg)
SQL generated:
select
category0_.CATEGORY_ID as CATEGORY1_0_0_,
vehicles1_.VEHICLE_ID as VEHICLE1_1_1_,
category0_.CATEGORY_NAME as CATEGORY2_0_0_,
vehicles1_.REG_NO as REG2_1_1_,
vehicles1_.DAILY_RENTAL as DAILY3_1_1_,
vehicles1_.FUEL_TYPE as FUEL4_1_1_,
vehicles1_.MILEAGE as MILEAGE1_1_,
vehicles1_.MANUFACTURER as MANUFACT6_1_1_,
vehicles1_.DESCRIPTION as DESCRIPT7_1_1_,
vehicles1_.CATEGORY_ID as CATEGORY8_1_1_
from
CATEGORY category0_
left outer join
VEHICLE vehicles1_
on category0_.CATEGORY_ID=vehicles1_.CATEGORY_ID
16
![Page 17: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/17.jpg)
17
![Page 18: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/18.jpg)
SQL:
select
vehicle0_.VEHICLE_ID as VEHICLE1_1_,
vehicle0_.REG_NO as REG2_1_,
vehicle0_.DAILY_RENTAL as DAILY3_1_,
vehicle0_.FUEL_TYPE as FUEL4_1_,
vehicle0_.MILEAGE as MILEAGE1_,
vehicle0_.MANUFACTURER as MANUFACT6_1_,
vehicle0_.DESCRIPTION as DESCRIPT7_1_,
vehicle0_.CATEGORY_ID as CATEGORY8_1_
from
VEHICLE vehicle0_
where
vehicle0_.MILEAGE>=(
select
avg(vehicle1_.MILEAGE)
from
VEHICLE vehicle1_
)
18
![Page 19: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/19.jpg)
Wrap the query text into a CDATA instruction so the XML parser doesn’t get confused by any characters in your query string that may accidentally be considered XML (such as the < or > than operator).
Note:
1) if <query> element is inside a <class> element : Prefix it with name of entity class:
session.getNamedQuery("com.mindtree.entity.Vehicle.getVehiclesByFuelType");
2) if <query> element is inside a <class> element, name of the query is globally unique:
session.getNamedQuery("getVehiclesByFuelType");
19
![Page 20: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/20.jpg)
Refer the same query illustrations in Native SQL section.
20
![Page 21: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/21.jpg)
21
![Page 22: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/22.jpg)
22
![Page 23: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/23.jpg)
23
![Page 24: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/24.jpg)
24
![Page 25: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/25.jpg)
25
![Page 26: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/26.jpg)
It’s easy to extend the Criteria API and add new methods or new Criterion classes.
Note: 1) Queries expressed as criteria are often much less readable than queries expressed in HQL.
2) Criteria API is native to Hibernate; it isn’t part of the Java Persistence API.
26
![Page 27: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/27.jpg)
If you need Vehicles of Category type = 1 or any vehicle of type ‘petrol’
Criteria criteria = session.createCriteria(Vehicle.class);
Criterion c1 = Restrictions.eq("category.categoryId", 1);
Criterion c2 = Restrictions.ilike("fuelType", "Petrol");
Criterion c3 = Restrictions.or(c1,c2);
criteria.add(c3);
27
![Page 28: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/28.jpg)
Restrictions class contain static factory methods for creating Criterion instances.
Queries comprise of Criterion instances, which say what objects to select.
Queries generated using Criteria be ordered using Order instances.
If you add multiple Criterion instances to the one Criteria instance, they’re applied conjunctively (using and).
You can use Restrictions.disjunction() and Restrictions.conjunction() to add “and” and/or “or” to where clause.
28
![Page 29: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/29.jpg)
SQL equivalent:
select * from VEHICLE where
( (
CATEGORY_ID=?
and lower(REG_NO) like ?
)
or MILEAGE>=?
)
29
![Page 30: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/30.jpg)
30
![Page 31: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/31.jpg)
The setProjection() method on a Criteria accepts a list of several properties that are to be included in the result.
31
![Page 32: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/32.jpg)
32
![Page 33: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/33.jpg)
Note: SQL’s are not portable.
33
![Page 34: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/34.jpg)
34
![Page 35: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/35.jpg)
Hibernate reads the resultset of the SQL query and tries to discover the column names and types as defined in your mapping metadata.
If the column REG_NO is returned, and it’s mapped to the name property of the Vehicle class, Hibernate knows how to populate that property and finally returns fully loaded entity objects.
Note: For addEntity() method entity class and mapping information is required.
35
![Page 36: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/36.jpg)
To achieve the same using HQL or Criteria API will be a complicated task.
TOTAL_RENT_AMOUNT calculated by taking the data difference between booking end date and booking start date multiplied by the daily rental
36
![Page 37: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/37.jpg)
37
![Page 38: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/38.jpg)
38
![Page 39: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/39.jpg)
39
![Page 40: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/40.jpg)
40
![Page 41: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/41.jpg)
41
![Page 42: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/42.jpg)
42
![Page 43: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/43.jpg)
43
![Page 44: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/44.jpg)
44
![Page 45: Hibernate_Query_Operations](https://reader031.vdocuments.us/reader031/viewer/2022020500/568c36141a28ab023596ae2d/html5/thumbnails/45.jpg)
45