2016-01-13 117 views
1

這是一個Client實例,其中idOrder實例爲clientId。有沒有辦法執行左連接OrderClient而不添加任何字段Order? 結果SQL建議立即進行刪除是這樣的:如何通過id創建hibernate關聯?

Select * from Client left join Order ON Order.clientId = Client.id 

注意Order.clientId可以爲空。冬眠有沒有技巧?

回答

1

我建議您從客戶端添加一對多關係到訂單。

public class Client{ 

    @OneToMany 
    private List<Order> orders = new ArrayList<Order>(); 

    public List<Order> getOrders() { 
     return orders; 
    } 
    public void setOrder(List<Order> orders){ 
     this.orders = orders; 
    } 
} 

那麼做到這一點:

EntityManager em = factory.createEntityManager(); 
Query query = em.createQuery("SELECT x FROM Client left join FETCH x.orders"); 
List<Client> clients = query.getResultList();