0

我有以下三个类的关系:

   @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;
    }

一个用户可以拥有 m 种类型的 n 个属性。

我需要创建 HQL 查询,它将返回List<AttributeType>特定用户属性的所有属性类型。

例如,用户具有类型 t 的属性 a、类型 t 的属性 b 和类型 t1 的属性 c。我需要返回List<AttributeType>包含 t 和 t1 的内容。

请帮忙。我刚刚迷失在这个查询中。

4

1 回答 1

1

您应将 Attribute 映射到 User 多对一关系,因此您需要以下查询:

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
于 2012-08-29T22:59:09.827 回答