hql内部联接加入的路径!错误 [英] hql inner join Path expected for join! error
问题描述
您好有以下实体:
@Entity
公共类FilesInfo {
@Id
@GeneratedValue
私人整数ID;
私人字符串名称;
私人字符串网址;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name =fileId)
private Collection< FilesShare> filesShared = new ArrayList< FilesShare>();
公共集合< 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;
+ -------- + --------- + ------ + ----- + --------- + --- ------------- +
|字段|类型|空| Key |默认|额外|
+ -------- + --------- + ------ + ----- + --------- + --- ------------- +
| id | int(11)| NO | PRI | NULL | auto_increment |
| userId | int(11)| NO | | NULL | |
|所有者| int(11)| NO | | NULL | |
| fileId | int(11)|是| MUL | NULL | |
+ -------- + --------- + ------ + ----- + --------- + --- ------------- +
4行设置(0.01秒)
mysql> desc filesinfo;
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
|字段|类型|空| Key |默认|额外|
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
| id | int(11)| NO | PRI | NULL | auto_increment |
|名称| varchar(255)|是| | NULL | |
| url | varchar(255)|是| | NULL | |
+ ------- + -------------- + ------ + ----- + --------- + ---------------- +
3行(0.00秒)
我想让hql成为下面的sql:
select a.id ,a.name,来自FilesInfo的a.url内部连接FilesShare b
对于a.id = b.fileid其中b.userid = 5和b.owner = 1;
给出以下输出:
的MySQL>从FilesInfo中选择a.id,a.name,a.url,在内部加入FilesShare b。
id = b.fileid其中b.userid = 5和b.owner = 1;
+ ---- + ------------------- + -------------------- ----------------- +
| id |名称| 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:
从文件信息中选择a.id,a.name,a.url一个内部加入FilesShare b
,其中a.id = b.fileid和b.userid = 5和b.owner = 1
但我收到此错误
加入的路径! [选择a.id,a.name,a.url从
app.domain.FilesInfo内部连接FilesShare b,其中a.id = b.fileid和
b.userid = 5和b.owner = 1]
现在如何做内连接,是问题
thankyou
您的查询需要路径,您可以参考此问题获取详细信息:HQL错误:预计加入的路径。
所以,像这样的东西应该可以解决它:
从FilesInfo中选择a.id,a.name,a.url一个内部联接a。 filesShared b where b.userid = 5 and b.owner = 1;
但是我只需要在您的ManyToOne映射FilesShare:
private FilesInfo filesInfo;
@ManyToOne
@JoinColumn(name =id)
public FilesInfo getFilesInfo(){
return filesInfo;
}
public void setFilesInfo(FilesInfo filesInfo){
this.filesInfo = filesInfo;
}
并执行此查询
来自FilesShare的
,其中userId = 5且拥有者= 1
它看起来更好,并返回文件共享列表,每个文件共享都拥有父项( filesInfo
)字段。
hi have following entities;
@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
}
the other one
@Entity
public class FilesShare {
private Integer id;
private int userId;
private int owner;
}
the resultand tables are:
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)
i want to make the hql as i made the sql below:
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;
that gives the following output:
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 |
+----+-------------------+-------------------------------------+
i tried the following 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
but i am getting this error
Path expected for join! [select a.id, a.name, a.url from app.domain.FilesInfo a inner join FilesShare b where a.id=b.fileid and b.userid=5 and b.owner=1]
how to do inner join now, is the question
thankyou
Your query requires path, you can refer to this question for details: HQL ERROR: Path expected for join.
So, something like this should resolve it:
select a.id, a.name, a.url from FilesInfo a inner join a.filesShared b where b.userid=5 and b.owner=1;
But I would just have a ManyToOne mapping in your FilesShare:
private FilesInfo filesInfo;
@ManyToOne
@JoinColumn(name = "id")
public FilesInfo getFilesInfo() {
return filesInfo;
}
public void setFilesInfo(FilesInfo filesInfo) {
this.filesInfo = filesInfo;
}
and do this query
from FilesShare where userId = 5 and owner = 1
It looks nicer and returns the list of file shares, each of them having parent (filesInfo
) field populated.
这篇关于hql内部联接加入的路径!错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!