使用条件选择查询

问题描述:

我这里有三张桌子。用户可以应用可用的训练。当他/她选择训练时,根据他/她的用户ID将其放在TRAINUSER表中。我想在他/她登录时选择用户并显示他/她未应用培训的TRAINUSER表中的数据。与user_id 1一样,已经应用了2次和3次训练,但未应用1次和4次训练。我希望user_id 1使用hibernate查询显示训练1和4。

I have three tables here.The user is able to apply the training available.when he/she selects the training then it is placed at TRAINUSER table according to his/her userID. I want to SELECT the user when he/she logs in and to show data from TRAINUSER table which he/she has not applied the training. Like user_id 1 has applied 2 and 3 training but has not applied 1 and 4 training. I want user_id 1 to show with training 1 and 4 using hibernate query.

其中一些将取决于您的域类的外观,您可以像这样使用executeQuery:

Some of this will depend on what your domain classes look like, you could use executeQuery like so:

Training.executeQuery( "from Training tr where tr.id not in ( select t.id from TrainUser tu join tu.training t join tu.user u where u.username = :uname )", [uname: 'ADMIN'] )

假设以下域名,仅包含相关字段:

Assuming the following domains, only relevant fields included:

class User {
    String username
}

class TrainUser {
    static hasMany = [training: Training, user: User]
}

class Training {
    String name
}