Hql inner join The path expected to join! error

hi have the 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 } 

other

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

result and tables:

 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 do hql, as I did 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; 

which gives the following result:

 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 get this error

The path expected to connect! [select a.id, a.name, a.url from app.domain.FilesInfo the internal Connection of FilesShare b where a.id = b.fileid and b.userid = 5 and b.owner = 1]

How to make an inner join now, question

Thankyou

+1
source share
2 answers

Your query needs a path, you can refer to this question for more information: HQL ERROR: the path expected to join .

So something like this should solve 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 the FilesShare file:

 private FilesInfo filesInfo; @ManyToOne @JoinColumn(name = "id") public FilesInfo getFilesInfo() { return filesInfo; } public void setFilesInfo(FilesInfo filesInfo) { this.filesInfo = filesInfo; } 

and execute this query

 from FilesShare where userId = 5 and owner = 1 

It looks better and returns a list of shared files, each of which has a parent ( filesInfo ) field.

+2
source

Updated spring data version to 1.10.4.RELEASE, and this solved the problem.

0
source

Source: https://habr.com/ru/post/1478944/


All Articles