简体   繁体   English

Java:使用条件创建内部联接

[英]Java: create a inner join using criteria

I'm having a hard time searching for good examples on how to create a criteria query with inner join.我很难搜索有关如何使用内部联接创建条件查询的好示例。 I've created the query below that would be what I'd like to do with the criteria.我已经创建了下面的查询,这将是我想要对标准做的事情。

SELECT DISTINCT *
FROM DT_DOCUMENT as document 
INNER JOIN DT_TRANSLATION as translation
ON translation.language_id IN(1, 2, 3) 
WHERE document.id = translation.document_id
AND document.title LIKE '%Document%';

Return all documents with the title Document and translations with id 1,2 and 3.返回标题为 Document 的所有文档和 ID 为 1,2 和 3 的翻译。

I was able to create 2 different select with the criteria, but I can not generate the inner join between those tables to unify the code.我能够使用标准创建 2 个不同的选择,但是我无法在这些表之间生成内部连接来统一代码。

Translation criteria query翻译条件查询

CriteriaQuery<Translation> translationQuery = builder.createQuery(Translation.class);
Root<Translation> translation = translationQuery.from(Translation.class);

List<Long> ids = new ArrayList<>();
ids.add(1);
ids.add(2);
ids.add(3);
Predicate idPredicate = translation.in(ids);
translationQuery.where(idPredicate);
translationQuery.distinct(true);

TypedQuery<Translation> query = this.entityManager.createQuery(translationQuery);
query.getResultList();

Return all translations with id 1,2 and 3;返回 id 为 1,2 和 3 的所有翻译;

Document criteria query文档条件查询

CriteriaQuery<Document> documentQuery = builder.createQuery(Document.class);
Root<Document> document = documentQuery.from(Document.class);

Predicate titlePredicate = builder.like(document.get("title"), "%Document%");
documentQuery.where(titlePredicate);
TypedQuery<Document> query = this.entityManager.createQuery(documentQuery);
query.getResultList();

Return all documents with title Document.返回标题为 Document 的所有文档。

Any tip?任何提示? Thanks.谢谢。

After much time searching I made this solution unifying the 2 queries and using the inner join:经过长时间的搜索,我制定了这个解决方案,统一了 2 个查询并使用了内部联接:

    //query creation
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Document> documentQuery = builder.createQuery(Document.class);
    Root<Document> root = documentQuery.from(Document.class);

    //parameter list for where
    List<Predicate> predicateList = new ArrayList<Predicate>();

    //doing the join: pass the translations reference (list <Translations) that exists in the document model.
    Join<Document, Translation> documentTranslationJoin = root.join("translations");
   //I get the ids of the languages ​​associated with the translations found in the join
    Path<Long> translationLanguageId = documentTranslationJoin.get("language");
    //I passed the ids of the languages ​​that I want to find translations
    List<Long> ids = new ArrayList<>();
    ids.add(1);
    ids.add(2);
    ids.add(3);

    //considers languages ​​of translations
    Predicate predicateTranslationId = builder.isTrue(translationLanguageId.in(ids));
    //I passed the ids of the languages ​​that will be the where looking for the translations that have the ids of the languages ​​that I want to see
    predicateList.add(predicateTranslationId);


    //considers title of document
    Predicate titlePredicate = builder.like(root.get("title"),"%documento%");
    predicateList.add(titlePredicate);

    //where
    Predicate[] predicates = new Predicate[predicateList.size()];
    predicateList.toArray(predicates);
    documentQuery.where(predicates);

    //execution of the query with its parameters
    TypedQuery<Document> query = this.entityManager.createQuery(documentQuery);

    query.setFirstResult(0);
    query.setMaxResults(8);

    //results
    List<Document> documents = query.getResultList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM