问题
In my new project I am trying to use Hibernate model class, here one user
domain class having OneToMany
relation userProfile
like
class User {
//Some fields and getter setter
//Problematic thing
@javax.persistence.OneToMany(mappedBy = "User")
private Set<UserProfile> userProfiles;
//getter is like
public Set<userProfile> getProfile() {
// the logic
}
public void setProfile() {
// the logic
}
}
So when I try to access this field in grails criteria like
def criteria = User.createCriteria()
List<User> userList = criteria.list() {
userProfiles {
eq("id",1 as long)
}
}
I getting the error like No signature of method: UserService.userProfiles()
. I think it might be because of different getter
and setter
name, 'cause for remaining OneToMany
fields the criteria
is working fine.
Is there any possible and standard way to address this issue.
回答1:
This is a more common thing to do:
class User {
static hasMany = [userProfiles: UserProfile]
}
Methods like getUserProfiles()
, setUserProfiles()
, addToUserProfiles(UserProfile p)
etc. are automatically generated. See http://grails.org/doc/latest/ref/Domain%20Classes/hasMany.html.
Then you could do something like this:
def userList = User.withCriteria {
userProfiles {
idEq 1
}
}
I hope that helps.
来源:https://stackoverflow.com/questions/25812376/grails-difficultly-in-using-hibernate-onetomany-model