0

hi i'm using spring data in My project and I'm trying group by two fields, heres the request:

@Query( "SELECT obj from Agence obj GROUP BY  obj.secteur.nomSecteur,obj.nomAgence" )
Iterable<Agence> getSecteurAgenceByPc();

it works but the data doesnt grouped by secteur...heres what it gives me:

{
    "status": 0,
    "data":
    [
        {
            "secteur": "Safi",
            "agence": "CTM"
        },
        {
            "secteur": "Safi",
            "agence": "Dep"
        },
        {
            "secteur": "Rabat",
            "agence": "Agdal"
        },
        {
            "secteur": "Rabat",
            "agence": "CTM"
        },
        {
            "secteur": "Essaouira",
            "agence": "CTM"
        },
        {
            "secteur": "Essaouira",
            "agence": "Gare Routiere Municipale"
        }
    ]
}

the behaviour which i'm looking for is:

-Safi
  -CTM
  -Dep  

-Rabat
  -CTM
  -Agdal

-Essaouira
  -CTM
  -Gare Routiere Municipale
  • Possible duplicate of [how to groupBY using spring data](http://stackoverflow.com/questions/41446011/how-to-groupby-using-spring-data) – Jens Schauder Jan 06 '17 at 06:53

1 Answers1

0

If you just want tou group by secteur try :

"SELECT obj from Agence obj GROUP BY obj.secteur.nomSecteur"

laside
  • 1
  • 1
  • And also i lose data(i lose agence's data) –  Jan 04 '17 at 14:21
  • maybe that help you : http://stackoverflow.com/questions/36328063/how-to-return-a-custom-object-from-a-spring-data-jpa-group-by-query – laside Jan 04 '17 at 14:54