2016-12-29 84 views
2

我有兩個表。JPA標準加入OneToMany表where子句不起作用

CREATE TABLE public.question 
(
    id  SERIAL PRIMARY KEY 
    , day  VARCHAR(2)  NOT NULL 
    , month VARCHAR(2)  NOT NULL 
    , year  VARCHAR(4)   NOT NULL 
); 

CREATE TABLE public.question_translation 
(
    id SERIAL PRIMARY KEY 
    , question_id INT REFERENCES public.question(id) NOT NULL 
    , question_text TEXT        NOT NULL 
    , language  VARCHAR(2)       NOT NULL 
); 

現在我想創建條件來檢索問題。 SQL是這樣的:

SELECT * FROM question q LEFT JOIN question_translation qt ON q.id = qt.question_id WHERE qt.language = 'en'

在Java中使用JPA規定 - 它看起來像這樣:

@Override 
public Collection<Question> findByMonthYear(String month, String year, String locale) { 
    EntityManager em = sessionFactory.createEntityManager(); 
    CriteriaBuilder builder = em.getCriteriaBuilder(); 
    CriteriaQuery<Question> criteriaQuery = builder.createQuery(Question.class); 

    List<Predicate> predicates = new ArrayList<>(); 

    Root<Question> questionRoot = criteriaQuery.from(Question.class); 
    ListJoin<Question, QuestionTranslation> questionTranslationJoinRoot = questionRoot.join(Question_.questionTranslation, JoinType.LEFT); 

    predicates.add(builder.equal(questionRoot.get(Question_.month), month)); 
    predicates.add(builder.equal(questionRoot.get(Question_.year), year)); 
    predicates.add(builder.equal(questionTranslationJoinRoot.get(QuestionTranslation_.language), locale)); 

    criteriaQuery.select(questionRoot).where(predicates.toArray(new Predicate[]{})); 

    TypedQuery<Question> query = em.createQuery(criteriaQuery); 

    String queryString = query.unwrap(Query.class).getQueryString(); 

    return query.getResultList(); 
} 

我使用ListJoin因爲在元模型Question_.class我得到這一行:

public static volatile ListAttribute<Question, QuestionTranslation> questionTranslation; 

但這一個返回我問題類與列表QuestionTranslation兩個條目,其中語言字段等於ende值。但是我指定where子句只返回一個條目,其中語言等於en的值。我的代碼有什麼問題?

更新#1:

我有第二種情況。

還有一個表:

CREATE TABLE public.user_answer 
(
    uuid  VARCHAR(36) PRIMARY KEY 
    , user_uuid VARCHAR(36) REFERENCES public.users(uuid) NOT NULL 
    , question_id INT   REFERENCES public.question(id) NOT NULL 
    , answer  TEXT          NOT NULL 
); 

,我想做出這樣的SQL:

SELECT * FROM user_answer ua LEFT JOIN question q on ua.question_id = q.id LEFT JOIN question_translation qt ON q.id = qt.question_id WHERE qt.language = 'en' AND ua.user_uuid = '00000000-user-0000-0000-000000000001' AND q.month = '01' AND q.day = '01' AND q.year = '2016';

在Java中使用JPA規定 - 它看起來像這樣:

@Override 
public UserAnswer findByDayMonthYear(String day, String month, String year, User user, String locale) { 
    EntityManager em = sessionFactory.createEntityManager(); 
    CriteriaBuilder builder = em.getCriteriaBuilder(); 
    CriteriaQuery<UserAnswer> criteriaQuery = builder.createQuery(UserAnswer.class); 

    List<Predicate> predicates = new ArrayList<>(); 

    Root<UserAnswer> userAnswerRoot = criteriaQuery.from(UserAnswer.class); 
    Join<UserAnswer, Question> questionJoin = userAnswerRoot.join(UserAnswer_.question); 
    ListJoin<Question, QuestionTranslation> questionTranslatJoin = questionJoin.join(Question_.questionTranslation); 

    predicates.add(builder.equal(builder.treat(questionJoin, Question.class).get(Question_.day), day)); 
    predicates.add(builder.equal(builder.treat(questionJoin, Question.class).get(Question_.month), month)); 
    predicates.add(builder.equal(builder.treat(questionJoin, Question.class).get(Question_.year), year)); 
    predicates.add(builder.equal(builder.treat(questionTranslatJoin, QuestionTranslation.class).get(QuestionTranslation_.language), locale)); 
    predicates.add(builder.equal(userAnswerRoot.get(UserAnswer_.user), user)); 

    criteriaQuery.select(userAnswerRoot).where(predicates.toArray(new Predicate[]{})); 

    TypedQuery<UserAnswer> query = em.createQuery(criteriaQuery); 

    String queryString = query.unwrap(Query.class).getQueryString(); 

    return query.getSingleResult(); 
} 

在這種情況下,問題列出了包含兩個帶有languag的QuestionTranlsation項目es ende,但我只需要一個QuestionTranlsation條目,其語言等於en

我在這種情況下要做什麼?

回答

2

這是可能的JPA 2.1功能JOIN ON

How to do JOIN ON query using Criteria API

+0

看看我的問題更新,請。 – dikkini

+0

爲什麼我需要它?您可以通過翻譯訪問問題。 –

+0

我使用JPA 2.1,給我一個建議如何使用'JOIN ON語言'? – dikkini