diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java index 7244f447a6..824d6ea546 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/IFooDao.java @@ -4,10 +4,11 @@ import org.baeldung.persistence.model.Foo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface IFooDao extends JpaRepository, JpaSpecificationExecutor { @Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)") - Foo retrieveByName(String name); + Foo retrieveByName(@Param("name") String name); } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java index 2bfe395df0..60d607b9ef 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/IFooService.java @@ -4,5 +4,7 @@ import org.baeldung.persistence.IOperations; import org.baeldung.persistence.model.Foo; public interface IFooService extends IOperations { - // + + Foo retrieveByName(String name); + } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java index f509068d42..533c0f5082 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/FooService.java @@ -32,6 +32,12 @@ public class FooService extends AbstractService implements IFooService { return dao; } + // custom methods + + public Foo retrieveByName(final String name) { + return dao.retrieveByName(name); + } + // overridden to be secured @Override diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java index e6a3385ce2..089d2d13a2 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -1,6 +1,7 @@ package org.baeldung.persistence.service; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertNotNull; import org.baeldung.persistence.IOperations; import org.baeldung.persistence.model.Foo; @@ -39,16 +40,20 @@ public class FooServicePersistenceIntegrationTest extends AbstractServicePersist service.create(new Foo()); } - // @Test(expected = DataIntegrityViolationException.class) - // public final void whenEntityWithLongNameIsCreated_thenDataException() { - // service.create(new Foo(randomAlphabetic(2048))); - // } + @Test(expected = DataIntegrityViolationException.class) + public final void whenEntityWithLongNameIsCreated_thenDataException() { + service.create(new Foo(randomAlphabetic(2048))); + } // custom Query method @Test - public final void givenUsingCustomQuery_whenExecuting_thenNoExceptions() { - // service.create(new Foo(randomAlphabetic(2048))); + public final void givenUsingCustomQuery_whenRetrievingEntity_thenFound() { + final String name = randomAlphabetic(6); + service.create(new Foo(name)); + + final Foo retrievedByName = service.retrieveByName(name); + assertNotNull(retrievedByName); } // work in progress