2015-10-20 78 views
0

我正在尋找一個更簡單的Grails方式(如果存在)以下來實現:的Grails GORM的對象屬性的財產找到

我UserIntegration.groovy域類:

class UserIntegration { 
User user 
Integration integration 
// rest of code 
} 

我Integration.groovy域:

class Integration { 
SomeType type 
// rest of code 
} 

我SomeType.groovy域:

class SomeType { 
String name 
// rest of code 
} 

所以基本上我需要的是一種方式,以便我能找到一個UserIntegration具有Integrationtype.name = "someTypeName"

換句話說,我要尋找UserIntegration.findByIntegrationTypeName("someTypeName");

顯然,這種動態取景器不存在,但有沒有更簡單的更方便的方法來做到這一點?我現在可以找到所有類型爲「someTypeName」的Integration對象,然後在findAllByIntegration中使用此集成,但是如果存在,則尋找更簡單的解決方案,最好是動態查找器。

+1

是否'UserIntegration.where {integration.type.name == 'someTypeName'}'工作? –

+0

@tim_yates是的,它按預期工作,因爲它實際上是使用標準。任何其他方式來做到這一點? – rahulserver

+0

添加一個[命名查詢](https://grails.github.io/grails-doc/latest/ref/Domain%20Classes/namedQueries.html)? –

回答

0

你也可以使用HQL做同樣的

def list = UserIntegration.executeQuery(from UserIntegration userInt 
             inner join fetch userint.integration integration 
             inner join fetch integration.type sometype 
             where sometype.name =:name,[name:'someTypeName']) 
0
UserIntegration.createCriteria().list(max:1){ 
'integration'{ 
     'type'{ 
      eq('name',"someTypeName") 
     } 
    } 
} 

希望工程