All language subtitles for 004 Update UserDAO Class (part 2)

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
en English
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
vi Vietnamese Download
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now let's get back to our project in Eclipse IDE

and implement code for the listAll() method and count() method here

you see

the listAll() method the returns all users from the database

so it needs to execute a query

select all from the user table

so let's open the Users model class

and we put a named query here

using @NamedQueries annotation

okay

in the NamedQueries annotation we can specify a list of named queries

so...

okay

@NamedQuery

as you see here

the first parameter is the name of the query

and the second parameter is a content of the query string

so...

the name is Users (is the table) and findAll is the query name

query = "Select u from Users u order by u.fullName

okay

SELECT u FROM Users u

order by the full name of the user

ORDER BY u.fullName

you can see this is the object-oriented syntax

the user here is the mapped Users class

it's not the table, so u.fullName accesses the fullName field of the Users class

you see

now in the JpaDAO class...

we need to implement a method which can be reused by its subclasses to execute a named query

so we can name it as...

this method returns...

returns a list of entity objects

its name is findWithNamedQuery

the parameter is the query name

okay

we create a Query object from the EntityManager

entityManger.createNamedQuery(queryName)

import Query from javax.persistence

and return the result as a collection

getResultList()

okay

Now in the UserDAO class...

in the listAll() method we can call the findWithNamedQuery() method to execute a query

return super.findWithNamedQuery()

the query name is the name we specified in the Users class here

Users.findAll

okay

that's very simple, right?

Now, let's write a test method to test this listAll() method in the UserDAOTest class

it will be...

@Test

public void testListAll()

okay

userDAO.listAll()

and assign the statement to a new local variable

listUsers

and we can assert that this listUsers has more than one elements

assertTrue(listUsers.size() > 0)

okay

Now let's run this test method

Run As > JUnit Test

you see

the test was successful

and Hibernate issues a SQL SELECT statement in the Console here - you can see

you can see: select fields from the table users and order by full name

exactly as we specified in the JPQL in the model class here

you see

select u from Users u order by u.fullName

if you are curious you can print the information of each user in the returned list here

for example

UserDAOTest, for example...

for each Users user in the list

print its email address

getEmail()

Now let's run this test method again

Now you can see in the Console view, there are 4 email addresses printed here

david, sophia, you, and nam

exactly what we have in the database

let's select

4 - you see

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

the count() method you see

this method returns the total number of Users objects or the total number of rows in table users in the database

So, similarly we create a named query in the model class Users

as the second named query here

name is is Users.countAll

and the query is: SELECT COUNT(*) FROM Users u

okay

and now...

call super.findWithNamedQuery and the query name is Users.countAll

here... you see

and this method returns a list of collection - a list of values

No, we need to...

this...should

the count query should return a single result

so we need to use the getSingleResult() method

okay

because the findWithNameQuery() method returns a list of values

so it's not convenient to be used for the count method so we use another method in the JpaDAO class

that returns a single result

you can

public long countWithNamedQuery(String queryName)

create a Query object

from the entityManager

createNamedQuery

and return the single result

query.getSingleResult()

and cast the returned value to the long data type

okay

and in the UserDAO class, we just delegate the call to its super's method

return super.countWithNamedQuery

and the query is Users.countAll here

you see

Now, let's write a test method

for this count() method

@Test

public void testCount()

okay

userDAO.count();

totalUsers

Now there are four rows in the table users

that means there are four Users object

so we can assert the total user equal to four or not

okay

assertTrue(totalUsers == 4)

or we can use the asertEquals

assertEquals

expected is 4 and actual is the total number of users

okay

let's run this test method

testCount

Run As > JUnit Test

and you see

it passes the test

very good, right?

And in the Console view, you can see Hibernate issues a SQL statement

Select count(*) from the user table

so far we have updated the UserDAO class to implement the basic: create, update, get, delete, listAll, count methods

and also we updated the UserDAOTest class to test these methods

all tests were successful

and..

we have done as per the design

you see

list, create, update, get, delete, listAll and count

and the checkLogin() and findByEmail() methods - we will implement later in this course

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