2013-09-24 55 views
1

您好我想寫這個地方並獲取查詢和我得到一個錯誤如何編寫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; 

任何想法?

+0

我想你已經留下了一些文本,同時張貼在這裏的代碼。 –

+0

謝謝。我修好了它。 – rayman

回答

3

的加入應該來的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(); 
+0

然後我得到不同的錯誤:2013-09-24 12:26:41,850錯誤PARSER:56 - 加入的路徑! – rayman

+0

@rayman在'Device'和'TradeInOldDevice'實體之間的映射中是否建立了關聯? –

+0

你問我是否在Device的customerRefId裏面?如果多數民衆贊成你所要求的比沒有..我得到了DB設備表內的customerRefId – rayman