【问题标题】:HQL query for Association关联的 HQL 查询
【发布时间】:2016-05-16 06:59:23
【问题描述】:

我在这里有下表,但在获取结果时遇到了一些问题。

@Entity
@Table(name = "USER_VW")
public class WorkspaceUserImpl
{
    @JoinColumn(name = "USER_ID", insertable=false, updatable=false)
    @OneToOne(targetEntity = UserImpl.class, fetch = FetchType.EAGER)
    private User user;

}

@Table(name = "IK_USER")
@Inheritance(strategy = InheritanceType.JOINED)
@AttributeOverride(name = "id", column = @Column(name = "USER_ID") )


    public class UserImpl extends BaseAuditable<UserIdentifier>implements User, UserAuthentication {

        private static Logger log = LoggerFactory.getLogger(UserImpl.class);

        @Id
        @Type(type = "com.commons.UserIdentifierTypeMapper")
        @Column(name = "USER_ID")
        private UserIdentifier id;
    }

和用户

Public Inteface User
{
      UserIdentifier getId();
}

现在我已经编写了一个 HQL 查询来从 WorkspaceUserImpl 类中获取所有数据,其中 UserImpl 类具有给定的用户 ID,如下所示。

SELECT w from WorkspaceUserImpl w where w.user.id = : user_id;

也试过了

SELECT w from WorkspaceUserImpl as w INNER JOIN w.user as u where u.id = : user_id;

甚至还尝试使用 JOIN FETCH

并将参数 user_id 设置为 1234。

但我将 List 作为特定 ID 的 emply,但在 DB 中它有 5 条记录。

我在这里犯了任何查询错误吗?好心的建议..

【问题讨论】:

  • 我相信你需要一个名为User的类,Hibernate可以加入WorkspaceUserImpl。实现User 是不够的。
  • 由于它是一个旧代码,我不允许在层次结构中进行任何修改......我的 HQL 怎么样?我做的对吗?

标签: hql


【解决方案1】:

您是否尝试过以下查询:

from WorkspaceUserImpl as w JOIN FETCH w.user as u where u.id = : user_id;

【讨论】:

    猜你喜欢
    • 2012-09-06
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多