0

2 つのオブジェクトが関連付けられているドメインに対してクエリを実行する必要があります。

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

特定のユーザーに属し、video.title が文字列のような場所にあるすべての UserVideos を見つける必要があります。たとえば、「ユーザー 'Fred' に属し、Video.title が '%Potter%' のようなすべての UserVideos を検索します。」何を試しても、何も機能しません。

要求に応じて、アクション用のコードを次に示します。

    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]
4

2 に答える 2

2

このようなもの

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}%"])
于 2013-04-28T02:00:54.813 に答える
1

executeQueryドメインクラスで使用できます。

サンプル:

def newUser = new User(name: "Fred")
def video = new Video(title: "Harry Potter")
[newUser, video]*.save(flush: true)

def userVideo = new UserVideo(user: newUser, video: video)
userVideo.save(flush: true)

def result = UserVideo.executeQuery(
                  "Select distinct uv from UserVideo uv " + 
                  "where uv.user.name = :userName " + 
                  "and uv.video.title like :title",
                  [userName : "Fred", title: "%Potter%"],
                  [max: 10, offset: 10]) //EDIT
//Pagination included. (max: 10, offset: 10) can also be part of the params maps like
//[userName : "Fred", title: "%Potter%", max: 10, offset: 10]

assert "Fred has the movie Harry Potter" == 
             "${result[0].user.name} has the movie ${result[0].video.title}"
于 2013-04-28T02:00:40.880 に答える