All language subtitles for 002 Update UserDAO Class (part 1)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now, let's update the UserDAO class

to implement all the CRUD operations: create, update, retrieve and delete

as well as other methods as per the design you can see here: checkLogin, findByEmail

And we continue to update the UserDAOTest class to test the UserDAO class

Let's open the UserDAO class

UserDAO class here

stop the server

Stop

you see

we already implemented the create() and update() methods

Now we will implement the get() and delete() methods

and we also need to update the JpaDAO class

the parent class of the UserDAO

Now we implement the get() method that retrieves an User object from the database

with the given ID - ID of the user

here I can...

change the name to userId, okay

and...

we delegate the call to the superclass - the JpaDAO class

so in the JpaDAO class we need to implement...

...a method that retrieves an entity object from the database

so...

I name this method is...

find

public

public E find() (return an entity)

okay

and the first parameter is the class type

class

type of class

the type of the domain model class

and then the ID of the...

ID of the object needs to be...

need to be searched in the database

We can use the EntityManager's find() method

type and id

this find() method of the EntityManager returns an entity so...

we assign the returned type to the...

to an object E

entity

okay

and return the entity

return entity

we need to refresh the retrieved entity

entityManager

refresh

sorry

refresh

entity

okay

Now in the UserDAO class, we just delegate the call to its super class JpaDAO

JpaDAO

return

sorry

return super.find()

the class type is the Users.class

id

okay

userId

Now, let's write unit tests for this get() method

Open the UserDAOTest class

in the test source folder here

UserDAOTest

you see

in the previous section we implemented our unit test for the create user

Now let's...

write another method for testing the get() method

collapse this

@Test

public void testGetUsersFound()

the user ID is Integer userId = 1

userDAO.find()

sorry

get

find() is its super class method

get(userId)

and assign the return statement to a new local variable user

and we assert that the user should not be null

assertNotNull(user)

okay

Let's see in our database, you see: there's an user with ID 1

you see here

ID 1 is the user with email nam@codejava.net

Now let's run this testGetUsersFound() method

right-click > Run As > JUnit Test

you see

a green bar appears here

that means the test was successful

and in the Console you can see that Hibernate prints a SQL statement

the select statement here: select from users table

you see

and try to print the email of the returned user

sysout Ctrl + space

okay

run the test method again

JUnit

Run

sorry

this should be in the get() method

okay

Run again

sorry

user

run again

and you can see, it means the email of the user with ID 1 is nam@codejava.net

and in the database

you see: nam@codejava.net

and to make the code safer...

we need to check...

we need to check nullability of the Users object first

if user is not null

then print the email

okay

and the second test case is that the user could not be found

okay

so we create another test method

@Test

testGetUsersNotFound()

okay

and here we use the ID which doesn't belong to any user in the database

Integer userId

for example

99

in the database we don't have any user with that ID, right?

Users user = userDAO.get(userId);

and here we assert that the Users object is null

okay

Now, let's run this test method

right-click

Run As > JUnit Test

oh we have an error here

in the JpaDao class, we need to check the nullability of the entity object first before we refresh it

okay

no problem

if entity is not null, refresh

okay

Now let's run the test method again

Run

you see, the test was successful

testGetUsersNotFound was successful

okay

Next, let's implement the delete() method in the UserDAO class

the delete() method here

to delete a user that associates with the given user ID

userId

okay

and we need to implement a delete() method in the superclass

JpaDAO - so it can be reused by all the DAO classes

okay

so this method is...

void delete()

accept the class type and ID of the entity object

and to delete an entity from the database we can use the remove() method of the EntityManager

entityManager

you can see the remove() method here

it takes a parameter which is the reference of the object

reference

okay

so we need to get the reference of the object to be removed by using the getReference() method of the EntityManager

Object reference = entityManager.getReference(type, id);

and because the delete operation makes change to the database, we need to wrap the code inside a transaction

so we begin the transaction here

entityManager.getTransaction().begin()

and commit the transaction after

entityManager.getTransaction().commit()

okay

Now in the UserDAO class, simply delegate the call to its super class

super.delete()

the type is the Users.class and userId

okay

Now let's test this method

in the UserDAOTest class

the test method is void testDeleteUser()

okay

@Test

so let's see which user we want to delete

the user with ID 5 you see here

Tommy

okay

Integer userId = 5

call the userDAO.delete(userId)

okay

and we can assert this test case by

trying to get the user again with the given ID

if the user is not found, then the test is successful

Users user = userDAO.get(userId);

and we assert that this user is null

assertNull(user)

okay

Now let's run this test method

testDeleteUsers

Right-click > Run As > JUnit test

and you see:

it passes the test

with a green bar

appears here

and in the Console view you can see

Hibernate issued a SELECT SQL statement to...

find the reference of the Users object

and then it issued a DELETE SQL statement to delete...

to remove the user from the database

you see

first it issued a SELECT SQL statement to get the reference

get the reference here

issued SELECT statement and then it issued DELETE statement to remove the reference

and then because we call the UserDAO's get method method, it issued the SELECT SQL statement again

very good, right?

and in case the user we want to delete doesn't exist in the database

it should throw an exception, so...

write another test case for delete user here

it should be testDeleteNonExistUsers, okay?

public void testDeleteNonExistUsers()

@Test

copy - paste

for example we are trying to delete the user with ID 55

right?

so this should throw an exception

so we can use the "expected" attribute of the Test annotation

and specify the exception class

here we use a generic exception

okay

now, let's run this test method: testDeleteNonExistUsers

Run As > JUnit Test

you see

the test was successful

what if we don't use the expected attribute?

run the test again

and you can see there was an error

javax.persistence.EntityNotFoundException

so...

to be more specific we can specify the exception class: EntityNotFoundException

in the expected attribute

here

EntityNotFoundException

okay

and run the test case again

Run

now the test was successful

okay

and...

to implement the listAll() and count() methods...

you need to know the concept: Named Queries in JPA

which you will learn in the next video

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.