2012-08-29 48 views
0

我有三個班以下關係:HQL查詢與若干個連接

@Entity 
    public class User{ 

     @OnetoMany 
     List<Attribute> attributes = new ArrayList<Attribute>(); 
    } 

    @Entity 
    public class Attribute{ 
     @ManyToOne 
     AttributeType attributeType; 

    } 

    @Entity 
    public class AttributeType{ 

     @Column 
     String type; 
    } 

一個用戶可以有n×m個類型的屬性。

我需要創建HQL查詢,它將返回特定用戶屬性的所有屬性類型List<AttributeType>

例如,用戶具有類型t的屬性a,類型t的屬性b和類型t1的屬性c。 我需要返回List<AttributeType>,它將包含t和t1。

請幫忙。我只是迷失在這個查詢中。

回答

1

您應映射屬性到用戶多對一的關係,因此下面的查詢是你所需要的:

select distinct atr.attributeType 
    from Attribute atr 
where atr.user = :user 

我覺得下面的查詢將工作太:

select distinct atrs.attributeType 
    from User as user 
    join user.attributes as atrs 
where user.id = :user_id