Grails difficultly in using Hibernate OneToMany model

假装没事ソ 提交于 2020-01-25 11:47:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!