forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
spring-jpa/src/main/java/org/baeldung/persistence/dao/IUserDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.baeldung.persistence.dao; | ||
|
||
import java.util.List; | ||
|
||
import org.baeldung.persistence.model.User; | ||
|
||
public interface IUserDAO { | ||
List<User> searchUser(String first, String last, int minAge); | ||
|
||
void save(User entity); | ||
} |
43 changes: 43 additions & 0 deletions
43
spring-jpa/src/main/java/org/baeldung/persistence/dao/UserDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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<User> searchUser(final String first, final String last, final int minAge) { | ||
final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); | ||
final CriteriaQuery<User> 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<User> result = entityManager.createQuery(query).getResultList(); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void save(final User entity) { | ||
entityManager.persist(entity); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
spring-jpa/src/main/java/org/baeldung/persistence/model/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
spring-jpa/src/main/java/org/baeldung/persistence/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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<User> searchUser(final String first, final String last, final int minAge) { | ||
return dao.searchUser(first, last, minAge); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
spring-jpa/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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 user_john; | ||
|
||
private User user_tom; | ||
|
||
@Before | ||
public void init() { | ||
user_john = new User(); | ||
user_john.setFirstName("John"); | ||
user_john.setLastName("Doe"); | ||
user_john.setEmail("[email protected]"); | ||
user_john.setAge(22); | ||
userService.saveUser(user_john); | ||
|
||
user_tom = new User(); | ||
user_tom.setFirstName("Tom"); | ||
user_tom.setLastName("Doe"); | ||
user_tom.setEmail("[email protected]"); | ||
user_tom.setAge(26); | ||
userService.saveUser(user_tom); | ||
} | ||
|
||
@Test | ||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { | ||
final List<User> result = userService.searchUser("John", "Doe", 0); | ||
|
||
assertEquals(1, result.size()); | ||
assertEquals(user_john.getEmail(), result.get(0).getEmail()); | ||
} | ||
|
||
@Test | ||
public void givenLast_whenGettingListOfUsers_thenCorrect() { | ||
final List<User> result = userService.searchUser("", "doe", 0); | ||
assertEquals(2, result.size()); | ||
} | ||
|
||
@Test | ||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { | ||
final List<User> result = userService.searchUser("", "doe", 25); | ||
|
||
assertEquals(1, result.size()); | ||
assertEquals(user_tom.getEmail(), result.get(0).getEmail()); | ||
} | ||
|
||
@Test | ||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { | ||
final List<User> result = userService.searchUser("Adam", "Fox", 0); | ||
assertEquals(0, result.size()); | ||
} | ||
|
||
@Test | ||
public void givenPartialFirstAndLast_whenGettingListOfUsers_thenCorrect() { | ||
final List<User> result = userService.searchUser("jo", "", 0); | ||
|
||
assertEquals(1, result.size()); | ||
assertEquals(user_john.getEmail(), result.get(0).getEmail()); | ||
} | ||
} |