Projection

Hibernate ORM - A Quick Start Guide Hibernate Query Language (HQL)
2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€42.77
List Price:  €59.88
You save:  €17.11
£37.08
List Price:  £51.91
You save:  £14.83
CA$68.96
List Price:  CA$96.55
You save:  CA$27.59
A$76.38
List Price:  A$106.94
You save:  A$30.56
S$64.32
List Price:  S$90.06
You save:  S$25.73
HK$390.06
List Price:  HK$546.11
You save:  HK$156.05
CHF 40.14
List Price:  CHF 56.21
You save:  CHF 16.06
NOK kr502.12
List Price:  NOK kr703
You save:  NOK kr200.88
DKK kr319.28
List Price:  DKK kr447.02
You save:  DKK kr127.73
NZ$85.06
List Price:  NZ$119.09
You save:  NZ$34.03
د.إ183.58
List Price:  د.إ257.03
You save:  د.إ73.45
৳6,085.49
List Price:  ৳8,520.17
You save:  ৳2,434.68
₹4,412.09
List Price:  ₹6,177.29
You save:  ₹1,765.19
RM211.14
List Price:  RM295.62
You save:  RM84.47
₦76,406.71
List Price:  ₦106,975.51
You save:  ₦30,568.80
₨14,191.25
List Price:  ₨19,868.89
You save:  ₨5,677.63
฿1,609.32
List Price:  ฿2,253.18
You save:  ฿643.86
₺2,062.31
List Price:  ₺2,887.40
You save:  ₺825.09
B$272.33
List Price:  B$381.29
You save:  B$108.95
R884.13
List Price:  R1,237.85
You save:  R353.72
Лв83.73
List Price:  Лв117.23
You save:  Лв33.50
₩69,495.77
List Price:  ₩97,299.65
You save:  ₩27,803.87
₪166.96
List Price:  ₪233.76
You save:  ₪66.79
₱2,844.65
List Price:  ₱3,982.74
You save:  ₱1,138.09
¥7,407.23
List Price:  ¥10,370.72
You save:  ¥2,963.48
MX$933.65
List Price:  MX$1,307.18
You save:  MX$373.53
QR182
List Price:  QR254.81
You save:  QR72.81
P719.51
List Price:  P1,007.37
You save:  P287.86
KSh6,458.70
List Price:  KSh9,042.70
You save:  KSh2,584
E£2,427.02
List Price:  E£3,398.02
You save:  E£971
ብር7,152.60
List Price:  ብር10,014.21
You save:  ብር2,861.61
Kz45,588.63
List Price:  Kz63,827.73
You save:  Kz18,239.10
CLP$48,602.27
List Price:  CLP$68,047.07
You save:  CLP$19,444.80
CN¥356.46
List Price:  CN¥499.08
You save:  CN¥142.61
RD$3,169.86
List Price:  RD$4,438.06
You save:  RD$1,268.20
DA6,491.87
List Price:  DA9,089.14
You save:  DA2,597.26
FJ$112.79
List Price:  FJ$157.92
You save:  FJ$45.12
Q383.64
List Price:  Q537.12
You save:  Q153.48
GY$10,460.92
List Price:  GY$14,646.12
You save:  GY$4,185.20
ISK kr6,124.77
List Price:  ISK kr8,575.17
You save:  ISK kr2,450.40
DH453.73
List Price:  DH635.26
You save:  DH181.53
L838.09
List Price:  L1,173.39
You save:  L335.30
ден2,640.43
List Price:  ден3,696.82
You save:  ден1,056.38
MOP$401.73
List Price:  MOP$562.45
You save:  MOP$160.72
N$886.82
List Price:  N$1,241.62
You save:  N$354.80
C$1,839.97
List Price:  C$2,576.11
You save:  C$736.13
रु7,047.30
List Price:  रु9,866.78
You save:  रु2,819.48
S/176.08
List Price:  S/246.53
You save:  S/70.45
K211.88
List Price:  K296.66
You save:  K84.77
SAR187.57
List Price:  SAR262.61
You save:  SAR75.04
ZK1,189.89
List Price:  ZK1,665.94
You save:  ZK476.05
L217.24
List Price:  L304.15
You save:  L86.91
Kč1,045.04
List Price:  Kč1,463.14
You save:  Kč418.09
Ft16,806.50
List Price:  Ft23,530.45
You save:  Ft6,723.94
SEK kr470.42
List Price:  SEK kr658.63
You save:  SEK kr188.20
ARS$68,111.66
List Price:  ARS$95,361.78
You save:  ARS$27,250.11
Bs345.64
List Price:  Bs483.93
You save:  Bs138.28
COP$199,687.37
List Price:  COP$279,578.30
You save:  COP$79,890.92
₡25,278.20
List Price:  ₡35,391.50
You save:  ₡10,113.30
L1,310.03
List Price:  L1,834.15
You save:  L524.12
₲360,426.55
List Price:  ₲504,626.01
You save:  ₲144,199.46
$U2,003.11
List Price:  $U2,804.51
You save:  $U801.40
zł181.94
List Price:  zł254.73
You save:  zł72.79
Already have an account? Log In

Transcript

So far whenever we are executing the query, we are getting that entire entity object. But sometimes it is desirable to retrieve only a couple of columns. For example, if I want the product name along with its category name, we can create a query for the same, which is called projection. To demonstrate the same, let's go and comment this out, and then write a new function called get product name with category names. Since I don't have this function, I press Command one or Ctrl one on Windows to auto generate that function, press tab, select the package level, and then press enter. Let's get it off the to do this time we're going to create a query a bit different than previous queries.

I'll say string HQ equals to select product name, come on category dot category name from product. As you can see, this is a property coming from directly the product class. But category name is a member of the category object, which is a member of the product here. Now we can see here query equals to session dot create query, we have to supply the SQL and the desired data type. All this time we gave product as a type or a category as a type here, so that we got a list of category or list of products. But this time, we are getting two strings instead of an entire product object.

Now these come as an array of two elements, so we can say here that it is an object or a class. So now the return value of session dot create query is a query object, which gives a list of object array element so I'm going to press your command one, press enter, you'll see that we got a query representing an object array. So in order to Get the data, we'll simply say query dot get result list, assign that to a variable called the list. But this time when I press Command one, you will see that it's a list of object array, where each array will have two elements in it, one product name, and the other one category name. Just to prove the point, let's loop that we say give me one object array called the data from the list. And then we're going to print the data of zero which is going to be the product name, plus something like this where data of one which is the category name going to be printed.

I save this around the same. This time there is no product object created, but we still be able to get the product names along with the category names

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.