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
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:06,800 --> 00:00:12,200 Now, let's update the UserDAO class 2 00:00:12,700 --> 00:00:20,500 to implement all the CRUD operations: create, update, retrieve and delete 3 00:00:21,000 --> 00:00:28,200 as well as other methods as per the design you can see here: checkLogin, findByEmail 4 00:00:29,100 --> 00:00:39,000 And we continue to update the UserDAOTest class to test the UserDAO class 5 00:00:39,400 --> 00:00:44,000 Let's open the UserDAO class 6 00:00:46,800 --> 00:00:50,180 UserDAO class here 7 00:00:53,900 --> 00:00:55,400 stop the server 8 00:00:57,200 --> 00:00:58,600 Stop 9 00:01:04,900 --> 00:01:05,860 you see 10 00:01:06,000 --> 00:01:11,900 we already implemented the create() and update() methods 11 00:01:12,500 --> 00:01:20,210 Now we will implement the get() and delete() methods 12 00:01:31,100 --> 00:01:36,580 and we also need to update the JpaDAO class 13 00:01:37,300 --> 00:01:41,650 the parent class of the UserDAO 14 00:01:54,000 --> 00:02:08,600 Now we implement the get() method that retrieves an User object from the database 15 00:02:09,600 --> 00:02:14,140 with the given ID - ID of the user 16 00:02:20,500 --> 00:02:23,520 here I can... 17 00:02:27,600 --> 00:02:32,080 change the name to userId, okay 18 00:02:41,000 --> 00:02:43,080 and... 19 00:02:44,700 --> 00:02:52,400 we delegate the call to the superclass - the JpaDAO class 20 00:02:52,500 --> 00:02:57,950 so in the JpaDAO class we need to implement... 21 00:02:59,800 --> 00:03:07,200 ...a method that retrieves an entity object from the database 22 00:03:08,600 --> 00:03:10,000 so... 23 00:03:12,500 --> 00:03:15,360 I name this method is... 24 00:03:16,500 --> 00:03:18,480 find 25 00:03:22,800 --> 00:03:24,500 public 26 00:03:26,150 --> 00:03:30,240 public E find() (return an entity) 27 00:03:30,800 --> 00:03:31,780 okay 28 00:03:36,000 --> 00:03:41,580 and the first parameter is the class type 29 00:03:41,900 --> 00:03:43,099 class 30 00:03:43,700 --> 00:03:45,380 type of class 31 00:03:45,700 --> 00:03:47,760 the type of the domain model class 32 00:03:48,200 --> 00:03:52,000 and then the ID of the... 33 00:03:55,000 --> 00:03:59,520 ID of the object needs to be... 34 00:04:00,900 --> 00:04:05,800 need to be searched in the database 35 00:04:09,500 --> 00:04:16,940 We can use the EntityManager's find() method 36 00:04:20,300 --> 00:04:23,500 type and id 37 00:04:26,300 --> 00:04:32,960 this find() method of the EntityManager returns an entity so... 38 00:04:34,000 --> 00:04:38,260 we assign the returned type to the... 39 00:04:38,800 --> 00:04:41,539 to an object E 40 00:04:43,400 --> 00:04:44,400 entity 41 00:04:44,500 --> 00:04:45,280 okay 42 00:04:47,500 --> 00:04:49,380 and return the entity 43 00:04:50,900 --> 00:04:54,340 return entity 44 00:04:58,100 --> 00:05:05,000 we need to refresh the retrieved entity 45 00:05:06,200 --> 00:05:08,560 entityManager 46 00:05:11,000 --> 00:05:12,180 refresh 47 00:05:12,600 --> 00:05:13,920 sorry 48 00:05:15,900 --> 00:05:18,490 refresh 49 00:05:19,700 --> 00:05:20,420 entity 50 00:05:20,420 --> 00:05:22,000 okay 51 00:05:28,300 --> 00:05:38,620 Now in the UserDAO class, we just delegate the call to its super class JpaDAO 52 00:05:39,400 --> 00:05:41,200 JpaDAO 53 00:05:45,200 --> 00:05:46,520 return 54 00:05:46,800 --> 00:05:47,700 sorry 55 00:05:48,700 --> 00:05:51,620 return super.find() 56 00:05:52,500 --> 00:05:57,240 the class type is the Users.class 57 00:05:57,900 --> 00:05:58,580 id 58 00:05:58,800 --> 00:05:59,900 okay 59 00:06:05,100 --> 00:06:06,360 userId 60 00:06:10,800 --> 00:06:15,960 Now, let's write unit tests for this get() method 61 00:06:16,900 --> 00:06:20,400 Open the UserDAOTest class 62 00:06:20,500 --> 00:06:23,060 in the test source folder here 63 00:06:23,500 --> 00:06:26,400 UserDAOTest 64 00:06:28,700 --> 00:06:29,720 you see 65 00:06:30,000 --> 00:06:39,040 in the previous section we implemented our unit test for the create user 66 00:06:39,900 --> 00:06:42,700 Now let's... 67 00:06:42,900 --> 00:06:49,570 write another method for testing the get() method 68 00:06:55,400 --> 00:06:57,360 collapse this 69 00:07:01,200 --> 00:07:02,580 @Test 70 00:07:03,200 --> 00:07:12,000 public void testGetUsersFound() 71 00:07:19,600 --> 00:07:26,000 the user ID is Integer userId = 1 72 00:07:29,000 --> 00:07:34,160 userDAO.find() 73 00:07:36,300 --> 00:07:36,980 sorry 74 00:07:37,200 --> 00:07:38,340 get 75 00:07:39,000 --> 00:07:42,200 find() is its super class method 76 00:07:42,400 --> 00:07:46,000 get(userId) 77 00:07:55,400 --> 00:08:03,200 and assign the return statement to a new local variable user 78 00:08:10,500 --> 00:08:14,700 and we assert that the user should not be null 79 00:08:15,700 --> 00:08:20,520 assertNotNull(user) 80 00:08:20,800 --> 00:08:22,000 okay 81 00:08:28,200 --> 00:08:33,799 Let's see in our database, you see: there's an user with ID 1 82 00:08:34,200 --> 00:08:35,320 you see here 83 00:08:35,700 --> 00:08:41,740 ID 1 is the user with email nam@codejava.net 84 00:08:43,000 --> 00:08:50,000 Now let's run this testGetUsersFound() method 85 00:08:51,200 --> 00:08:56,000 right-click > Run As > JUnit Test 86 00:09:02,800 --> 00:09:03,640 you see 87 00:09:04,000 --> 00:09:06,960 a green bar appears here 88 00:09:07,300 --> 00:09:12,000 that means the test was successful 89 00:09:12,400 --> 00:09:22,000 and in the Console you can see that Hibernate prints a SQL statement 90 00:09:22,400 --> 00:09:27,500 the select statement here: select from users table 91 00:09:27,600 --> 00:09:28,600 you see 92 00:09:42,400 --> 00:09:53,740 and try to print the email of the returned user 93 00:09:54,300 --> 00:09:56,800 sysout Ctrl + space 94 00:09:57,000 --> 00:09:58,240 okay 95 00:10:00,100 --> 00:10:01,940 run the test method again 96 00:10:02,500 --> 00:10:03,800 JUnit 97 00:10:04,670 --> 00:10:06,020 Run 98 00:10:16,300 --> 00:10:17,380 sorry 99 00:10:21,200 --> 00:10:23,940 this should be in the get() method 100 00:10:25,100 --> 00:10:25,820 okay 101 00:10:27,100 --> 00:10:28,880 Run again 102 00:10:32,700 --> 00:10:33,580 sorry 103 00:10:34,500 --> 00:10:35,540 user 104 00:10:38,300 --> 00:10:40,140 run again 105 00:10:45,700 --> 00:10:53,000 and you can see, it means the email of the user with ID 1 is nam@codejava.net 106 00:10:53,400 --> 00:10:55,080 and in the database 107 00:10:55,400 --> 00:10:59,000 you see: nam@codejava.net 108 00:11:07,000 --> 00:11:10,380 and to make the code safer... 109 00:11:11,000 --> 00:11:12,600 we need to check... 110 00:11:12,820 --> 00:11:17,000 we need to check nullability of the Users object first 111 00:11:17,200 --> 00:11:22,840 if user is not null 112 00:11:23,800 --> 00:11:26,000 then print the email 113 00:11:26,300 --> 00:11:27,560 okay 114 00:11:29,800 --> 00:11:36,100 and the second test case is that the user could not be found 115 00:11:36,200 --> 00:11:37,360 okay 116 00:11:37,500 --> 00:11:41,200 so we create another test method 117 00:11:41,300 --> 00:11:42,820 @Test 118 00:11:44,600 --> 00:11:49,040 testGetUsersNotFound() 119 00:11:49,200 --> 00:11:50,140 okay 120 00:11:53,200 --> 00:12:02,440 and here we use the ID which doesn't belong to any user in the database 121 00:12:02,900 --> 00:12:05,700 Integer userId 122 00:12:05,800 --> 00:12:07,380 for example 123 00:12:07,900 --> 00:12:09,040 99 124 00:12:10,300 --> 00:12:16,640 in the database we don't have any user with that ID, right? 125 00:12:17,700 --> 00:12:24,000 Users user = userDAO.get(userId); 126 00:12:24,400 --> 00:12:30,000 and here we assert that the Users object is null 127 00:12:30,100 --> 00:12:31,160 okay 128 00:12:35,300 --> 00:12:39,340 Now, let's run this test method 129 00:12:45,500 --> 00:12:46,760 right-click 130 00:12:47,100 --> 00:12:51,000 Run As > JUnit Test 131 00:13:04,000 --> 00:13:08,400 oh we have an error here 132 00:13:09,200 --> 00:13:22,200 in the JpaDao class, we need to check the nullability of the entity object first before we refresh it 133 00:13:22,300 --> 00:13:23,080 okay 134 00:13:23,700 --> 00:13:24,800 no problem 135 00:13:25,500 --> 00:13:31,120 if entity is not null, refresh 136 00:13:32,800 --> 00:13:34,120 okay 137 00:13:36,500 --> 00:13:41,000 Now let's run the test method again 138 00:13:41,600 --> 00:13:42,680 Run 139 00:13:47,100 --> 00:13:50,460 you see, the test was successful 140 00:13:51,500 --> 00:13:55,000 testGetUsersNotFound was successful 141 00:13:55,400 --> 00:13:56,420 okay 142 00:13:58,400 --> 00:14:08,000 Next, let's implement the delete() method in the UserDAO class 143 00:14:08,600 --> 00:14:10,680 the delete() method here 144 00:14:11,100 --> 00:14:21,000 to delete a user that associates with the given user ID 145 00:14:21,800 --> 00:14:22,720 userId 146 00:14:22,720 --> 00:14:23,640 okay 147 00:14:24,800 --> 00:14:30,800 and we need to implement a delete() method in the superclass 148 00:14:31,000 --> 00:14:38,500 JpaDAO - so it can be reused by all the DAO classes 149 00:14:43,100 --> 00:14:44,220 okay 150 00:14:47,400 --> 00:14:50,200 so this method is... 151 00:14:51,700 --> 00:14:53,600 void delete() 152 00:14:54,300 --> 00:15:04,750 accept the class type and ID of the entity object 153 00:15:07,700 --> 00:15:22,000 and to delete an entity from the database we can use the remove() method of the EntityManager 154 00:15:22,300 --> 00:15:25,340 entityManager 155 00:15:25,900 --> 00:15:28,000 you can see the remove() method here 156 00:15:28,500 --> 00:15:35,980 it takes a parameter which is the reference of the object 157 00:15:42,000 --> 00:15:43,200 reference 158 00:15:43,500 --> 00:15:45,000 okay 159 00:15:47,700 --> 00:15:58,600 so we need to get the reference of the object to be removed by using the getReference() method of the EntityManager 160 00:15:59,300 --> 00:16:13,200 Object reference = entityManager.getReference(type, id); 161 00:16:15,400 --> 00:16:26,000 and because the delete operation makes change to the database, we need to wrap the code inside a transaction 162 00:16:26,600 --> 00:16:30,040 so we begin the transaction here 163 00:16:32,900 --> 00:16:38,103 entityManager.getTransaction().begin() 164 00:16:38,900 --> 00:16:43,000 and commit the transaction after 165 00:16:45,300 --> 00:16:50,060 entityManager.getTransaction().commit() 166 00:16:50,200 --> 00:16:51,260 okay 167 00:16:51,900 --> 00:16:59,660 Now in the UserDAO class, simply delegate the call to its super class 168 00:17:01,200 --> 00:17:03,760 super.delete() 169 00:17:05,400 --> 00:17:13,000 the type is the Users.class and userId 170 00:17:15,000 --> 00:17:15,760 okay 171 00:17:16,099 --> 00:17:19,780 Now let's test this method 172 00:17:20,900 --> 00:17:24,949 in the UserDAOTest class 173 00:17:25,800 --> 00:17:33,919 the test method is void testDeleteUser() 174 00:17:34,800 --> 00:17:35,580 okay 175 00:17:37,400 --> 00:17:38,700 @Test 176 00:17:40,200 --> 00:17:45,540 so let's see which user we want to delete 177 00:17:45,700 --> 00:17:50,000 the user with ID 5 you see here 178 00:17:50,400 --> 00:17:51,420 Tommy 179 00:17:52,000 --> 00:17:53,280 okay 180 00:17:55,300 --> 00:17:59,260 Integer userId = 5 181 00:18:01,500 --> 00:18:06,340 call the userDAO.delete(userId) 182 00:18:06,500 --> 00:18:07,520 okay 183 00:18:09,500 --> 00:18:13,500 and we can assert this test case by 184 00:18:13,900 --> 00:18:18,600 trying to get the user again with the given ID 185 00:18:18,600 --> 00:18:27,000 if the user is not found, then the test is successful 186 00:18:31,200 --> 00:18:40,100 Users user = userDAO.get(userId); 187 00:18:40,200 --> 00:18:43,200 and we assert that this user is null 188 00:18:43,900 --> 00:18:46,700 assertNull(user) 189 00:18:46,800 --> 00:18:48,200 okay 190 00:18:53,900 --> 00:18:57,640 Now let's run this test method 191 00:19:00,500 --> 00:19:02,960 testDeleteUsers 192 00:19:03,900 --> 00:19:08,820 Right-click > Run As > JUnit test 193 00:19:13,600 --> 00:19:15,160 and you see: 194 00:19:15,800 --> 00:19:17,600 it passes the test 195 00:19:17,800 --> 00:19:19,200 with a green bar 196 00:19:19,500 --> 00:19:20,680 appears here 197 00:19:20,800 --> 00:19:22,900 and in the Console view you can see 198 00:19:23,200 --> 00:19:30,200 Hibernate issued a SELECT SQL statement to... 199 00:19:32,200 --> 00:19:34,900 find the reference of the Users object 200 00:19:35,000 --> 00:19:41,300 and then it issued a DELETE SQL statement to delete... 201 00:19:41,500 --> 00:19:44,400 to remove the user from the database 202 00:19:44,500 --> 00:19:45,200 you see 203 00:19:45,300 --> 00:19:53,000 first it issued a SELECT SQL statement to get the reference 204 00:19:54,000 --> 00:19:56,320 get the reference here 205 00:19:56,900 --> 00:20:04,400 issued SELECT statement and then it issued DELETE statement to remove the reference 206 00:20:05,200 --> 00:20:16,500 and then because we call the UserDAO's get method method, it issued the SELECT SQL statement again 207 00:20:17,400 --> 00:20:19,000 very good, right? 208 00:20:23,900 --> 00:20:32,340 and in case the user we want to delete doesn't exist in the database 209 00:20:33,200 --> 00:20:36,420 it should throw an exception, so... 210 00:20:36,800 --> 00:20:42,500 write another test case for delete user here 211 00:20:44,600 --> 00:20:48,820 it should be testDeleteNonExistUsers, okay? 212 00:20:48,820 --> 00:20:56,000 public void testDeleteNonExistUsers() 213 00:20:59,700 --> 00:21:00,640 @Test 214 00:21:05,700 --> 00:21:08,400 copy - paste 215 00:21:08,900 --> 00:21:14,340 for example we are trying to delete the user with ID 55 216 00:21:15,800 --> 00:21:16,500 right? 217 00:21:16,700 --> 00:21:21,020 so this should throw an exception 218 00:21:21,900 --> 00:21:29,500 so we can use the "expected" attribute of the Test annotation 219 00:21:30,200 --> 00:21:33,040 and specify the exception class 220 00:21:33,900 --> 00:21:36,500 here we use a generic exception 221 00:21:36,600 --> 00:21:37,500 okay 222 00:21:39,800 --> 00:21:46,200 now, let's run this test method: testDeleteNonExistUsers 223 00:21:46,600 --> 00:21:48,980 Run As > JUnit Test 224 00:21:52,000 --> 00:21:52,800 you see 225 00:21:53,100 --> 00:21:55,680 the test was successful 226 00:21:57,300 --> 00:22:01,280 what if we don't use the expected attribute? 227 00:22:02,800 --> 00:22:05,590 run the test again 228 00:22:14,200 --> 00:22:17,240 and you can see there was an error 229 00:22:17,800 --> 00:22:22,500 javax.persistence.EntityNotFoundException 230 00:22:27,300 --> 00:22:28,400 so... 231 00:22:28,700 --> 00:22:34,400 to be more specific we can specify the exception class: EntityNotFoundException 232 00:22:34,800 --> 00:22:37,260 in the expected attribute 233 00:22:37,500 --> 00:22:38,560 here 234 00:22:39,200 --> 00:22:43,000 EntityNotFoundException 235 00:22:43,900 --> 00:22:44,580 okay 236 00:22:44,800 --> 00:22:47,460 and run the test case again 237 00:22:48,000 --> 00:22:49,120 Run 238 00:22:52,800 --> 00:22:55,450 now the test was successful 239 00:22:56,200 --> 00:22:57,400 okay 240 00:23:03,200 --> 00:23:04,320 and... 241 00:23:05,300 --> 00:23:08,720 to implement the listAll() and count() methods... 242 00:23:09,200 --> 00:23:15,200 you need to know the concept: Named Queries in JPA 243 00:23:15,800 --> 00:23:20,260 which you will learn in the next video 15521

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