2

This is how my domain looks:

public class Template implements Serializable {
    private static final long serialVersionUID = 1L;    
    @OneToOne(cascade=CascadeType.ALL)
    private FieldConfig fieldConfig;
}

public class FieldConfig implements Serializable {
    private static final long serialVersionUID = 1L;

    @OneToMany(cascade= CascadeType.PERSIST)
    @JoinColumn(name = "fieldConfigId")
    private Set<Field> fieldSet;
}

I want to achieve if I load a template from the db that automatically the fieldConfig is loaded and the fieldSet of that fieldconfig.

my current JPQL:

TypedQuery<Template> query = em.createQuery("SELECT t from Template t LEFT JOIN FETCH t.fieldConfig"
                + " fconfig LEFT JOIN FETCH fconfig.fieldSet where t.id = :id", Template.class);

my exception:

Internal Exception: NoViableAltException(80@[()* loopback of 477:9: (node= join )*])
Exception Description: Syntax error parsing the query [SELECT t from Template t LEFT JOIN FETCH t.fieldConfig fconfig LEFT JOIN FETCH fconfig.fieldSet where t.id = :id], line 1, column 55: unexpected token [fconfig].

Any thoughts on creating such a query?

7
  • Are @Template and @FieldConfig annotated as entities or configures so in the descriptor? Commented Nov 19, 2011 at 16:05
  • yes ofcourse, I didn't include that for readability :) Commented Nov 19, 2011 at 16:06
  • The query looks correct to me. What's your JPA engine? Have you tried using AS fconfig to see if it makes a difference? Commented Nov 19, 2011 at 16:14
  • I'm using EclipseLink, tried adding AS got => Exception Description: Syntax error parsing the query [SELECT t from Template t LEFT JOIN FETCH t.fieldConfig AS fconfig LEFT JOIN FETCH fconfig.fieldSet where t.id = :id], line 1, column 55: unexpected token [AS]. Commented Nov 19, 2011 at 16:19
  • 2
    See stackoverflow.com/questions/2319913/… Commented Nov 19, 2011 at 16:41

1 Answer 1

3

You cannot use an alias on a join fetch in JPQL, this is disallowed by the spec.

EclipseLink does allow nested join fetch through the query hint,

"eclipselink.join-fetch"="t.fieldConfig.fieldSet"
Sign up to request clarification or add additional context in comments.

1 Comment

TypedQuery<Template> query = em.createQuery("SELECT t from Template t JOIN FETCH t.fieldConfig" + " JOIN FETCH t.theme" + " JOIN FETCH t.dataConfig" + " JOIN t.fieldConfig.fieldSet fs" + " JOIN t.dataConfig.textSet ts where t.id = :id", Template.class); query.setParameter("id", id); query.setHint(QueryHints.FETCH, "t.fieldConfig.fieldSet"); query.setHint(QueryHints.FETCH, "t.dataConfig.textSet"); return query.getSingleResult();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.