HQL内连接预期联接路径! 错误(hql inner join Path expected fo

2019-09-27 17:11发布

喜有以下实体;

@Entity
public class FilesInfo {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String url;

    @OneToMany(cascade= CascadeType.ALL)
    @JoinColumn(name="fileId")
    private Collection<FilesShare> filesShared = new ArrayList<FilesShare>();

    public Collection<FilesShare> getFilesShared() {
        return filesShared;
    }

    public void setFilesShared(Collection<FilesShare> filesShared) {
        this.filesShared = filesShared;
    }
 //getters & setters   
}

另一个

@Entity
public class FilesShare {
    private Integer id;
    private int userId;
    private int owner;
}

在resultand表是:

mysql> desc filesshare;
+--------+---------+------+-----+---------+----------------+
| Field  | Type    | Null | Key | Default | Extra          |
+--------+---------+------+-----+---------+----------------+
| id     | int(11) | NO   | PRI | NULL    | auto_increment |
| userId | int(11) | NO   |     | NULL    |                |
| owner  | int(11) | NO   |     | NULL    |                |
| fileId | int(11) | YES  | MUL | NULL    |                |
+--------+---------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> desc filesinfo;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
| url   | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

我想使HQL为我做了下面的SQL:

    select a.id, a.name, a.url from FilesInfo a inner join FilesShare b 
on a.id=b.fileid where b.userid=5 and b.owner=1;

这提供了以下的输出:

mysql> select a.id, a.name, a.url from FilesInfo a inner join FilesShare b on a.
id=b.fileid where b.userid=5 and b.owner=1;
+----+-------------------+-------------------------------------+
| id | name              | url                                 |
+----+-------------------+-------------------------------------+
|  1 | dwnld_btn_1.png   | C:\shareapp\admin\dwnld_btn_1.png   |
|  2 | dwnld_btn_1.png   | C:\shareapp\admin\dwnld_btn_1.png   |
|  3 | dwnld_btn_1.png   | C:\shareapp\admin\dwnld_btn_1.png   |
|  4 | dwnld_btn_1_1.png | C:\shareapp\admin\dwnld_btn_1_1.png |
+----+-------------------+-------------------------------------+

我尝试以下HQL:

select a.id, a.name, a.url from FilesInfo a inner join FilesShare b 
where a.id=b.fileid and b.userid=5 and b.owner=1

但我得到这个错误

路径预期的加入! [选择a.id,a.name,a.url从app.domain.FilesInfo一个内连接FilesShare b其中a.id = b.fileid和b.userid = 5和b.owner = 1]

怎么办,现在内部联接,是个问题

谢谢

Answer 1:

查询需要的路径,你可以参考这个问题的详细介绍: HQL错误:预期的联接路径 。

所以,这样的事情应该解决这个问题:

select a.id, a.name, a.url from FilesInfo a inner join a.filesShared b where b.userid=5 and b.owner=1;

但我只想在你FilesShare一个多对一映射:

private FilesInfo filesInfo;

@ManyToOne
@JoinColumn(name = "id")
public FilesInfo getFilesInfo() {
    return filesInfo;
}

public void setFilesInfo(FilesInfo filesInfo) {
    this.filesInfo = filesInfo;
}

而做到这一点查询

from FilesShare where userId = 5 and owner = 1

它看起来更好,并返回的文件共享列表,其中每一个具有父( filesInfo填充)字段。



Answer 2:

春季更新数据版本1.10.4.RELEASE,这解决了这个问题。



文章来源: hql inner join Path expected for join! error