java - 如何正确编写 HQL

标签 java hibernate hql

您好,我正在尝试将此写到哪里并获取查询,但出现错误

 Query query = session.createQuery("from Device d where d.customerRefId=:customerId " +
                    "left join fetch d.tradeInOldDevice ");                    
  query.setParameter("customerId", "myId");  
 Set<Device> customerDevices= (Set<Device>) query.list();

错误:

2013-09-24 12:18:45,753 ERROR PARSER:56 - line 1:98: unexpected token: left

这就是设备实体:

@Entity
@Table(name = "Devices")
public class Device implements Serializable, IHidingDeletable {

...

  @ManyToOne(optional = true,fetch=FetchType.LAZY)
    private Customer owningCompany

 @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "tradeInOldDevice")
    @Cascade(CascadeType.ALL)
    private Device tradeInOldDevice;

..

那是客户:

public class Customer extends AbstractCustomer {...}


@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "Customers")
@DiscriminatorColumn(name = "type")
public abstract class AbstractCustomer implements IVersionized, Serializable {

   @Id
    protected String customerRefId;

有什么想法吗?

最佳答案

join 应该在 where 子句之前:

    Query query = session.createQuery("from Device d left join " + 
    "fetch d.tradeInOldDevice od where d.customer.customerRefId=:customerId");

    query.setParameter("customerId", "myId");
    Set<Device> customerDevices= (Set<Device>) query.list();  

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

相关文章:

java - 使用复选框更改应用程序主题(使用java)

java - 插入时出现 Hibernate 异常

database - 正确关闭 Hibernate 事务

java - 映射异常 : No Dialect mapping for JDBC type: 2002

java - HQL:如何通过id查找子集合中的项目

java - 使用带参数的 HQL 选择表行

java - 为什么此正则表达式中的后视表达式没有 "obvious maximum length"?

java - AnimationTimer stop()方法不停止

java - em.merge 不生成更新语句

java - spring部署在Tomcat中时是否需要JTA TX管理器