【问题标题】:Grails GORM nested has-many relationship queryGrails GORM 嵌套的 has-many 关系查询
【发布时间】:2015-08-18 13:37:18
【问题描述】:

我尝试通过以下方式获取所有针对包含一个特定用户的受众的帖子:

StreamPost.findAllByAudience(Friendzone.findAllByFriends(User.findAllById(2)))

def posts = StreamPost.where {
    audience.friends.id ==~ userId
}.list()

第一个结果

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 1

第二个也不行,返回:

[]

我有以下领域模型:

class StreamPost {
    static belongsTo = User
    Friendzone audience

    Date postTimestamp
    String postComment
    String postType

    static constraints = {
        postType inList: ['event','activitylevel','checkin','rating']
    }
}

class Friendzone {
    static belongsTo = User
    static hasMany = [friends:User,
                      streamposts:StreamPost]
    User owner

    String zoneName
    static constraints = {
        owner nullable: false
        friends nullable: false
    }
}

class User {
    static hasMany = [friends:User,
                      friendzones:Friendzone,
                      streamposts:StreamPost]
    static mappedBy  = [ friends: 'friends' ]

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    static constraints = {
        username nullable: false
        password nullable: true
    }
}

所以 user1 可能会为他的friendzone1 发布一个可见的帖子,其中包含 user2 和 user3。 现在我想获取对 user2 可见的所有帖子...

执行此操作的最佳方法是什么?动态查找器,在哪里查询、标准或 hql?如何避免前面提到的错误?

数据库方案:

table: user
id  |  username
1   |  user1
2   |  user2

table: user_friendzone
user_id  |  friendzone_id
2        |  1

table: friendzone
id  |  owner_id  |  zone_name
1   |  1         |  user2only

table: stream_post
id  |  audience_id  |  post_comment
1   |  1            |  comment

编辑 19.08.2015

我认为friendzone类会导致问题。根据 Emmanuel 的评论,我发现错误(与上述相同的错误)是在尝试查询朋友区时引发的。例如:

def audience = Friendzone.get(1)

可能是和“用户类”的关系

static belongsTo = User
static hasMany = [friends:User,
                  streamposts:StreamPost]

【问题讨论】:

    标签: hibernate grails nested grails-orm has-many


    【解决方案1】:

    这个怎么样?

    def user = User.get(2)
    def audiences = Friendzone.findAllByFriends(user)
    def posts = StreamPost.findAllByAudienceInList(audiences)
    

    我这样写是为了更容易阅读,并找出哪个查询失败了。

    【讨论】:

    • 好主意!查询失败:def audiences = Friendzone.findAllByFriends(user) 使用数据库方案更新问题
    • ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 1
    【解决方案2】:

    通过更改 StreamPost 类来完成:

    class StreamPost {
        static belongsTo = [owner:User, audience:Friendzone]
    
        User owner
        Friendzone audience
    
        Date postTimestamp
        String postComment
        String postType
    
        static constraints = {
            postType inList: ['event','activitylevel','checkin','rating']
        }
    }
    

    使用以下查询:

    def user = User.get(userID)
    def posts = StreamPost.findAllByAudienceInList(user.friendzones.asList())
    

    【讨论】:

    • 太棒了!这真是令人费解。我尝试了 where() 查询,但我无法完全理解。很高兴您找到了一个优雅的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    相关资源
    最近更新 更多