简体   繁体   中英

Querydsl entity inheritance query with join

I have three entities. User(parent)

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "user_type")
@Table(name = "userinfo")
@SequenceGenerator(name = "userInfoUserIdSeq", initialValue = 1, allocationSize = 100, sequenceName = "userinfo_user_id_seq")
public abstract class UserInfo {

public static final String EDITOR = "E";
public static final String TALENT = "T";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userInfoUserIdSeq")
@Column(name = "user_id")
private Long id;
...

Editor(child)

@Entity
@DiscriminatorValue(UserInfo.EDITOR)
@Table(name = "editors")
public class Editor extends UserInfo { ....

Journalist(child)

@Entity
@DiscriminatorValue(UserInfo.TALENT)
@Table(name = "talent")
public class Talent extends UserInfo { .....

I have the following query that isn't working, written on querydsl

    QUserInfo userInfo = QUserInfo.userInfo;
    text = "%"  + text + "%";
    QTalent talent = QTalent.talent;
    QEditor editor = QEditor.editor;
    SearchResults<UserInfo> results = query.from(userInfo).leftJoin(userInfo, talent._super)
            .leftJoin(userInfo, editor._super).where( .....

I've got the following stacktrace:

     at            com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:127)
    at com.mysema.query.jpa.impl.AbstractJPAQuery.listResults(AbstractJPAQuery.java:261)
    at com.washpost.talent.dao.implementation.UserInfoDaoImpl.findAllUsersByNamesAndEmails(UserInfoDaoImpl.java:33)
    ... 113 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select count(userInfo)
from com.washpost.talent.model.UserInfo userInfo
  left join treat(userInfo as Talent) as talent
  left join treat(userInfo as Editor) as editor
where ....

I can not understand why my joins is not working

Cannot say without seeing the query in whole.

But normally you would not join on the inheritance tables in JPA. If you want all Users (including journalists, editors...) you just run Select U from User u .

If you need only journalists you query: select u From Journalist U ;

JPA will translate it to the joins when needed (so that you could change your inheritance type to single table, and your queries would still work)

In JPQL you can join over properties, but not discriminator tables, like you try to do. But it would be helpful if you could describe what info you need.

It is not necessary to make the left join. As you've already done the mapping in the class "@Inheritance (strategy = InheritanceType.JOINED)". So when you make a query, the join will automatically be done using this way:

        UserInfo userInfo = new UserInfo ("userInfo");
        QTalent talent = userInfo.as(QTalent.class);
        QEditor editor = userInfo.as(QEditor.class);

Documentation about Inheritance

References: Simple Example QueryDsl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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