查询实体实例与 GORM 的 M:N 关系_不_相互关联?

发布于 2024-12-02 18:48:41 字数 1105 浏览 1 评论 0原文

我需要跟踪与属性的 M:N 关系,因此我使用链接表(遵循 没有 Hibernate XML 的多对多映射) ...但是,我不知道如何查询该会员资格合法但尚不存在的关系,例如,不在给定团队中的任何员工(或用户尚未出价的项目等)。我正在 HQL 中研究它,但我对此很菜鸟,所以我可以使用一些关于什么技术最有效的指导......或者,此类查询的示例;)

为了讨论,只需假设员工:团队会员级别,每个级别都有一个非常大的集合(太大而无法直接拉入中间层并进行集合操作)。

class Membership {
    Employee employee
    Team team
    String other // I need attributes on the relationship
}

class Employee {
    Date dateJoinedCompany
    String name
    static hasMany = [managedTeams:Team, memberships:Membership]
    static mappedBy = [managedTeams:"manager"]
}

class Team {
    String name
    Employee manager
    static belongsTo = Employee
    static hasMany = [memberships:Membership]
}

因此,我需要一个查询,该查询返回一个多月前不属于团队 #2 的员工,或者员工 #5 不属于的团队,诸如此类的事情什么是最好的技术 - 有没有办法做这与标准?或者,关于如何最好地使用 HQL 有什么建议吗?

我应该添加我当前的想法,使用 HQL 和子选择:

from Team t where t not in (select m.team from Membership m where m.employee = 5)

TIA!

I need to track a M:N relationship with attributes, so I'm using a link table (following the pattern at Many-to-Many Mapping without Hibernate XML) ... but, I can't see how to query that Membership relationship for legal-but-don't-yet-exist relationships, e.g., any Employees not in a given Team (or Items which a User hasn't yet bid on, etc). I'm working on it in HQL, but I'm a noob to that so I could use some guidance on what technique works best ... or, examples of this sort of query ;)

For discussion, just assume the Employees:Team Membership class, with a very large set of each (too large to just pull into the mid-tier and do set operations).

class Membership {
    Employee employee
    Team team
    String other // I need attributes on the relationship
}

class Employee {
    Date dateJoinedCompany
    String name
    static hasMany = [managedTeams:Team, memberships:Membership]
    static mappedBy = [managedTeams:"manager"]
}

class Team {
    String name
    Employee manager
    static belongsTo = Employee
    static hasMany = [memberships:Membership]
}

So, I need a query which returns Employees not on Team #2 who jined the company more than a month ago, or Teams which Employee #5 is not part of, that sort of thing What's the best technique - is there a way to do this with Criteria? Or, any suggestions on how to best use HQL for it?

I should add my current thought, using HQL and a subselect:

from Team t where t not in (select m.team from Membership m where m.employee = 5)

TIA!

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

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

发布评论

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

评论(1

≈。彩虹 2024-12-09 18:48:41

一个多月前不属于团队 #2 的员工:

Employee.executeQuery("select e from Employee as e inner join e.memberships as m where m.team.id != :tId and e.dateJoinedCompany > :date", [tId: 2, date: new Date() - 60]
//calculate the exact date. instead of using the team id you can use the team instance

员工 #5 不属于的团队

Team.exccuteQuery("select t from Team as t inner join t.memberships as m where m.e.id != :eId",[eId: 5])

Employees not on Team #2 who jined the company more than a month ago:

Employee.executeQuery("select e from Employee as e inner join e.memberships as m where m.team.id != :tId and e.dateJoinedCompany > :date", [tId: 2, date: new Date() - 60]
//calculate the exact date. instead of using the team id you can use the team instance

Teams which Employee #5 is not part of

Team.exccuteQuery("select t from Team as t inner join t.memberships as m where m.e.id != :eId",[eId: 5])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文