您好StackOverflow社区,
我遇到HQL查询问题。对于财务应用程序,我们希望基于给定的StrategyInstrument实体获得TradeTicket实体。以下是已定义的实体(省略详细信息,仅显示关系)。
@Entity
public class Trade {
@ManyToOne
private Ticket ticket;
@ManyToOne
private Order buyOrder;
@ManyToOne
private Order sellOrder;
...
}
@Entity
public class TradeTicket {
private Date modifiedDateTime;
...
}
@Entity
public class Order {
@OneToOne
private StrategyInstrument strategyInstrument;
...
}
@Entity
public class StrategyInstrument {
...
}
我尝试以下查询:
String queryString = "select ticket from TradeTicket ticket"
+ " join Trade trade"
+ " join Order order"
+ " where trade.tradeTicket = ticket"
+ " and (trade.buyOrder = order or trade.sellOrder = order)"
+ " and order.strategyInstrument = :strategyInstrument"
+ " and ticket.clearingApproval.approved = true"
+ " order by ticket.modifiedDateTime";
List<TradeTicket> results = getSession()
.createQuery(queryString)
.setParameter("strategyInstrument", strategyInstrument)
.list();
导致以下异常:
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: Order near line 1, column 102 [select ticket from com.mycompany.model.transactional.TradeTicket ticket join Trade trade join Order order where trade.tradeTicket = ticket and (trade.buyOrder = order or trade.sellOrder = order) and order.strategyInstrument = :strategyInstrument and ticket.clearingApproval.approved = true order by ticket]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
关于我的HQL有什么问题的任何线索?
谢谢,
何
答案 0 :(得分:1)
order
是保留的JPQL和SQL关键字。更改实体的名称。
你的语法也错了。加入HQL使用实体之间的关联。正确的语法例如是
select ticket from Trade trade
join trade.ticket ticket
答案 1 :(得分:0)
Order
是保留字;尝试使用别的东西。
答案 2 :(得分:0)
这些人是正确的,Order
是保留的,但不要更改您的实体名称尝试使用该表定义中的反引号。
@Entity
@Table(name="`Order`")
public class Order {`
来源: Hibernate Jira