GORM

2013-08-23 2 views
1

에서 자식 테이블의 특정 행을 얻기 위해 어떻게하면이 같은 두 개의 도메인이 : 나는 발견 할GORM

class Color { 
    static hasMany = [shade: Shade] 
    String colorName 
    String colorType 
} 

class Shade { 
    static belongsTo = [color: Color] 
    String shadeName 
    boolean isAvailable 
} 

을 모든 colors이 그 사용할 수 없습니다 어떤 shades :

그래서 내 데이터는 다음과 같은 경우 :

ID  color_name color_type 
---- --------  ---------- 
22  RED   CRAYON 
23  GREEN   PAINT 
45  GREY   CRAYON 

ID  color_id  shade_name  is_available 
--- ----------  ------------- ---------- 
2  22    DARK   false 
3  22    LIGHT   true 
4  23    DARK   true 
5  23    LIGHT   true 
6  45    DARK   false 
7  45    LIGHT   false 

나는 나의 결과가 012을 일부 그늘이 있기 때문에 IDS 22 and 45 색상의 항목 크기 2의되고 싶어

38,내가이 쿼리를 시도하지만이 내가 이것에 대한 최대 절전 모드에 의해 생성 된 SQL을 볼 때 나는

def query = Color.where { 
shade.isAvailable == false 
} 
def list = query.list() 

를 원하는 반환 여부를 완전히 확실하지 않다, 나는 어떤 group by 절을 통지하지 않습니다 select 문은 당신이 필요로하는 것을 얻을 모두 colorshade

+0

이 답변이 도움이되었다 IMO 가장 쉬운 방법입니다? – dmahapatro

답변

1

당신은 기준 또는 HQL을 사용할 수 있습니다에서 coloumns을 받고있다 :

//Criteria Approach: 
//In this approach "shade" will be fetched eagerly with color 
def colors = Color.createCriteria().listDistinct{ 
    shade{ 
     eq('isAvailable', false) 
    } 
} 

//HQL 
//In this approach only color will be fetched 
//without shade, unless specified as ".... inner join fetch c.shade ..." 
def colors = Color.executeQuery("select distinct c from Color as c \ 
           inner join c.shade as s \ 
           where s.isAvailable is false") 

hasMany 연관에 대한 복수 표기법을 선호하므로 shade 대신 shades (관련성을 더 생생하게 표현)을 사용합니다.

0

HQL은

Shade.executeQuery("select distinct s.color from Shade s where s.isAvailable = false") 
관련 문제