我有以下三个类的关系:
@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。
请帮忙。我刚刚迷失在这个查询中。
最佳答案
您应将属性映射到用户多对一关系,因此您需要以下查询:
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
关于java - 具有多个连接的 HQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187574/