2014-10-30 4 views
1

(Grails 2.4.2)컬렉션이 예상 결과를 생성하지 않는 도메인 클래스를 쿼리하는 Grails

사용자가 액세스 할 수있는 이야기의 목록을 얻으려고합니다. 따라서 사용자가 이야기 또는 사용자의 소유자 인 모든 이야기는이 이야기의 편집자 집단에 있습니다. createCriteria에서 HQL 작성에 이르기까지 여러 가지 다른 쿼리를 시도했습니다. 사용자가 소유하고있는 모든 이야기를 쿼리 할 수 ​​있습니다. 사용자가 편집자 컬렉션에있는 모든 이야기를 쿼리 할 수 ​​있지만 OR ...과 함께 쿼리를 넣을 때 올바른 결과를 얻지 못합니다.

class Story { 
    String title 
    Date dateCreated 
    Date lastUpdated 
    String description 
    Boolean isPublic 
    List storyContent = [] 
    User owner 

    static belongsTo = [owner: User] 
    static hasMany = [storyContent : StoryContent, viewers : Viewer, editors : Editor] 
...more 


class Editor { 
    User user 
    static belongsTo = [story: Story] 

나는 다음과 같은 쿼리를 수행 할 때 :

Result: [[com.storycreate.Story : 3, com.storycreate.Editor : 1], [com.storycreate.Story : 2, null]] 

조 지금은 모든 쿼리 층 2 층 3의 소유자이다 :

def hql = "from Story as s left outer join s.editors as se where s.owner.username = 'joe'" 
def results = Story.executeQuery(hql) 

을 나는 정확한 결과를 얻을 수 조이 편집자 인 이야기

def hql = "from Story as s left outer join s.editors as se where se.user.username='joe'" 
def results = Story.executeQuery(hql) 

얻을 올바른 결과 : 나는 조 소유자 또는 편집자 중 하나입니다이 이야기의 목록을 결합하면 지금

Result: [[com.storycreate.Story : 3, com.storycreate.Editor : 1], [com.storycreate.Story : 4, com.storycreate.Editor : 4]] 

(조 편집기와 소유자 이야기 (3)과 story4의 편집자 둘 다)

def hql = "from Story as s left outer join s.editors as se where (s.owner.username='joe' OR se.user.username='joe')" 
def results = Story.executeQuery(hql) 

내가 잘못된 결과가 결국

Result: [[com.storycreate.Story : 3, com.storycreate.Editor : 1], [com.storycreate.Story : 4, com.storycreate.Editor : 4]] 

(조 소유자 인 스토리 2 누락) 얻을, 나는 쿼리가 나에게 isPublic 사실 또는 로그인 한 사용자가 모든 이야기의 목록을 제공 할 헛소리 야. 네이더, 편집자, 또는 시청자. 하지만 나는 Hibernate가 여기서 어떻게 작동하는지 이해하지 못하는 것 같다.

+0

, 나는이 결과를 생각'결과 : [[com.storycreate.Story : 3, COM .storycreate.Editor : 1], [com.storycreate.Story : 2, null]]'보이지 않습니다. –

+0

아마도 결과를 이해할 수 없습니다. 나는 Grails와 조금은 초보자이다. 나에게 결과는 2 개의 행을 보여준다. 첫 번째는 ID 3의 Story 객체를 표시하고 두 번째는 Story 객체 ID 2를 표시합니다. 맞지 않습니까? – jer0dh

답변

0

이 통합 규격은 2.4.3에서 나를 위해 전달합니다 조 이야기 2, 3의 소유자 인 경우

class StoryControllerIntSpec extends IntegrationSpec { 

    def setup() { 
     User joe = new User(userName: "joe").save() 
     User bar = new User(userName: "bar").save() 
     User foo = new User(userName: "foo").save() 

     // joe as both owner and editor 
     Story s1 = new Story(isPublic: true, title: "Story1", owner: joe) 
     Editor e1 = new Editor(user: joe) 
     s1.addToEditors(e1) 

     // joe only as owner 
     Story s2 = new Story(isPublic: true, title: "Story2", owner: joe) 
     Editor e2 = new Editor(user: foo) 
     s2.addToEditors(e2) 

     // joe only as editor 
     Story s3 = new Story(isPublic: true, title: "Story3", owner: bar) 
     Editor e3 = new Editor(user: joe) 
     s3.addToEditors(e3) 

     // joe not related to story 
     Story s4 = new Story(isPublic: true, title: "Story4", owner: foo) 
     Editor e4 = new Editor(user: bar) 
     s4.addToEditors(e4) 

     [s1, s2, s3, s4]*.save() 
    } 

    void "test something"() { 
     given: 
     StoryController controller = new StoryController() 

     when: 
     def model = controller.index() 

     then: 
     model.result.size() == 3 
     def stories = model.result.collect { it[0] } 
     stories.size() == 3 
     stories*.title == ['Story1', 'Story2', 'Story3'] 
    } 
} 

// Controller 
class StoryController { 

    def index() { 
     def hql = "from Story as s left outer join s.editors as se \ 
        where (s.owner.userName='joe' OR se.user.userName='joe')" 
     def results = Story.executeQuery(hql) 

     [result: results] 
    } 
} 
+0

와우, dmahapatro. 그것을 시도해 주셔서 감사 드리며 아마도 이것을 정확하게하고 있음을 확인해 주실 것입니다. 내 버젼의 Grails와 Hibernate (현재 : 4.3.5.4)에 문제가 될 수 있습니다. 이 문제를 테스트하기 위해 업데이트 된 환경을 만들 것입니다. 상관 없다고 생각하지만 데이터 소스는 무엇을 사용하고 있습니까? H2 메모리 또는 SQL 데이터베이스에? – jer0dh

+0

H2 in-memoory db. – dmahapatro

+0

Grails 2.4.3 및 4.3.5.4에서 4.3.6.1로 내 Hibernate 플러그인으로 업그레이드되었으며 쿼리가 작동했습니다. 어떤 이유로 업그레이드로 인해 모든 autoTimestamp 필드 (dateCreated 및 lastUpdated)가 작동하지 않게되었습니다. 나는 그들을 언급해야했다. 그러나 그것은 다른 시간 동안이다. 도와 줘서 고마워! 대답으로 표시했지만 0 평판으로는 +1 할 수 없습니다. – jer0dh

관련 문제