2014-10-30 49 views
1

(Grails 2.4.2)Grails使用不會產生預期結果的集合查詢域類

我試圖獲取用戶可以訪問的故事列表。因此,故事的所有故事用戶都是故事的所有者,或者用戶處於故事的編輯集合中。我已經嘗試了許多不同的查詢,從createCriteria到編寫HQL。我可以查詢用戶是所有者的所有故事。我可以查詢用戶在編輯器集合中的所有故事,但是當我將查詢與OR..I一起輸入時,得不到正確的結果。

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] 

當我做下面的查詢:

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

我得到正確的結果:

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

喬的故事2和3,故事的主人現在我查詢所有Joe編輯的故事

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

,得到正確的結果:(喬既是編輯和老闆的故事3和story4編輯)

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

如果我結合這讓故事的一個列表,其中喬是所有者或編輯:

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) 

我得到不正確的結果(失蹤的故事2,其中喬是老闆)

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

最後,我想查詢給我所有的故事,其中isPublic爲TRUE或登錄用戶的列表是流量編輯或者故事的觀衆。但我似乎缺少對Hibernate如何在這裏工作的一些理解。

+0

如果喬是故事2和3的車主,我覺得這個結果'結果:[com.storycreate.Story:3,COM .storycreate.Editor:1],[com.storycreate.Story:2,null]]'沒有顯示它。 – 2014-10-30 22:58:14

+0

也許我不瞭解結果。我對Grails有點新手。對我來說結果顯示2行。第一個顯示ID 3的Story對象,第二個顯示Story對象ID 2.這是不對的? – jer0dh 2014-10-30 23:18:59

回答

0

這種整合規範通行證我在2.4.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 2014-10-31 03:36:44

+0

H2 in-memoory db。 – dmahapatro 2014-11-01 02:02:02

+0

升級到Grails 2.4.3和我的Hibernate插件從4.3.5.4升級到4.3.6.1,查詢工作。出於某種原因,升級導致我所有的autoTimestamp字段(dateCreated和lastUpdated)停止工作。我不得不對他們發表評論......但那是另一次。謝謝你的協助!我將它標記爲答案,但我的0聲望不允許我加1。 – jer0dh 2014-11-05 21:27:49

相關問題