grails - 对两个关联的Grails GORM查询

标签 grails gorm

我需要在具有两个关联对象的域上进行查询。

class UserVideo {
    User user
    Video video
}
class User {
    String name
}
class Video {
    String title
}

我需要找到属于给定用户的所有UserVideo,并且video.title就像一个字符串。例如,“查找属于用户'Fred'的所有UserVideo,而Video.title就像'%Potter%'。”
无论我尝试了什么,都无济于事。

根据要求,以下是我执行操作的代码:
    def list(Integer max, String title) {
    def userName = springSecurityService?.principal?.username
    def user = User.findByUsername(userName)
    params.max = Math.min(max ?: 10, 100)
    def results
    if (title) {
        def c = UserVideo.createCriteria()
        results = c.list {
            eq("user", user)
            video {
                ilike("title", "%${title}%")
            }
        }
    } else {
        results = UserVideo.findAllByUser(user, params)
    }
    def userVideos = results.collect({ UserVideo uv ->
        [uId: uv.user?.id, vId: uv.video?.id, number: uv.number, title: uv.video?.title,
                format: uv.video?.format?.name, rating: uv.video?.rating?.name, genres: uv.video?.genreNames]
    })
    [userVideos: userVideos, total: UserVideo.count()]
}

这是我得到的错误:
URI
/movies/userVideo/list
Class
org.h2.jdbc.JdbcSQLException
Message
Column "VIDEO_ALIA1_.TITLE" not found; SQL statement: select this_.user_id as user1_2_0_, this_.video_id as video2_2_0_, this_.number as number2_0_ from user_video this_ where this_.user_id=? and (lower(video_alia1_.title) like ?) [42122-170]

最佳答案

像这样

def c = UserVideo.createCriteria()
def results = c.list {
  eq("user", User.findByName("Fred"))
  video {
    ilike("title", "%Potter%")
  }
}

更新-尝试HQL
UserVideo.executeQuery("""
  Select uv
  From UserVideo as uv join uv.video v join uv.user u
  where lower(v.title) like lower(:title)
  and u = :user
""", [user: user, title: "%${title}%"])

关于grails - 对两个关联的Grails GORM查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16258604/

相关文章:

grails - Grails Groovy-无法从另一个类访问静态 map (集合)

eclipse - Eclipse JDK设置如何影响系统的JDK设置

grails - grails 3 webflow不起作用

grails - 查询 GORM 关联

java - Grails mongodb 映射类型给出非法状态异常

grails - Grails-动态查找器派生日期查询GORM

grails - 可以使用Grails的派生属性检索域对象吗?

grails - Grails-唯一且为空

grails - Grails分页设置最高记录

spring - 检查直接非循环图是否没有循环?