我尝试通过以下方式获取所有贴在包含特定用户的受众群体的帖子:
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类会导致问题。在Emmanuel的评论中,我发现错误(与上面相同的错误)是在尝试查询friendzone时抛出的。例如:
def audience = Friendzone.get(1)
也许这是与“用户类”的关系
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
答案 0 :(得分:1)
通过更改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())
答案 1 :(得分:0)
这个怎么样?
def user = User.get(2)
def audiences = Friendzone.findAllByFriends(user)
def posts = StreamPost.findAllByAudienceInList(audiences)
我这样写它是为了让它更容易阅读,并找到哪个查询失败。