Andrey Sukhovitsky
Andrey Sukhovitsky

Reputation: 199

Using hibernate @Formula to fetch a collection

I have a similar db structure. The only difference that I have more tables on the path from A to C entity:

enter image description here

And I have following mapping for this structure:

@Entity
@Table(name = "a")
class A {
    @Id
    private int id;

    private String title;

    @ElementCollection(targetClass=String.class)
    @Formula("(select (c.useful_information) from A a " +
            "join B b on a.id = b.a_id " +
            "join C c on b.id = c.b_id " +
            "where a.id = id)")
    private List<String> usefulStuff;
}

My aim is to get a list of all useful things from table C in entity A.

But I get syntax errors.

Could you say what's wrong in my example? And maybe you know better way for this purpose?

Upvotes: 6

Views: 9018

Answers (1)

Robin
Robin

Reputation: 1736

The problem in your @Formula annotation is the "from A a". The value passed to this annotation is actually SQL, not JPQL.

Thus, if you want to alias the table to reference it elsewhere, you need to rather write FROM A AS a.

Upvotes: 3

Related Questions