gpt4 book ai didi

java - 如何进行正确的 HQL 连接

转载 作者:行者123 更新时间:2023-12-01 11:48:40 24 4
gpt4 key购买 nike

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 出了什么问题有任何线索吗?

谢谢

何塞

最佳答案

order 是保留的 JPQL 和 SQL 关键字。更改您的实体的名称。

你的语法也是错误的。 HQL 中的连接使用实体之间的关联。正确的语法例如是

select ticket from Trade trade
join trade.ticket ticket

关于java - 如何进行正确的 HQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28951340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com