简体   繁体   English

带连接的Querydsl实体继承查询

[英]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 我有以下查询不起作用,写在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. 但是通常您不会加入JPA中的继承表。 If you want all Users (including journalists, editors...) you just run Select U from User u . 如果要所有用户(包括记者,编辑...),只需运行“ Select U from User u

If you need only journalists you query: select u From Journalist U ; 如果只需要新闻工作者,则可以查询: 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) JPA将在需要时将其转换为联接(以便您可以将继承类型更改为单个表,并且查询仍然可以使用)

In JPQL you can join over properties, but not discriminator tables, like you try to do. 在JPQL中,您可以像尝试那样连接属性,但不能连接鉴别表。 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)". 正如您已经完成了类“ @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 参考:简单示例QueryDsl

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

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