2011-09-01 142 views
0

我需要跟踪A M:带属性N的关系,所以我使用一个链接表(以下在Many-to-Many Mapping without Hibernate XML模式)...但是,我看不出如何查询法律 - 丁 - 即会员关系还不存在的关系,例如给定团队(或用户尚未出价的项目等)中的任何员工而不是。我正在它HQL,但我是一个小白到,所以我可以使用一些指导什么技术效果最好...或者,这种查询的例子;)与GORM查询M:N关系的实体实例_not_彼此之间的关系?

进行讨论,只是假设Employees:Team Membership类,每个类都有非常大的集合(太大而不能拉入中间层并执行集合操作)。

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谁在一个月前jined公司不止,或团队,其5号员工是不是的一部分,那种事有什么最好的方法 - 是否有一种方法与标准做到这一点?或者,有关如何最好地使用HQL的建议?

我要补充我目前的想法,使用HQL和子查询:

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

TIA!

回答

0

员工不能在队#2谁jined公司一个多月前更多:

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]) 
+0

会的一部分,对会员加入引起这些查询可以省略未在会员表中列出的实体 - 例如,一个多月前加入但尚未加入*任何*会员的员工? – Wayne

+0

然后更换了左内加盟加盟 – hitty5

+0

只是为了让您通报:我没有下跌了或忘记接受的答案...但我还不能拿到外(!新手的危险)加入工作。在我的实际应用程序中,我有Audio,UserCallFlow和Hearing对象(UCF中的用户收听音频,这些都通过具有'audi'和'ucf'字段的听力表连接起来)。子选择'Audio aud not in(选择听力h中的h.audio,其中h.ucf =:ucf'正在工作,但正在使用'来自Audio aud left join aud.hearings h where h.ucf!=:ucf'always always return空集。所以,仍然黑客! – Wayne