new custom query operation for spring data and tests
This commit is contained in:
parent
6da20ff4db
commit
39b7570d82
|
@ -4,10 +4,11 @@ import org.baeldung.persistence.model.Foo;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
public interface IFooDao extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
|
public interface IFooDao extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
|
||||||
|
|
||||||
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
|
||||||
Foo retrieveByName(String name);
|
Foo retrieveByName(@Param("name") String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,7 @@ import org.baeldung.persistence.IOperations;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
|
||||||
public interface IFooService extends IOperations<Foo> {
|
public interface IFooService extends IOperations<Foo> {
|
||||||
//
|
|
||||||
|
Foo retrieveByName(String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,12 @@ public class FooService extends AbstractService<Foo> implements IFooService {
|
||||||
return dao;
|
return dao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// custom methods
|
||||||
|
|
||||||
|
public Foo retrieveByName(final String name) {
|
||||||
|
return dao.retrieveByName(name);
|
||||||
|
}
|
||||||
|
|
||||||
// overridden to be secured
|
// overridden to be secured
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.baeldung.persistence.service;
|
package org.baeldung.persistence.service;
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
import org.baeldung.persistence.IOperations;
|
import org.baeldung.persistence.IOperations;
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
|
@ -39,16 +40,20 @@ public class FooServicePersistenceIntegrationTest extends AbstractServicePersist
|
||||||
service.create(new Foo());
|
service.create(new Foo());
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Test(expected = DataIntegrityViolationException.class)
|
@Test(expected = DataIntegrityViolationException.class)
|
||||||
// public final void whenEntityWithLongNameIsCreated_thenDataException() {
|
public final void whenEntityWithLongNameIsCreated_thenDataException() {
|
||||||
// service.create(new Foo(randomAlphabetic(2048)));
|
service.create(new Foo(randomAlphabetic(2048)));
|
||||||
// }
|
}
|
||||||
|
|
||||||
// custom Query method
|
// custom Query method
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingCustomQuery_whenExecuting_thenNoExceptions() {
|
public final void givenUsingCustomQuery_whenRetrievingEntity_thenFound() {
|
||||||
// service.create(new Foo(randomAlphabetic(2048)));
|
final String name = randomAlphabetic(6);
|
||||||
|
service.create(new Foo(name));
|
||||||
|
|
||||||
|
final Foo retrievedByName = service.retrieveByName(name);
|
||||||
|
assertNotNull(retrievedByName);
|
||||||
}
|
}
|
||||||
|
|
||||||
// work in progress
|
// work in progress
|
||||||
|
|
Loading…
Reference in New Issue