Criteria query: could not resolve property (a twitter like application to show posts from current user)

StackOverflow https://stackoverflow.com/questions/23405612

  •  13-07-2023
  •  | 
  •  

Question

I'm doing a twitter clone project. Here I want to show posts from logged in User. I've used my own authentication. I have following domains.

User - has email and password property (user has one profile, User has many posts) Profile - has name, phone and bio property. (profile belongs to User) Post - has post and date created (post belongs to User)

Post Domain

package blog.dwit

class Post {

    String post;
    Date dateCreated;

    static belongsTo = [user:User]

    static constraints = {
        post size: 1..250
    }
}

Post Controller package blog.dwit

class PostController {
    def postService;

    def index() {}

    def insert (){
        render 'it works'
        def post = postService.insertPost(params, session.user)
    }

    def showPost (){
        def user = User.get(session.user)
        def c= Post.createCriteria()
        def userPost = c.list {
            ilike('user_id', user.id )
            order('dateCreated', 'desc')
        }

       [userPost: userPost]

    }
}

I'm getting problem in show post, and new to hibernate and criteria query. I'm trying to map user_id - field in my database to the user id of currently logged in user. If this is totally wrong could you please give me a solution.

Post Service

package blog.dwit

class PostService {

    def insertPost (def params, def UserId){
        def user = User.get(UserId)

            def post = new Post(params)

            user.addToPosts(post)
            return user.save(flush: true)
    }
}

Error

Error 500: Internal Server Error
URI - /blog-dwit/post/showPost
Class -  org.hibernate.QueryException
Message - could not resolve property: user_id of: blog.dwit.Post

Around line 18 of grails-app/controllers/blog/dwit/PostController.groovy
15:    def showPost (){16:        def user = User.get(session.user)17:        def c=        Post.createCriteria()18:        def userPost = c.list {19:            ilike('user_id', user.id )20:            order('dateCreated', 'desc')21:        }
Was it helpful?

Solution

You shouldn't pass params into the service, the HTTP request params should only be used in the web layer. Try this instead

Controller

class PostController {
    def postService;

    def index() {}

    def insert () {
        render 'it works'
        def post = postService.insertPost(params.post, session.user)
    }

    def showPost () {
        def user = User.get(session.user)

        def userPost = Post.withCriteria {
            eq('user', user)
            order('dateCreated', 'desc')
        }

       [userPost: userPost]

    }
}

Service

class PostService {

    def insertPost (String post, userId) {

        def user = User.get(userId)
        def post = new Post(post: post)
        user.addToPosts(post)
        return user.save(flush: true)
    }
}

OTHER TIPS

When using criteria, GORM or HQL, you're working with domain objects properties not with database columns, so you should use something like this instead:

    def userPost = c.list {
        ilike('user', user)
        order('dateCreated', 'desc')
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top