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. Now I want to get all posts which are visible for user2...
Which ist the best method doing this? dynamic finders, where queries, criteria or 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
I think the friendzone class causes the problems. With Emmanuel's comment I figured out that the error (same error as above) is thrown trying to query a 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]
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.