2014-10-30 45 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

相关问题