Activitats
Modificar les dades d'un usuari
L’objectiu d’aquesta activitat és escriure un test que comprovi que el codi té la capacitat de modificar les dades d’un usuari correctament.
Com podeu veure a la classe UserServiceImpl
, el mètode de l’especificació JPA que s’utilitza per modificar dades és merge
. Aquesta és la documentació:
/** * Merge the state of the given entity into the * current persistence context. * @param entity entity instance * @return the managed instance that the state was merged to * @throws IllegalArgumentException if instance is not an * entity or is a removed entity * @throws TransactionRequiredException if there is no transaction when * invoked on a container-managed entity manager of that is of type * <code>PersistenceContextType.TRANSACTION
- /
public <T> T merge(T entity); </code>
Com podeu veure, el mètode retorna l’objecte modificat. A partir del següent
edit
de la interfície UserService
, la seva implementació i afegiu el test editUser
al test UserServiceTest
, que comprovi que les dades es modifiquen correctament.
Eliminar un usuari
L’objectiu d’aquesta activitat és escriure un test que comprovi que el codi té la capacitat d’eliminar un usuari de la base de dades.
A partir del següent
removeUser
al test UserServiceTest
, que comprovi que un usuari s’esborra correctament.
Quan escriviu el codi us trobareu dos problemes. El primer és que quan s’intenti esborrar l’usuari hi haurà el següent error:
Caused by: java.lang.IllegalArgumentException: Entity must be managed to call remove: org.ioc.daw.user.User@59655457, try merging the detached and try the remove again.
Una pista: el problema és que per poder esborrar una entitat ha de ser part de la sessió, i això s’aconsegueix amb el mètode merge
.
Un cop solucioneu això us trobareu un altre problema: en executar el test es llançarà una excepció:
Caused by: javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
Aquí teniu la documentació del mètode getSingleResult
.
/** * Execute a SELECT query that returns a single untyped result. * @return the result * @throws NoResultException if there is no result * @throws NonUniqueResultException if more than one result * @throws IllegalStateException if called for a Java * Persistence query language UPDATE or DELETE statement * @throws QueryTimeoutException if the query execution exceeds * the query timeout value set and only the statement is * rolled back * @throws TransactionRequiredException if a lock mode other than * <code>NONE</code > has been set and there is no transaction * or the persistence context has not been joined to the transaction * @throws PessimisticLockException if pessimistic locking * fails and the transaction is rolled back * @throws LockTimeoutException if pessimistic locking * fails and only the statement is rolled back * @throws PersistenceException if the query execution exceeds * the query timeout value set and the transaction * is rolled back */ Object getSingleResult();
Modifiqueu el mètode findUserByUsername
perquè retorni null si no troba cap resultat a la base de dades.
Trobar els usuaris actius
L’objectiu d’aquesta activitat és escriure el codi i un test que permeti trobar els usuaris actius.
Modifiqueu UserService
i UserServiceImpl
i afegiu-hi un test.
'Rank' d'un usuari
L’objectiu d’aquesta activitat és escriure el codi i un test que permeti trobar l’usuari amb el rank més gran.
Modifiqueu UserService
i UserServiceImpl
i afegiu-hi un test.