Search code examples
javamysqldatabasehibernategrails

Select query using the condition


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.

enter image description here


Solution

  • 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
    }