Hibernate 条件 aPI 多选
Hibernate Criteria aPI Multiselect
如果我使用 Hibernate Criteria API 比如:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root<OneEntity> entityOneRoot = criteriaQuery.from(OneEntity.class);
Root<TwoEntity> entityTwoRoot = criteriaQuery.from(TwoEntity.class);
criteriaQuery.multiselect(OneEntity, TwoEntity);
我是否需要使用 EQUAL 限制(如 SQL 中的 ON 限制)来使两个表(实体)之间的 ID 相等?因为两个表的笛卡尔积?
criteriaQuery.where(criteriaBuilder.equal(OneEntity.get("fk_id"), TwoEntity.get("id")));
我的意思是......在SQL中使用join我们需要使用on-clausule like:
select * from table_1 t1, table_2 t2 where t1.t2_id=t2.t1_id;
但我无法在 API 条件中找到有关它的信息。
来自 JPA 2.1 规范,章节 4.4.5 连接 :
An inner join may be implicitly specified by the use of a cartesian
product in the FROM
clause and a join condition in the WHERE
clause. In the absence of a join condition, this reduces to the
cartesian product.
The main use case for this generalized style of join is when a join
condition does not involve a foreign key relationship that is mapped
to an entity relationship.
Example:
SELECT c FROM Customer c, Employee e WHERE c.hatsize = e.shoesize
In general, use of this style of inner join (also referred to as
theta-join) is less typical than explicitly defined joins over
relationships.
由于 JPQL 与 Criteria 的工作方式相同 API,因此您的查询需要相同的限制。
如果我使用 Hibernate Criteria API 比如:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
Root<OneEntity> entityOneRoot = criteriaQuery.from(OneEntity.class);
Root<TwoEntity> entityTwoRoot = criteriaQuery.from(TwoEntity.class);
criteriaQuery.multiselect(OneEntity, TwoEntity);
我是否需要使用 EQUAL 限制(如 SQL 中的 ON 限制)来使两个表(实体)之间的 ID 相等?因为两个表的笛卡尔积?
criteriaQuery.where(criteriaBuilder.equal(OneEntity.get("fk_id"), TwoEntity.get("id")));
我的意思是......在SQL中使用join我们需要使用on-clausule like:
select * from table_1 t1, table_2 t2 where t1.t2_id=t2.t1_id;
但我无法在 API 条件中找到有关它的信息。
来自 JPA 2.1 规范,章节 4.4.5 连接 :
An inner join may be implicitly specified by the use of a cartesian product in the
FROM
clause and a join condition in theWHERE
clause. In the absence of a join condition, this reduces to the cartesian product.The main use case for this generalized style of join is when a join condition does not involve a foreign key relationship that is mapped to an entity relationship.
Example:
SELECT c FROM Customer c, Employee e WHERE c.hatsize = e.shoesize
In general, use of this style of inner join (also referred to as theta-join) is less typical than explicitly defined joins over relationships.
由于 JPQL 与 Criteria 的工作方式相同 API,因此您的查询需要相同的限制。