Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.