简体   繁体   English

HQL内部联接查询在ManyToOne集合成员中排除对象

[英]HQL inner join query exlude objects in ManyToOne set member

I have a Translations class eg: 我有一个翻译班,例如:

 class Translation{
      String key;
      String type;
      String userId;
      @OneToMany
      Set<TranslationValue> translations;
 }

which holds a OneToMany relationship with a TranslationValue class eg: 它与TranslationValue类保持OneToMany关系,例如:

 class TranslationValue{
      String language;
      String value;
      @ManyToOne
      Translation translation;
 }

I would like to query based on the TranslationValue.language member and return a list of Translation objects which contain a set with only 1 TranslationValue object -> the one which was used as query parameter eg: 我想基于TranslationValue.language成员进行查询,并返回包含一组仅包含1个TranslationValue对象->用作查询参数的set的Translation对象的列表,例如:

translationDao.findAllForLanguage("en");

This would return every translation object in the db that has TranslationValue.language = "en" and furthermore would remove from each object Translation.translations where language is not "en". 这将返回数据库中具有TranslationValue.language =“ en”的每个翻译对象,并且还将从每个对象中删除语言不是“ en”的Translation.translations。

So far I'm returning a list of all Translation objects which have a TranslationValue object with a language="en" member in their respective translations Sets. 到目前为止,我将返回所有Translation对象的列表,这些对象在各自的翻译集中都有一个TranslationValue对象,该对象的language =“ en”成员。 I need to remove all TranslationValue objects that don't have language="en" though. 我需要删除所有没有language =“ en”的TranslationValue对象。

EDIT: progress 编辑:进度

This query returns the correct amount of hits, but all values are null. 此查询返回正确的点击量,但所有值均为null。 -> ->

 @Override
public List<Translation> findAllForLanguage2(String language) {
    //TODO:finish
    final Query query = entityManager.createQuery(
            "select new " + getDomain().getSimpleName() + "(t.key,t.clientName,t.userId,t.type,t.platform,tv) from " + getDomain().getSimpleName() + " t right join t.translations tv where tv = some(from tv where tv.language = :language)");
    query.setParameter("language", language);


    return query.getResultList();
}

printing all from query
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}
translation: Translation{key='null', clientName='null', userId='null', type=null, platform='null', translations=null}

This query returns a Object [], where the second object is actually the TranslationValue object I want, but the Translation object is null -> 该查询返回一个Object [],其中第二个对象实际上是我想要的TranslationValue对象,但是Translation对象为null->

    @Override
public List<Object> findAllForLanguage(String language) {
    //TODO:finish
    final Query query = entityManager.createQuery(
            "from " + getDomain().getSimpleName() + " t full join t.translations tv where tv = some(from tv where tv.language = :language))");
    query.setParameter("language", language);


    return query.getResultList();
}

printing all from query
null
TranslationValue{language='en', value='feed'}
null
TranslationValue{language='en', value='feed'}
null
TranslationValue{language='en', value='broken'}
null
TranslationValue{language='en', value='broken'}
null
TranslationValue{language='en', value='water'}
null
TranslationValue{language='en', value='broken'}

This query should do fine 这个查询应该做得很好

select t from Translation t join t.translations tv 
where tv.language = :language 
and size(t.translations) = 1

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

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