From 1b7b58052b06696a19d2a5ffcf8cc2a6d67cb0df Mon Sep 17 00:00:00 2001 From: DOHA Date: Tue, 27 Jan 2015 19:02:41 +0200 Subject: [PATCH] spring query clean up --- .../baeldung/persistence/dao/IUserDAO.java | 11 --- .../org/baeldung/persistence/dao/UserDAO.java | 43 --------- .../persistence/dao/UserRepository.java | 9 -- .../persistence/dao/UserSpecifications.java | 45 --------- .../org/baeldung/persistence/model/User.java | 96 ------------------- .../org/baeldung/persistence/model/User_.java | 11 --- .../persistence/service/UserService.java | 28 ------ .../query/JPACriteriaQueryTest.java | 85 ---------------- .../query/JPASpecificationsTest.java | 89 ----------------- .../query/JPACriteriaQueryTest.java | 3 +- .../persistence/query/JPAQuerydslTest.java | 3 +- .../query/JPASpecificationsTest.java | 3 +- 12 files changed, 3 insertions(+), 423 deletions(-) delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/dao/IUserDAO.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/dao/UserDAO.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/dao/UserRepository.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/dao/UserSpecifications.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/model/User.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/model/User_.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/service/UserService.java delete mode 100644 spring-jpa/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java delete mode 100644 spring-jpa/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/IUserDAO.java b/spring-jpa/src/main/java/org/baeldung/persistence/dao/IUserDAO.java deleted file mode 100644 index a03824b4ae..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/IUserDAO.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.baeldung.persistence.dao; - -import java.util.List; - -import org.baeldung.persistence.model.User; - -public interface IUserDAO { - List searchUser(String first, String last, int minAge); - - void save(User entity); -} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserDAO.java b/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserDAO.java deleted file mode 100644 index 56e79b504d..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserDAO.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.baeldung.persistence.dao; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - -import org.baeldung.persistence.model.User; -import org.springframework.stereotype.Repository; - -@Repository -public class UserDAO implements IUserDAO { - - @PersistenceContext - private EntityManager entityManager; - - @Override - public List searchUser(final String first, final String last, final int minAge) { - final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); - final CriteriaQuery query = builder.createQuery(User.class); - final Root r = query.from(User.class); - - Predicate predicate = builder.conjunction(); - predicate = builder.and(predicate, builder.like(r.get("firstName"), "%" + first + "%")); - predicate = builder.and(predicate, builder.like(r.get("lastName"), "%" + last + "%")); - predicate = builder.and(predicate, builder.greaterThanOrEqualTo(r.get("age"), minAge)); - - query.where(predicate); - - final List result = entityManager.createQuery(query).getResultList(); - return result; - } - - @Override - public void save(final User entity) { - entityManager.persist(entity); - } - -} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserRepository.java b/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserRepository.java deleted file mode 100644 index de7acf60d5..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.model.User; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; - -public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { - -} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserSpecifications.java b/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserSpecifications.java deleted file mode 100644 index a0f545b158..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/UserSpecifications.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.baeldung.persistence.dao; - -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; - -import org.baeldung.persistence.model.User; -import org.baeldung.persistence.model.User_; -import org.springframework.data.jpa.domain.Specification; - -public class UserSpecifications { - public static Specification firstNameIsLike(final String term) { - - return new Specification() { - @Override - public Predicate toPredicate(final Root root, final CriteriaQuery query, final CriteriaBuilder cb) { - return cb.like(root.get(User_.firstName), "%"+term+"%"); - } - - }; - } - - public static Specification lastNameIsLike(final String term) { - - return new Specification() { - @Override - public Predicate toPredicate(final Root root, final CriteriaQuery query, final CriteriaBuilder cb) { - return cb.like(root.get(User_.lastName), "%" + term + "%"); - } - - }; - } - - public static Specification ageIsGreaterThan(final int minAge) { - - return new Specification() { - @Override - public Predicate toPredicate(final Root root, final CriteriaQuery query, final CriteriaBuilder cb) { - return cb.greaterThanOrEqualTo(root.get(User_.age), minAge); - } - - }; - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/User.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/User.java deleted file mode 100644 index d220f4458e..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/User.java +++ /dev/null @@ -1,96 +0,0 @@ -package org.baeldung.persistence.model; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; - -@Entity -public class User { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - private String firstName; - - private String lastName; - - private String email; - - private int age; - - public User() { - super(); - } - - public Long getId() { - return id; - } - - public void setId(final Long id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(final String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(final String lastName) { - this.lastName = lastName; - } - - public String getEmail() { - return email; - } - - public void setEmail(final String username) { - email = username; - } - - public int getAge() { - return age; - } - - public void setAge(final int age) { - this.age = age; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((email == null) ? 0 : email.hashCode()); - return result; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - final User user = (User) obj; - if (!email.equals(user.email)) - return false; - return true; - } - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("User [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]"); - return builder.toString(); - } - -} \ No newline at end of file diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/User_.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/User_.java deleted file mode 100644 index c7b6ba2de8..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/User_.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.baeldung.persistence.model; - -import javax.persistence.metamodel.SingularAttribute; -import javax.persistence.metamodel.StaticMetamodel; - -@StaticMetamodel(User.class) -public class User_ { - public static volatile SingularAttribute firstName; - public static volatile SingularAttribute lastName; - public static volatile SingularAttribute age; -} \ No newline at end of file diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/service/UserService.java b/spring-jpa/src/main/java/org/baeldung/persistence/service/UserService.java deleted file mode 100644 index 90ab7f42f5..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/service/UserService.java +++ /dev/null @@ -1,28 +0,0 @@ -package org.baeldung.persistence.service; - -import java.util.List; - -import org.baeldung.persistence.dao.IUserDAO; -import org.baeldung.persistence.model.User; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional -public class UserService { - @Autowired - private IUserDAO dao; - - public UserService() { - super(); - } - - public void saveUser(final User user) { - dao.save(user); - } - - public List searchUser(final String first, final String last, final int minAge) { - return dao.searchUser(first, last, minAge); - } -} diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java deleted file mode 100644 index 7482f296b5..0000000000 --- a/spring-jpa/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.baeldung.persistence.query; - -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.baeldung.config.PersistenceJPAConfig; -import org.baeldung.persistence.model.User; -import org.baeldung.persistence.service.UserService; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import org.springframework.test.context.transaction.TransactionConfiguration; -import org.springframework.transaction.annotation.Transactional; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) -@Transactional -@TransactionConfiguration -public class JPACriteriaQueryTest { - - @Autowired - private UserService userService; - - private User userJohn; - - private User userTom; - - @Before - public void init() { - userJohn = new User(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); - userJohn.setEmail("john@doe.com"); - userJohn.setAge(22); - userService.saveUser(userJohn); - - userTom = new User(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); - userTom.setEmail("tom@doe.com"); - userTom.setAge(26); - userService.saveUser(userTom); - } - - @Test - public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final List result = userService.searchUser("John", "Doe", 0); - - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); - } - - @Test - public void givenLast_whenGettingListOfUsers_thenCorrect() { - final List result = userService.searchUser("", "doe", 0); - assertEquals(2, result.size()); - } - - @Test - public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { - final List result = userService.searchUser("", "doe", 25); - - assertEquals(1, result.size()); - assertEquals(userTom.getEmail(), result.get(0).getEmail()); - } - - @Test - public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { - final List result = userService.searchUser("Adam", "Fox", 0); - assertEquals(0, result.size()); - } - - @Test - public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { - final List result = userService.searchUser("jo", "", 0); - - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); - } -} diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java deleted file mode 100644 index 374afc1ef7..0000000000 --- a/spring-jpa/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.baeldung.persistence.query; - -import static org.baeldung.persistence.dao.UserSpecifications.ageIsGreaterThan; -import static org.baeldung.persistence.dao.UserSpecifications.firstNameIsLike; -import static org.baeldung.persistence.dao.UserSpecifications.lastNameIsLike; -import static org.junit.Assert.assertEquals; - -import java.util.List; - -import org.baeldung.config.PersistenceJPAConfig; -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.model.User; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.domain.Specifications; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import org.springframework.test.context.transaction.TransactionConfiguration; -import org.springframework.transaction.annotation.Transactional; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceJPAConfig.class }, loader = AnnotationConfigContextLoader.class) -@Transactional -@TransactionConfiguration -public class JPASpecificationsTest { - - @Autowired - private UserRepository repository; - - private User userJohn; - - private User userTom; - - @Before - public void init() { - userJohn = new User(); - userJohn.setFirstName("John"); - userJohn.setLastName("Doe"); - userJohn.setEmail("john@doe.com"); - userJohn.setAge(22); - repository.save(userJohn); - - userTom = new User(); - userTom.setFirstName("Tom"); - userTom.setLastName("Doe"); - userTom.setEmail("tom@doe.com"); - userTom.setAge(26); - repository.save(userTom); - } - - @Test - public void givenLast_whenGettingListOfUsers_thenCorrect() { - final List result = repository.findAll(lastNameIsLike("doe")); - assertEquals(2, result.size()); - } - - @Test - public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final List result = repository.findAll(Specifications.where(lastNameIsLike("doe")).and(firstNameIsLike("john"))); - - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); - } - - @Test - public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { - final List result = repository.findAll(Specifications.where(lastNameIsLike("doe")).and(ageIsGreaterThan(25))); - - assertEquals(1, result.size()); - assertEquals(userTom.getEmail(), result.get(0).getEmail()); - } - - @Test - public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { - final List result = repository.findAll(Specifications.where(lastNameIsLike("Adam")).and(firstNameIsLike("Fox"))); - assertEquals(0, result.size()); - } - - @Test - public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { - final List result = repository.findAll(firstNameIsLike("jo")); - - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); - } -} diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java index 9a2716232d..b805263cf7 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java @@ -17,12 +17,11 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@ContextConfiguration(classes = { PersistenceConfig.class }) @Transactional @TransactionConfiguration public class JPACriteriaQueryTest { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java index 2f2978378d..0a53831c41 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java @@ -19,12 +19,11 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@ContextConfiguration(classes = { PersistenceConfig.class }) @Transactional @TransactionConfiguration public class JPAQuerydslTest { diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java index 7ff447c285..7cd279d6d0 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java @@ -18,12 +18,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.Specifications; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@ContextConfiguration(classes = { PersistenceConfig.class }) @Transactional @TransactionConfiguration public class JPASpecificationsTest {