具有两个参数的 Hibernate CriteriaBuilder
Hibernate CriteriaBuilder with two parameters
我在 Hibernate 5.2.1 中使用 Java8,在 MySQL 中使用 JPA2。
我有以下完美运行的查询:
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
CriteriaQuery<Rating> criteria = criteriaBuilder.createQuery(Rating.class);
Root<Rating> root = criteria.from(Rating.class);
ParameterExpression<Job> paramJob = criteriaBuilder.parameter(Job.class);
criteria.select(root).where(criteriaBuilder.equal(root.get("job"), paramJob));
如您所见,查询有一个参数paramJob
。我想添加第二个参数:
ParameterExpression<Person> paramPerson = criteriaBuilder.parameter(Person.class);
问题
如何修改以下行以适应第二个参数?
criteria.select(root).where(criteriaBuilder.equal(root.get("job"), paramJob));
我可以添加第二行吗:
criteria.select(root).where(criteriaBuilder.equal(root.get("person"), paramPerson));
谢谢
试试这个:
criteria.select(root).where(
criteriaBuilder.equal(root.get("job"), paramJob),
criteriaBuilder.equal(root.get("person"), paramPerson)
);
在我看来,这是一种更加动态的方法。你将不得不调整一些东西,但这样你也可以设置一个动态的 where 子句:
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
CriteriaQuery<Rating> criteria = criteriaBuilder.createQuery(Rating.class);
Root<Rating> root = criteria.from(Rating.class);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.equal(root.get("job"), Job.class)); //might need to change ther Person.class to whatever matches the job
predicates.add(criteriaBuilder.equal(root.get("person"), paramPerson)); //same as above
criteria.select(root).where(predicates.toArray(new Predicate[]{}));
//execute query
我在 Hibernate 5.2.1 中使用 Java8,在 MySQL 中使用 JPA2。
我有以下完美运行的查询:
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
CriteriaQuery<Rating> criteria = criteriaBuilder.createQuery(Rating.class);
Root<Rating> root = criteria.from(Rating.class);
ParameterExpression<Job> paramJob = criteriaBuilder.parameter(Job.class);
criteria.select(root).where(criteriaBuilder.equal(root.get("job"), paramJob));
如您所见,查询有一个参数paramJob
。我想添加第二个参数:
ParameterExpression<Person> paramPerson = criteriaBuilder.parameter(Person.class);
问题
如何修改以下行以适应第二个参数?
criteria.select(root).where(criteriaBuilder.equal(root.get("job"), paramJob));
我可以添加第二行吗:
criteria.select(root).where(criteriaBuilder.equal(root.get("person"), paramPerson));
谢谢
试试这个:
criteria.select(root).where(
criteriaBuilder.equal(root.get("job"), paramJob),
criteriaBuilder.equal(root.get("person"), paramPerson)
);
在我看来,这是一种更加动态的方法。你将不得不调整一些东西,但这样你也可以设置一个动态的 where 子句:
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final CriteriaBuilder criteriaBuilder = entityManagerFactory.getCriteriaBuilder();
CriteriaQuery<Rating> criteria = criteriaBuilder.createQuery(Rating.class);
Root<Rating> root = criteria.from(Rating.class);
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.equal(root.get("job"), Job.class)); //might need to change ther Person.class to whatever matches the job
predicates.add(criteriaBuilder.equal(root.get("person"), paramPerson)); //same as above
criteria.select(root).where(predicates.toArray(new Predicate[]{}));
//execute query