Bulk update

Hibernate ORM - A Quick Start Guide Hibernate Query Language (HQL)
3 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
€41.81
List Price:  €58.53
You save:  €16.72
£36.21
List Price:  £50.70
You save:  £14.48
CA$67.66
List Price:  CA$94.73
You save:  CA$27.07
A$70.84
List Price:  A$99.19
You save:  A$28.34
S$63.19
List Price:  S$88.47
You save:  S$25.28
HK$390.20
List Price:  HK$546.31
You save:  HK$156.11
CHF 38.38
List Price:  CHF 53.74
You save:  CHF 15.35
NOK kr478.85
List Price:  NOK kr670.43
You save:  NOK kr191.57
DKK kr312.19
List Price:  DKK kr437.10
You save:  DKK kr124.90
NZ$82.40
List Price:  NZ$115.37
You save:  NZ$32.96
د.إ183.58
List Price:  د.إ257.03
You save:  د.إ73.45
৳6,112.16
List Price:  ৳8,557.52
You save:  ৳2,445.35
₹4,597.61
List Price:  ₹6,437.02
You save:  ₹1,839.41
RM196.33
List Price:  RM274.88
You save:  RM78.55
₦69,348.62
List Price:  ₦97,093.62
You save:  ₦27,745
₨13,992.41
List Price:  ₨19,590.50
You save:  ₨5,598.08
฿1,558.73
List Price:  ฿2,182.35
You save:  ฿623.62
₺2,170.81
List Price:  ₺3,039.30
You save:  ₺868.49
B$260.01
List Price:  B$364.04
You save:  B$104.02
R787.55
List Price:  R1,102.63
You save:  R315.08
Лв81.77
List Price:  Лв114.48
You save:  Лв32.71
₩71,595.81
List Price:  ₩100,239.87
You save:  ₩28,644.05
₪154.14
List Price:  ₪215.81
You save:  ₪61.66
₱2,949.54
List Price:  ₱4,129.60
You save:  ₱1,180.05
¥7,664.25
List Price:  ¥10,730.56
You save:  ¥3,066.31
MX$857.41
List Price:  MX$1,200.44
You save:  MX$343.03
QR181.86
List Price:  QR254.62
You save:  QR72.75
P654.47
List Price:  P916.31
You save:  P261.84
KSh6,446.21
List Price:  KSh9,025.21
You save:  KSh2,579
E£2,343.91
List Price:  E£3,281.67
You save:  E£937.75
ብር7,777.53
List Price:  ብር10,889.17
You save:  ብር3,111.63
Kz45,597.52
List Price:  Kz63,840.18
You save:  Kz18,242.66
CLP$43,282.19
List Price:  CLP$60,598.53
You save:  CLP$17,316.34
CN¥347.18
List Price:  CN¥486.08
You save:  CN¥138.90
RD$3,147.03
List Price:  RD$4,406.10
You save:  RD$1,259.06
DA6,454
List Price:  DA9,036.12
You save:  DA2,582.11
FJ$109.50
List Price:  FJ$153.32
You save:  FJ$43.81
Q383.74
List Price:  Q537.26
You save:  Q153.52
GY$10,464.44
List Price:  GY$14,651.06
You save:  GY$4,186.61
ISK kr6,054.78
List Price:  ISK kr8,477.18
You save:  ISK kr2,422.40
DH452.24
List Price:  DH633.17
You save:  DH180.93
L840.87
List Price:  L1,177.29
You save:  L336.41
ден2,574.82
List Price:  ден3,604.96
You save:  ден1,030.13
MOP$401.97
List Price:  MOP$562.79
You save:  MOP$160.82
N$795.31
List Price:  N$1,113.50
You save:  N$318.18
C$1,840.56
List Price:  C$2,576.94
You save:  C$736.37
रु7,350.97
List Price:  रु10,291.94
You save:  रु2,940.97
S/167.35
List Price:  S/234.31
You save:  S/66.95
K214.10
List Price:  K299.76
You save:  K85.65
SAR187.49
List Price:  SAR262.50
You save:  SAR75.01
ZK994.08
List Price:  ZK1,391.79
You save:  ZK397.71
L213.07
List Price:  L298.31
You save:  L85.24
Kč1,016.13
List Price:  Kč1,422.66
You save:  Kč406.53
Ft15,954.66
List Price:  Ft22,337.81
You save:  Ft6,383.14
SEK kr442.46
List Price:  SEK kr619.48
You save:  SEK kr177.02
ARS$72,210.55
List Price:  ARS$101,100.55
You save:  ARS$28,890
Bs345.62
List Price:  Bs483.90
You save:  Bs138.27
COP$182,899.65
List Price:  COP$256,074.14
You save:  COP$73,174.49
₡24,825.23
List Price:  ₡34,757.30
You save:  ₡9,932.07
L1,319.97
List Price:  L1,848.07
You save:  L528.09
₲335,885
List Price:  ₲470,265.88
You save:  ₲134,380.87
$U1,871.59
List Price:  $U2,620.38
You save:  $U748.78
zł175.89
List Price:  zł246.26
You save:  zł70.37
Already have an account? Log In

Transcript

Hibernate query language also supports bulk update and bulk delete. For example, if you want to increase the price of all the products by $1, then you can use this feature. To demonstrate the same. Let's go and comment out this and write a new function called increment product price by and then I'm going to supply what is the amount by which you want to increment, I'm going to supply say, for example, one dot zero saying that increment the product price by $1. We don't have this function, so I'm going to create a new one and then change this to ANC maybe a variable for representing increment price. Now in order to increment this, I can say here string SQL equals to update p capital product because it's the name of the class, not the name of the table.

Set unit price equals to the current unit price. Plus probably a parameter here called ANC price. And this ANC that I received as function argument must be supplied over here, we simply say your QR y equals to session dot create query. This time, we don't have to really worry about the type of the data that we want, because we're not going to retrieve any data from the DB. This is my SQL. And I simply create a query object just like that.

I can say query dot set parameter, and then the parameter name is ANC price, the value happens to be a CNC. And then I have to say your query dot execute update. This returns the number of rows affected. So I can always type your count equals to press Control one or Command one to generate that variable. And then I can type your total of count products. Update Before we run this, there is one other thing that we have to do, we have to do this in a transactional boundary.

So I'm going to say here somewhere in the beginning session dot begin transaction. And after executing, I'm going to say here, session dot, get the current transaction and then commit the same. Ideally, we should surround all these statements inside a try block. And then in the catch block, we should also do a rollback. before running the code. Let's go and see what prizes we have.

For the products. first few of them you can see $18 $19 and $10. If I go and run this program, now I should see that there are 77 products updated. If I go here and then rerun this, you should see that it is $19 $20 and $11. If I run this once more, again, it's going to be updated with the new price. I'll come back here and rerun this.

It changes from 19 2011 to 2021 and 12

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.