我正在尝试使用 HQL 从数据库中发出选择
这是代码:
public Collection<Link> getAllLinksForDevice(Device device) {
Session session = sessionFactory.getCurrentSession();
String hql = "SELECT Link as l WHERE l.device1 = :deviceId OR l.device2 = :deviceId";
Query query = session.createQuery(hql); <-- Fails here!!
query.setParameter("deviceId",device.getId());
Collection<Link> linkList = query.list();
return linkList;
}
和链接实体:
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name="device1_id")
private Device device1;
@ManyToOne
@JoinColumn(name="device2_id")
private Device device2;
设备实体:
@Entity
@Table(name = "device")
public class Device {
@Id
@Column(name = "id")
@GeneratedValue
private int id;
@Column(name = "dns_Name")
private String dnsName;
@Column(name = "ip_address_v4")
private String ipV4;
@Column(name = "device_type")
private int deviceType;
@ManyToOne
@JoinColumn(name="area_id")
private Area area;
但我得到一个例外:
15:10:55,570 DEBUG ErrorCounter:51 - :0:0:子树的意外结束:0:0:org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3117)的子树意外结束。 hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720) 在 org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:571) 在 org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker. java:288) 在 org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254) 在 org.hibernate.hql.ast 的 org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231) .QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
和:
15:10:55,622 错误解析器:56 - 无效路径:'l.device1' 15:10:55,628 调试错误计数器:51 - 无效路径:'l.device1' 无效路径:org.hibernate.hql 中的'l.device1' .ast.util.LiteralProcessor.lookupConstant(LiteralProcessor.java:135) 在 org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:216) 在 org.hibernate.hql.ast.tree.FromReferenceNode.resolve (FromReferenceNode.java:117) 在 org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113) 在 org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:854) 在 org. hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1293) 在 org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4243) 在 org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.爪哇:3722)