new custom query operation for spring data and tests

This commit is contained in:
eugenp 2014-05-24 12:17:05 +03:00
parent 6da20ff4db
commit 39b7570d82
4 changed files with 22 additions and 8 deletions

View File

@ -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<Foo, Long>, JpaSpecificationExecutor<Foo> {
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
Foo retrieveByName(String name);
Foo retrieveByName(@Param("name") String name);
}

View File

@ -4,5 +4,7 @@ import org.baeldung.persistence.IOperations;
import org.baeldung.persistence.model.Foo;
public interface IFooService extends IOperations<Foo> {
//
Foo retrieveByName(String name);
}

View File

@ -32,6 +32,12 @@ public class FooService extends AbstractService<Foo> implements IFooService {
return dao;
}
// custom methods
public Foo retrieveByName(final String name) {
return dao.retrieveByName(name);
}
// overridden to be secured
@Override

View File

@ -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