【问题标题】:HQL query with several joins具有多个连接的 HQL 查询
【发布时间】:2012-08-24 15:10:35
【问题描述】:

我有以下三个类的关系:

   @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&lt;AttributeType&gt;。

例如用户具有类型t的属性a,类型t的属性b和类型t1的属性c。 我需要返回包含 t 和 t1 的 List&lt;AttributeType&gt;。

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

【问题讨论】:

    标签: java hibernate jakarta-ee hql


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      相关资源
      最近更新 更多