From 09b8b9a3da7e219a3527419a0b3ada9db6fe00a3 Mon Sep 17 00:00:00 2001 From: Matthew Warman Date: Tue, 27 Oct 2015 15:10:51 -0400 Subject: [PATCH] Updated AccountRepository to illustrate various query definition strategies. --- README.md | 4 + .../java/org/example/ws/model/Account.java | 8 + .../ws/repository/AccountRepository.java | 68 ++++++- .../ws/repository/AccountRepositoryTests.java | 189 ++++++++++++++++++ 4 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/example/ws/repository/AccountRepositoryTests.java diff --git a/README.md b/README.md index 8e78793..3500c74 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ The branch named `reference-entity` contains the source code illustrated in the The branch named `joda` contains the source code illustrated in the episode [Using JODA Framework Classes as JPA Entity Model Attributes with Spring Data JPA](https://youtu.be/OcKtf_-K5cc?list=PLGDwUiT1wr693flGbjtm0WoB_722X6lNc). +#### jpa-query-definition + +The branch named `jpa-query-definition` contains the source code illustrated in the episode [Exploring Spring Data JPA Query Definition Strategies](https://youtu.be/S5vZP_03ENY?list=PLGDwUiT1wr693flGbjtm0WoB_722X6lNc). + ## Languages diff --git a/src/main/java/org/example/ws/model/Account.java b/src/main/java/org/example/ws/model/Account.java index 5cbb0ce..70c0358 100644 --- a/src/main/java/org/example/ws/model/Account.java +++ b/src/main/java/org/example/ws/model/Account.java @@ -8,6 +8,8 @@ import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.validation.constraints.NotNull; /** @@ -17,6 +19,12 @@ * * @author Matt Warman */ +@NamedQueries({ @NamedQuery( + name = "Account.findByUsernameNamedQuery", + query = "SELECT a FROM Account a WHERE a.username = ?1"), + @NamedQuery( + name = "Account.findAllEnabledLikeUsernameNamedQuery", + query = "SELECT a FROM Account a WHERE UPPER(a.username) like UPPER(?1) AND a.enabled = true ORDER BY a.username ASC") }) @Entity public class Account extends TransactionalEntity { diff --git a/src/main/java/org/example/ws/repository/AccountRepository.java b/src/main/java/org/example/ws/repository/AccountRepository.java index 8c88888..e14ebbc 100644 --- a/src/main/java/org/example/ws/repository/AccountRepository.java +++ b/src/main/java/org/example/ws/repository/AccountRepository.java @@ -1,7 +1,11 @@ package org.example.ws.repository; +import java.util.Collection; + import org.example.ws.model.Account; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; /** @@ -16,11 +20,73 @@ public interface AccountRepository extends JpaRepository { /** - * Query for a single Account entity by username. + * Query for a single Account entity by username. This method illustrates + * the Query Method approach for query definition. + * + *
+     * SELECT a FROM Account a WHERE a.username = ?1
+     * 
* * @param username A String username value to query the repository. * @return An Account or null if none found. */ Account findByUsername(String username); + /** + * Query for a Collection of Account entities by username. This method + * illustrates the Query Method approach for query definition. + * + *
+     * SELECT a
+     *   FROM Account a
+     *   WHERE UPPER(a.username) like UPPER('%' + ?1 + '%')
+     *     AND a.enabled = true
+     *   ORDER BY A.username ASC
+     * 
+ * + * @param username A String username value. + * @return A Collection of Account entities. + */ + Collection findByUsernameContainingIgnoreCaseAndEnabledTrueOrderByUsernameAsc( + String username); + + /** + * Query for a single Account entity by username. This method is associated + * with a NamedQuery annotated on the Account entity class. + * + * @param username A String username value to query the repository. + * @return An Account or null if none found. + */ + Account findByUsernameNamedQuery(String username); + + /** + * Query for a Collection of Account entities by username. This method is + * associated with a NamedQuery annotated on the Account entity class. + * + * @param username A String username value. + * @return A Collection of Account entities. + */ + Collection findAllEnabledLikeUsernameNamedQuery(String username); + + /** + * Query for a single Account entity by username. This method illustrates + * the Query annotation approach for query definition. + * + * @param username A String username value. + * @return An Account or null if none found. + */ + @Query("SELECT a FROM Account a WHERE a.username = :username") + Account findByUsernameQuery(@Param("username") String username); + + /** + * Query for a Collection of Account entities by username. This method + * illustrates the Query annotation approach for query definition. + * + * @param username A String username value. + * @return A Collection of Account entities. + */ + @Query("SELECT a FROM Account a WHERE a.enabled = true AND UPPER(a.username) like UPPER(:username) ORDER BY a.username ASC") + Collection findAllEnabledLikeUsernameQuery( + @Param("username") String username); + } diff --git a/src/test/java/org/example/ws/repository/AccountRepositoryTests.java b/src/test/java/org/example/ws/repository/AccountRepositoryTests.java new file mode 100644 index 0000000..76c9da6 --- /dev/null +++ b/src/test/java/org/example/ws/repository/AccountRepositoryTests.java @@ -0,0 +1,189 @@ +package org.example.ws.repository; + +import java.util.Collection; + +import org.example.ws.AbstractTest; +import org.example.ws.model.Account; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; + +/** + * Unit tests for the AccountRepository interface. + * + * @author Matt Warman + * + */ +@Transactional +public class AccountRepositoryTests extends AbstractTest { + + @Autowired + private AccountRepository repository; + + @Before + public void setUp() { + super.setUp(); + } + + @Test + public void testFindByUsername() { + + String username = "user"; + + Account entity = repository.findByUsername(username); + + Assert.assertNotNull("failure - expected entity not null", entity); + Assert.assertEquals("failure - expected username attribute match", + username, entity.getUsername()); + + } + + @Test + public void testFindByUsernameLike() { + + String username = "er"; + + Collection list = repository + .findByUsernameContainingIgnoreCaseAndEnabledTrueOrderByUsernameAsc( + username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindByUsernameLikeMixedCase() { + + String username = "eR"; + + Collection list = repository + .findByUsernameContainingIgnoreCaseAndEnabledTrueOrderByUsernameAsc( + username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindByUsernameLikeMixedCaseSingleResult() { + + String username = "UsEr"; + + Collection list = repository + .findByUsernameContainingIgnoreCaseAndEnabledTrueOrderByUsernameAsc( + username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 1, list.size()); + + } + + @Test + public void testFindByUsernameNamedQuery() { + + String username = "user"; + + Account entity = repository.findByUsernameNamedQuery(username); + + Assert.assertNotNull("failure - expected entity not null", entity); + Assert.assertEquals("failure - expected username attribute match", + username, entity.getUsername()); + + } + + @Test + public void testFindAllEnabledLikeUsernameNamedQuery() { + + String username = "%er%"; + + Collection list = repository + .findAllEnabledLikeUsernameNamedQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindAllEnabledLikeUsernameNamedQueryMixedCase() { + + String username = "%eR%"; + + Collection list = repository + .findAllEnabledLikeUsernameNamedQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindAllEnabledLikeUsernameNamedQueryMixedCaseSingleResult() { + + String username = "%UsEr%"; + + Collection list = repository + .findAllEnabledLikeUsernameNamedQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 1, list.size()); + + } + + @Test + public void testFindByUsernameQuery() { + + String username = "user"; + + Account entity = repository.findByUsernameQuery(username); + + Assert.assertNotNull("failure - expected entity not null", entity); + Assert.assertEquals("failure - expected username attribute match", + username, entity.getUsername()); + + } + + @Test + public void testFindAllEnabledLikeUsernameQuery() { + + String username = "%er%"; + + Collection list = repository + .findAllEnabledLikeUsernameQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindAllEnabledLikeUsernameQueryMixedCase() { + + String username = "%eR%"; + + Collection list = repository + .findAllEnabledLikeUsernameQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 2, list.size()); + + } + + @Test + public void testFindAllEnabledLikeUsernameQueryMixedCaseSingleResult() { + + String username = "%UsEr%"; + + Collection list = repository + .findAllEnabledLikeUsernameQuery(username); + + Assert.assertNotNull("failure - expected list not null", list); + Assert.assertEquals("failure - expected list size", 1, list.size()); + + } + +}