Retrieving the Category instance from the RDBMS

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
€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

So let's now create a new program to retrieve the same category based on the ID. So let's right click new class. And then I'm going to change this to 02. I call the class as main, press enter. I forgot to select the Add main function. So I'm going to type here main control space and select the main method over here.

I'm going to say here factory equals to hibernate util dot get session factory, press Control one or Command one on Mac, and then I get the session factory and also are here at try finally lock. And inside this finally block, I'm going to say factory dot close. And then I also like to save your session equals to factory dot open session. We haven't created the session variable. Let's create that by pressing Command one, and then I got that somewhere at the end. I also have to say efficient close unlike the previous example, where we inserted a new record Which is qualified as DML, which is supposed to be given in a transactional boundary.

Select statements, on the other hand, do not require transactions. Now, in order to get data from the DB, all we need is the primary key value of the table that we have, which is categories. And the ID here is one, we can just come down here and then say session dot get. And we can say what is the object that we want hibernate to give us, that's going to be category dot class. And that's from our entity package. And the ID here is one which is the primary key value.

This returns a new category object. At this point in time, hibernate is going to read the metadata from this category Java, such as table names, column names, etc. and generate an SQL SELECT statement based on the primary key value compared with this one here. And then it retrieves the data gets a result set converts that result set into a category object returns the category object to me here. Again, I have to create this variable i press Command one, enter, I got the new category object. Once the session is closed, I can still access that c one by typing c one over here.

So if I save this and run the program, I should be able to see a select statement as well as the values from the DB. So this is the select statement that got generated. And this is the category object that we have retrieved from the DB

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.