问题描述
在我的新项目中,我尝试使用Hibernate模型类,这里有一个 user 具有 OneToMany 关系的domain类 userProfile like
class User {
//一些字段和getter setter
//有问题的东西
@ javax.persistence.OneToMany(mappedBy =User)
private Set< UserProfile>的UserProfiles;
// getter就像
public Set< userProfile> (){
//逻辑
}
}
因此,当我尝试以grails标准访问此字段(如
)时,def criteria = User.createCriteria()
List< User> userList = criteria.list(){
userProfiles {
eq(id,1 as long)
}
}
我得到如下错误:没有方法的签名:UserService.userProfiles()。我认为这可能是因为 getter 和 setter 名称不同,导致剩余的 OneToMany 字段标准工作正常。
是否有任何可能的标准方式解决这个问题。
class User {
static hasMany = [userProfiles:UserProfile]
}
$ b 类似于 getUserProfiles(), setUserProfiles(),<$ c自动生成$ c> addToUserProfiles(UserProfile p)等。请参阅。
然后你可以做这样的事情:
def userList = User.withCriteria {
userProfiles {
idEq 1
}
}
我希望有帮助。
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.
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.
这篇关于Grails很难使用Hibernate OneToMany模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!