Reputation: 393
I'm new to Spring-boot and Jpa, I have a problem with a query. I have to implement a method in which if there are no Users with a specific name, it returns me an error message while if there are, it returns me the one with the furthest insertion date. Now I have done the code but I don't know how to insert the exception in case there is no name in the database. I don't know how to put it in the method! Below I enter the method I made
public UserUpdateEntity findFirstByName(String name) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<UserUpdateEntity> criteria = builder.createQuery(UserUpdateEntity.class);
Root<UserUpdateEntity> root = criteria.from(UserUpdateEntity.class);
criteria.select(root).where(builder.equal(root.get("name"), name));
criteria.orderBy(builder.asc(root.get("timestamp")));
TypedQuery<UserUpdateEntity> query = em.createQuery(criteria).setMaxResults(1);
return query.getSingleResult();
}
Upvotes: 0
Views: 349
Reputation: 6973
Question is misleading, this is how i have done it, when you use get single result it is bound to throw NoResultException
in this case you should not setMaxResults
and getSingleResult
instead use getResultList()
when its size is 1 you got the data present in the table otherwise something is fishy. Please follow my code below:
Entity class
@Entity
@Table(name = "user_update_entity")
public class UserUpdateEntity {
@Id
@GeneratedValue
@Type(type = "uuid-char")
private UUID id;
private String name;
@Temporal(value = TemporalType.TIMESTAMP)
private Date timestamp;
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
}
CriteriQuery
private UserUpdateEntity searchUserUpdateEntity(String name) throws IndexOutOfBoundsException {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<UserUpdateEntity> criteriaQuery = criteriaBuilder.createQuery(UserUpdateEntity.class);
Root<UserUpdateEntity> from = criteriaQuery.from(UserUpdateEntity.class);
criteriaQuery.where(criteriaBuilder.equal(from.get(UserUpdateEntity_.NAME), name));
TypedQuery<UserUpdateEntity> typedQuery = entityManager.createQuery(criteriaQuery);
// This line will throw IndexOutOfBoundsException if result set is less than 1
UserUpdateEntity result = typedQuery.getResultList().get(0);
return result;
}
Test to check the logic
@Test
void testUserExists() {
UserUpdateEntity userUpdateEntity = new UserUpdateEntity();
userUpdateEntity.setName("random-user");
userUpdateEntity.setTimestamp(Calendar.getInstance().getTime());
entityManager.persist(userUpdateEntity);
Assertions.assertNotNull(userUpdateEntity.getId());
UserUpdateEntity searched = searchUserUpdateEntity(userUpdateEntity.getName());
Assertions.assertSame(searched, userUpdateEntity);
try {
searched = searchUserUpdateEntity("asdada");
} catch (IndexOutOfBoundsException e) {
Assertions.assertTrue(true);
}
}
Upvotes: 2