Skip to content

Commit

Permalink
using criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihane committed Oct 29, 2021
1 parent d8aa445 commit d135e46
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Expand Up @@ -3,5 +3,6 @@
import java.util.List;

public interface CustomizedARepository {
List<A> getAllCriteria();
List<A> getAll();
}
Expand Up @@ -4,6 +4,10 @@
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.JoinType;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.transform.BasicTransformerAdapter;
import org.springframework.stereotype.Repository;
Expand All @@ -15,6 +19,17 @@ public class CustomizedARepositoryImpl implements CustomizedARepository {
@PersistenceContext
private EntityManager entityManager;

@Override
public List<A> getAllCriteria() {
// https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#criteria-from-fetch
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<A> criteria = builder.createQuery(A.class);
final Root<A> root = criteria.from(A.class);
root.fetch("bs", JoinType.LEFT);
criteria.select(root).distinct(true);
return entityManager.createQuery(criteria).getResultList();
}

@Override
public List<A> getAll() {
// https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#sql-entity-associations-query
Expand Down
Expand Up @@ -29,4 +29,9 @@ public List<A> index() {
public List<A> jpa() {
return repo.findAll();
}

@GetMapping("/criteria")
public List<A> criteria() {
return repo.getAllCriteria();
}
}

0 comments on commit d135e46

Please sign in to comment.