hql inner join要加入的路径!错误

时间:2013-04-27 07:51:59

标签: hibernate jpa jpa-2.0 hibernate-mapping

您有以下实体;

@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;
}

结果表是:

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]

现在如何做内连接,是问题

三江源

2 个答案:

答案 0 :(得分:3)

您的查询需要路径,有关详细信息,请参阅此问题:HQL ERROR: Path expected for join

所以,这样的事情应该解决它:

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

但我会在你的FilesShare中有一个ManyToOne映射:

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)字段。

答案 1 :(得分:0)

将弹簧数据版本更新为1.10.4.RELEASE,这解决了这个问题。