Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,060 --> 00:00:04,440
Now let's get back to our project in Eclipse IDE
2
00:00:04,800 --> 00:00:11,800
and implement code for the listAll() method and count() method here
3
00:00:11,900 --> 00:00:12,800
you see
4
00:00:15,300 --> 00:00:22,400
the listAll() method the returns all users from the database
5
00:00:23,000 --> 00:00:27,620
so it needs to execute a query
6
00:00:28,600 --> 00:00:34,000
select all from the user table
7
00:00:35,600 --> 00:00:43,880
so let's open the Users model class
8
00:00:44,800 --> 00:00:51,000
and we put a named query here
9
00:00:51,900 --> 00:00:57,080
using @NamedQueries annotation
10
00:00:58,100 --> 00:01:00,000
okay
11
00:01:05,800 --> 00:01:16,060
in the NamedQueries annotation we can
specify a list of named queries
12
00:01:18,500 --> 00:01:19,720
so...
13
00:01:20,200 --> 00:01:21,300
okay
14
00:01:25,100 --> 00:01:27,600
@NamedQuery
15
00:01:31,000 --> 00:01:32,620
as you see here
16
00:01:33,200 --> 00:01:39,500
the first parameter is the name of the query
17
00:01:39,800 --> 00:01:44,200
and the second parameter is a content of the query string
18
00:01:45,200 --> 00:01:46,520
so...
19
00:01:47,500 --> 00:01:54,700
the name is Users (is the table) and findAll is the query name
20
00:01:54,800 --> 00:02:08,199
query = "Select u from Users u order by u.fullName
21
00:02:08,500 --> 00:02:09,500
okay
22
00:02:11,900 --> 00:02:17,960
SELECT u FROM Users u
23
00:02:22,600 --> 00:02:26,520
order by the full name of the user
24
00:02:27,300 --> 00:02:31,400
ORDER BY u.fullName
25
00:02:32,000 --> 00:02:34,860
you can see this is the object-oriented syntax
26
00:02:34,860 --> 00:02:38,400
the user here is the mapped Users class
27
00:02:38,500 --> 00:02:47,200
it's not the table, so u.fullName accesses the fullName field of the Users class
28
00:02:47,200 --> 00:02:48,460
you see
29
00:02:53,400 --> 00:02:58,600
now in the JpaDAO class...
30
00:02:59,200 --> 00:03:11,000
we need to implement a method which can be reused by its subclasses to execute a named query
31
00:03:11,400 --> 00:03:16,120
so we can name it as...
32
00:03:27,600 --> 00:03:29,960
this method returns...
33
00:03:30,700 --> 00:03:35,600
returns a list of entity objects
34
00:03:38,300 --> 00:03:45,220
its name is findWithNamedQuery
35
00:03:46,600 --> 00:03:48,660
the parameter is the query name
36
00:03:48,660 --> 00:03:50,000
okay
37
00:04:07,800 --> 00:04:12,020
we create a Query object from the EntityManager
38
00:04:12,600 --> 00:04:22,700
entityManger.createNamedQuery(queryName)
39
00:04:28,700 --> 00:04:32,100
import Query from javax.persistence
40
00:04:36,300 --> 00:04:39,360
and return the result as a collection
41
00:04:39,700 --> 00:04:42,060
getResultList()
42
00:04:43,900 --> 00:04:45,000
okay
43
00:04:48,800 --> 00:04:51,760
Now in the UserDAO class...
44
00:04:52,900 --> 00:05:03,600
in the listAll() method we can call the findWithNamedQuery() method to execute a query
45
00:05:16,400 --> 00:05:21,100
return super.findWithNamedQuery()
46
00:05:21,400 --> 00:05:29,800
the query name is the name we specified in the Users class here
47
00:05:30,000 --> 00:05:32,100
Users.findAll
48
00:05:32,400 --> 00:05:33,480
okay
49
00:05:40,800 --> 00:05:43,760
that's very simple, right?
50
00:05:44,500 --> 00:05:54,000
Now, let's write a test method to test this listAll() method in the UserDAOTest class
51
00:06:03,100 --> 00:06:07,020
it will be...
52
00:06:14,900 --> 00:06:15,820
@Test
53
00:06:16,400 --> 00:06:20,640
public void testListAll()
54
00:06:20,900 --> 00:06:22,040
okay
55
00:06:31,200 --> 00:06:35,000
userDAO.listAll()
56
00:06:35,800 --> 00:06:40,000
and assign the statement to a new local variable
57
00:06:40,600 --> 00:06:45,000
listUsers
58
00:06:52,200 --> 00:07:02,180
and we can assert that this listUsers has more than one elements
59
00:07:13,500 --> 00:07:20,020
assertTrue(listUsers.size() > 0)
60
00:07:20,400 --> 00:07:21,360
okay
61
00:07:23,300 --> 00:07:26,060
Now let's run this test method
62
00:07:28,200 --> 00:07:31,160
Run As > JUnit Test
63
00:07:35,500 --> 00:07:36,480
you see
64
00:07:36,800 --> 00:07:39,500
the test was successful
65
00:07:42,900 --> 00:07:51,000
and Hibernate issues a SQL SELECT statement in the Console here - you can see
66
00:07:51,600 --> 00:07:56,940
you can see: select fields from the table users and order by full name
67
00:07:57,000 --> 00:08:04,200
exactly as we specified in the JPQL in the model class here
68
00:08:04,800 --> 00:08:05,600
you see
69
00:08:06,000 --> 00:08:10,000
select u from Users u order by u.fullName
70
00:08:20,600 --> 00:08:29,000
if you are curious you can print the information of each user in the returned list here
71
00:08:32,299 --> 00:08:35,179
for example
72
00:08:37,600 --> 00:08:41,539
UserDAOTest, for example...
73
00:08:43,000 --> 00:08:49,940
for each Users user in the list
74
00:08:53,900 --> 00:08:59,720
print its email address
75
00:08:59,720 --> 00:09:01,600
getEmail()
76
00:09:02,300 --> 00:09:07,900
Now let's run this test method again
77
00:09:16,800 --> 00:09:23,400
Now you can see in the Console view, there are 4 email addresses printed here
78
00:09:23,500 --> 00:09:24,920
david, sophia, you, and nam
79
00:09:26,500 --> 00:09:30,490
exactly what we have in the database
80
00:09:33,000 --> 00:09:34,580
let's select
81
00:09:35,900 --> 00:09:38,320
4 - you see
82
00:09:44,000 --> 00:09:51,600
Next, let's implement the count() method in the UserDAO class
83
00:09:52,000 --> 00:09:54,200
the count() method you see
84
00:09:54,200 --> 00:10:10,000
this method returns the total number of Users objects or the total number of rows in table users in the database
85
00:10:10,800 --> 00:10:18,480
So, similarly we create a named query in the model class Users
86
00:10:20,800 --> 00:10:24,000
as the second named query here
87
00:10:29,000 --> 00:10:35,100
name is is Users.countAll
88
00:10:35,900 --> 00:10:47,650
and the query is:
SELECT COUNT(*) FROM Users u
89
00:10:48,500 --> 00:10:49,460
okay
90
00:10:50,800 --> 00:10:52,940
and now...
91
00:11:05,200 --> 00:11:19,000
call super.findWithNamedQuery and the query name is Users.countAll
92
00:11:20,000 --> 00:11:22,340
here... you see
93
00:11:33,400 --> 00:11:44,560
and this method returns a list of collection - a list of values
94
00:12:11,400 --> 00:12:13,220
No, we need to...
95
00:12:13,900 --> 00:12:16,600
this...should
96
00:12:16,700 --> 00:12:23,000
the count query should return a single result
97
00:12:24,200 --> 00:12:32,120
so we need to use the getSingleResult() method
98
00:12:36,700 --> 00:12:37,700
okay
99
00:12:41,600 --> 00:12:50,700
because the findWithNameQuery() method returns a list of values
100
00:12:50,700 --> 00:13:06,400
so it's not convenient to be used for the count method so we use another method in the JpaDAO class
101
00:13:07,400 --> 00:13:10,520
that returns a single result
102
00:13:11,200 --> 00:13:11,820
you can
103
00:13:12,600 --> 00:13:24,890
public long countWithNamedQuery(String queryName)
104
00:13:27,000 --> 00:13:28,760
create a Query object
105
00:13:29,300 --> 00:13:31,740
from the entityManager
106
00:13:34,000 --> 00:13:37,140
createNamedQuery
107
00:13:38,500 --> 00:13:41,320
and return the single result
108
00:13:41,900 --> 00:13:44,520
query.getSingleResult()
109
00:13:45,000 --> 00:13:51,200
and cast the returned value to the long data type
110
00:13:51,500 --> 00:13:52,640
okay
111
00:13:53,200 --> 00:14:02,000
and in the UserDAO class, we just delegate the call to its super's method
112
00:14:02,400 --> 00:14:06,540
return super.countWithNamedQuery
113
00:14:06,900 --> 00:14:14,870
and the query is Users.countAll here
114
00:14:18,100 --> 00:14:19,000
you see
115
00:14:19,000 --> 00:14:23,040
Now, let's write a test method
116
00:14:23,800 --> 00:14:25,800
for this count() method
117
00:14:31,600 --> 00:14:32,560
@Test
118
00:14:32,700 --> 00:14:36,000
public void testCount()
119
00:14:36,400 --> 00:14:37,600
okay
120
00:14:40,200 --> 00:14:44,160
userDAO.count();
121
00:14:47,600 --> 00:14:50,280
totalUsers
122
00:14:51,900 --> 00:14:57,639
Now there are four rows in the table users
123
00:14:58,500 --> 00:15:03,160
that means there are four Users object
124
00:15:03,900 --> 00:15:13,500
so we can assert the total user equal to four or not
125
00:15:13,500 --> 00:15:14,000
okay
126
00:15:15,500 --> 00:15:23,000
assertTrue(totalUsers == 4)
127
00:15:24,600 --> 00:15:26,640
or we can use the asertEquals
128
00:15:26,900 --> 00:15:29,829
assertEquals
129
00:15:30,800 --> 00:15:36,920
expected is 4 and actual is the total number of users
130
00:15:37,000 --> 00:15:37,720
okay
131
00:15:38,000 --> 00:15:40,980
let's run this test method
132
00:15:42,000 --> 00:15:43,000
testCount
133
00:15:43,200 --> 00:15:45,900
Run As > JUnit Test
134
00:15:50,700 --> 00:15:51,540
and you see
135
00:15:51,900 --> 00:15:54,640
it passes the test
136
00:15:54,900 --> 00:15:56,240
very good, right?
137
00:15:56,700 --> 00:16:01,600
And in the Console view, you can see Hibernate issues a SQL statement
138
00:16:01,600 --> 00:16:06,560
Select count(*) from the user table
139
00:16:11,000 --> 00:16:27,000
so far we have updated the UserDAO class to implement the basic:
create, update, get, delete, listAll, count methods
140
00:16:30,200 --> 00:16:40,000
and also we updated the UserDAOTest class to test these methods
141
00:16:41,800 --> 00:16:44,880
all tests were successful
142
00:16:45,900 --> 00:16:47,000
and..
143
00:16:47,500 --> 00:16:50,400
we have done as per the design
144
00:16:52,000 --> 00:16:52,880
you see
145
00:16:53,200 --> 00:16:57,000
list, create, update, get, delete, listAll and count
146
00:16:57,500 --> 00:17:07,000
and the checkLogin() and findByEmail() methods - we will implement later in this course
9942
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.