Problems with "findBy" (may be problem of join)

Crixus

New member
Hi there !

I'm begginning with Grails,

This is my first domain class : "Batiment" who has many "Salle"

Code:
class Batiment {
    
    String nom;
    
    static hasMany = [salles : Salle]
    
    static constraints = {
        nom (blank : false, unique : true)
    }
    
    Batiment(String nom){
        this.nom = nom
    }
    
    String toString(){
        return "$nom"
    }
}

This is my second domain class : "Salle" who has one "Batiment"

Code:
class Salle {
    
    String nom;
    int capacite;
    
    static hasOne = [batiment : Batiment]
    static hasMany = [reservations : Reservation]
    static belongsTo = [batiment : Batiment]
    
    static constraints = {
        nom (blank : false, unique:['batiment'])
        capacite (blank : false, min : 1)
    }
    
    Salle(String nom, int capacite){
        this.nom = nom;
        this.capacite = capacite;
    }
    
    String toString() {
        return "$nom"
    }
}

And this is my Bootstrap of test :
Code:
def init = { servletContext ->
        // Création des Batiments de tests
        List<String> listeNomBatiments = [
            "1A", "2A", "3A", "4A",
            "1TP1", "1TP2", "1TP3", "1TP4",
            "1R1", "1R2", "1R3", "1R4",
            "U1", "U2", "U3", "U4"
        ]
        listeNomBatiments.each { 
            nom -> new Batiment(nom).save(failOnError: true);
        }
        
        // Création des Salles de tests
        Batiment.findByNom("U1")
            .addToSalles(new Salle("201",30))
            .addToSalles(new Salle("202",30))
            .addToSalles(new Salle("203",30))
            .addToSalles(new Salle("204",30))
            .addToSalles(new Salle("205",30))
            .addToSalles(new Salle("206",30))
            .addToSalles(new Salle("207",30))
       
       System.out.println("Batiment :")
       Batiment b = Batiment.findByNom("U2")
       if(b != null){
           System.out.println(b) // OK : "U2" 
       }
       else{
           System.out.println("null !")
       }
       
       System.out.println("Salle :");
       Salle s = Salle.findByNom("202")
       if(s != null){
           System.out.println(s) 
       }
       else{
           System.out.println("null !")
       }

The problem is that :

"Batiment b = Batiment.findByNom("U2")" returns the correct "Batiment"
but
"Salle s = Salle.findByNom("202")" returns null
and "202" exists...
I think it's a problem with the "join" on "Batiment"...

That problem makes me crazy since one week :D yet, all "Salle" appear in my web application and "202" is one of them

I try also :
Code:
def salles = Salle.executeQuery(
           'from Salle s where s.nom = :nomSalle',
            [nomSalle: "202"]
       )

But same results...

If someone can help me...

Thanks
 

Crixus

New member
Problem solved,

Do "addTo" in the Bootstrap don't persists the object who is added
It must be done in the constructor :

Domain class "Salle" :
Code:
class Salle {
    
    String nom;
    int capacite;
    
    static hasOne = [batiment : Batiment]
    static hasMany = [reservations : Reservation]
    static belongsTo = [batiment : Batiment]
    
    static constraints = {
        nom (blank : false, unique:['batiment'])
        capacite (blank : false, min : 1)
    }
    
    Salle(Batiment b, String nom, int capacite){
        this.nom = nom;
        this.capacite = capacite;
        b.addToSalles(this)
    }
    
    String toString() {
        return "$nom"
    }

Bootstrap :
Code:
class BootStrap {

    def init = { servletContext ->
        // Création des Batiments de tests
        List<String> listeNomBatiments = [
            "1A", "2A", "3A", "4A",
            "1TP1", "1TP2", "1TP3", "1TP4",
            "1R1", "1R2", "1R3", "1R4",
            "U1", "U2", "U3", "U4"
        ]
        listeNomBatiments.each { 
            nom -> new Batiment(nom).save(failOnError: true);
        }
        
        // Création des Salles de tests
        Batiment U1 = Batiment.findByNom("U1")
        Batiment U2 = Batiment.findByNom("U2")
        new Salle(U1, "201", 30).save(failOnError: true);
        new Salle(U1, "202", 30).save(failOnError: true);
        new Salle(U2, "201", 30).save(failOnError: true);
 
Top