2011-09-01 137 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