[英]Grails GORM nested has-many relationship query
I try to get all posts that are adressed to an audience containing one specific user by: 我尝试通过以下方式获取包含一个特定用户的所有受众群体的帖子:
StreamPost.findAllByAudience(Friendzone.findAllByFriends(User.findAllById(2)))
or 要么
def posts = StreamPost.where {
audience.friends.id ==~ userId
}.list()
First results in 的第一个结果
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 1
Second does not work either, returns: 第二个也不起作用,返回:
[]
I have the following domain model: 我有以下域模型:
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
}
}
So user1 might do a post visible for his friendzone1 which contains user2 and user3. 因此,user1可能会为包含其user2和user3的friendzone1做一个可见的帖子。 Now I want to get all posts which are visible for user2...
现在我想获取所有对user2可见的帖子...
Which ist the best method doing this? 哪种方法是最好的方法? dynamic finders, where queries, criteria or hql?
动态查找器,查询,条件或hql在哪里? How can I avoid previous mentioned errors?
如何避免前面提到的错误?
Database scheme: 数据库方案:
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
Edit 19.08.2015 编辑19.08.2015
I think the friendzone class causes the problems. 我认为friendzone类会引起问题。 With Emmanuel's comment I figured out that the error (same error as above) is thrown trying to query a friendzone.
通过Emmanuel的评论,我发现尝试查询Friendzone时抛出了错误(与上述相同)。 For example:
例如:
def audience = Friendzone.get(1)
Maybe it is the relationship to the "user class" 也许是与“用户类”的关系
static belongsTo = User
static hasMany = [friends:User,
streamposts:StreamPost]
Got it done by changing StreamPost class: 通过更改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']
}
}
with following query: 与以下查询:
def user = User.get(userID)
def posts = StreamPost.findAllByAudienceInList(user.friendzones.asList())
How about this? 这个怎么样?
def user = User.get(2)
def audiences = Friendzone.findAllByFriends(user)
def posts = StreamPost.findAllByAudienceInList(audiences)
I wrote it this way to make it easier to read, and to find which query is failing. 我以这种方式编写它,以使其更易于阅读,并查找哪个查询失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.