我有以下实体类
public class Customer {
@Id
private int id;
@Column(name = "CID")
private String cid;
}
public class Alccs {
@Id
private int vid;
// @Column(name = "CID")
// private String cid;
@ManyToOne
@JoinColumn(name = "CID", referencedColumnName = "CID")
private Customer customer;
}
public class Rtwo {
@Id
private int jobNo;
@ManyToOne
@JoinColumn(name = "CID", referencedColumnName = "CID")
private Customer customer;
}
public class RTwoCycle {
@Id
private int cycleJobNo;
@Column
private int vid;
@ManyToOne
@JoinColumn(name = "JobNo", referencedColumnName = "JobNo")
private TblRtwo tblRtwo;
}
如何使用HQL
和criteria
编写类似以下的查询
select top 20 * from RTwoCycle rtCycle join Alccs al on rtCycle.vid = al.vid;
对于HQL,我尝试关注
from TBLRTwoCycle rtCycle join TBLAL al on rtCycle.vid = al.vid
但出现QuerySyntaxException: Path expected for join!
错误
关于标准,我不知道怎么做
该怎么做?
答案 0 :(得分:0)
早于5.1的JPA和Hibernate版本需要定义的关系才能在JPQL查询中连接两个实体。这通常会导致级联JOIN语句以遍历实体之间的关系图,或者导致如果实体之间没有关系,则无法使用JPQL进行实体JOIN的语句。
Hibernate 5.1引入了对不相关实体的显式联接。语法与SQL非常相似,可让您在ON语句中定义JOIN条件。
答案 1 :(得分:0)
from RTwoCycle as rtCycle inner join Alccs as al with rtCycle.vid = al.vid
答案 2 :(得分:0)
对于HQL
的简单连接,on clause
就足够了
from table1 as t1 inner join table2 as t2 on t1.vid=t2.vid
或
from table1 as t1, table2 as t2 where t1.vid=t2.vid
标准Restriction.sqlRestriction
是此问题的解决方案
session.createCriteria(Class1,"c1")
.createAlias("class2", "c2")
.add(Restrictions.sqlRestriction("c1.vid=c2.vid")