1

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

Aadam
  • 1,521
  • 9
  • 30
  • 60
  • fileid is the id column from the filesinfo table its a foreign key. i think its clear from the entityclasses i mean the above given – Aadam Apr 27 '13 at 08:11

2 Answers2

3

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.

Community
  • 1
  • 1
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95
  • can you please write complete query i am getting confused. please atleast sample using inner join – Aadam Apr 27 '13 at 08:16
  • updated answer with what seems to me the only way to do what you want – Oleg Mikheev Apr 27 '13 at 10:13
  • what is wrong in this 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 – Aadam Apr 27 '13 at 15:22
0

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

Tosalisu
  • 1
  • 1