如何进行正确的 HQL 连接
How to do a proper HQL join
嗨,Whosebug 社区,
我在处理 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 有什么问题的任何线索?
谢谢,
何塞
order
是保留的 JPQL 和 SQL 关键字。更改您的实体名称。
你的语法也不对。加入 HQL 使用实体之间的关联。正确的语法是例如
select ticket from Trade trade
join trade.ticket ticket
Order
是保留字;尝试使用其他东西。
这些人是正确的,Order
已保留,但不要更改您的实体名称,请尝试在 table 的定义中使用反引号。
@Entity
@Table(name="`Order`")
public class Order {`
来源:
Hibernate Jira
嗨,Whosebug 社区,
我在处理 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 有什么问题的任何线索?
谢谢,
何塞
order
是保留的 JPQL 和 SQL 关键字。更改您的实体名称。
你的语法也不对。加入 HQL 使用实体之间的关联。正确的语法是例如
select ticket from Trade trade
join trade.ticket ticket
Order
是保留字;尝试使用其他东西。
这些人是正确的,Order
已保留,但不要更改您的实体名称,请尝试在 table 的定义中使用反引号。
@Entity
@Table(name="`Order`")
public class Order {`
来源: Hibernate Jira