gpt4 book ai didi

hibernate - Grails GORM嵌套的多对多关系查询

转载 作者:行者123 更新时间:2023-12-02 15:23:21 24 4
gpt4 key购买 nike

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

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可能会为包含其user2和user3的friendzone1做一个可见的帖子。
现在我想获取所有对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类会引起问题。根据伊曼纽尔的评论,我发现尝试查询交友区时抛出了错误(与上述相同)。例如:
def audience = Friendzone.get(1)

也许是与“用户类”的关系
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]

最佳答案

这个怎么样?

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

我以这种方式编写它,以使其更易于阅读,并查找哪个查询失败。

关于hibernate - Grails GORM嵌套的多对多关系查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com