2012-08-29 47 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