休眠条件搜索多个实体

发布于 2024-12-14 08:39:47 字数 726 浏览 0 评论 0原文

我有三个实体:日志、员工和代理机构

日志,其中与员工

有多对一关联。 员工与代理机构有多对一关联

DetachedCriteria criteria = DetachedCriteria.forClass(Log.class);

if (StringUtils.isNotBlank(c.getName())) {
      criteria.createAlias("employee", "e").add(Restrictions.ilike("e.name", "%" + c.getName() + "%"));
    }
if (StringUtils.isNotBlank(c.getAgency())) {
      criteria.createAlias("employee", "e").createAlias("agency", "a").add(Restrictions.ilike("e.a.name", "%" + c.getAgency() + "%"));
        }

。 以上适用于员工名称,但不适用于代理机构名称。

我收到以下错误:

Exception: org.hibernate.QueryException: could not resolve property: agency of: com.example.Log

我找到的所有示例仅向下一级。有人可以向我指出一个资源,告诉我如何向下导航吗?

I have three entities: Log, Employee, and Agency

Log with a many to one association to Employee

Employee with a many to one association to Agency

DetachedCriteria criteria = DetachedCriteria.forClass(Log.class);

if (StringUtils.isNotBlank(c.getName())) {
      criteria.createAlias("employee", "e").add(Restrictions.ilike("e.name", "%" + c.getName() + "%"));
    }
if (StringUtils.isNotBlank(c.getAgency())) {
      criteria.createAlias("employee", "e").createAlias("agency", "a").add(Restrictions.ilike("e.a.name", "%" + c.getAgency() + "%"));
        }

The above works for Employee name, but it doesn't work for Agency name.

I get the below error:

Exception: org.hibernate.QueryException: could not resolve property: agency of: com.example.Log

All the examples I find only go one level down. Can someone point me to a resource that tells me how to navigate lower?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-12-21 08:39:47

尝试下面的代码

DetachedCriteria criteria = DetachedCriteria.forClass(Log.class);
criteria.createAlias("employee", "e");

if (StringUtils.isNotBlank(c.getName())) {
    criteria.add(Restrictions.ilike("e.name", c.getName(), MatchMode.ANYWHERE));
}

if (StringUtils.isNotBlank(c.getAgency())) {
    criteria.createAlias("e.agency", "a"); //  <===========
    criteria.add(Restrictions.ilike("a.name", c.getAgency(), MatchMode.ANYWHERE));
}

Try the following code

DetachedCriteria criteria = DetachedCriteria.forClass(Log.class);
criteria.createAlias("employee", "e");

if (StringUtils.isNotBlank(c.getName())) {
    criteria.add(Restrictions.ilike("e.name", c.getName(), MatchMode.ANYWHERE));
}

if (StringUtils.isNotBlank(c.getAgency())) {
    criteria.createAlias("e.agency", "a"); //  <===========
    criteria.add(Restrictions.ilike("a.name", c.getAgency(), MatchMode.ANYWHERE));
}
孤独患者 2024-12-21 08:39:47

第二个应该像这样工作:

criteria.createAlias("employee", "e").createAlias("e.agency","a")
           .add(Restrictions.ilike("a.name", "%" + c.getAgency() + "%"));

The second one should work like this:

criteria.createAlias("employee", "e").createAlias("e.agency","a")
           .add(Restrictions.ilike("a.name", "%" + c.getAgency() + "%"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文