Netmaster
Netmaster

Reputation: 287

JPQL multiple select

I want to extract all data from TypeCmplt like this :

    public List<TypeCmplt> listerrTypeCmplt()
throws DaoException {
    Query query = cmpltTitreEM.createQuery("select t.cdTypeCmplt, t.lbTypeCmplt from TypeCmplt t");
    List<TypeCmplt> listeType = new ArrayList<TypeCmplt>();
    listeType = query.getResultList();
    return listeType;
}

The test method :

    @Test
public void listerComplementsParTypeTest(){
    logger.debug("Afficher la liste des types de complémént") ;

    try {
        List<TypeCmplt> type = cmpltTitreDao.listerrTypeCmplt();
        for (int i = 0; i < type.size(); i++) {
            System.out.println(type.get(i));
        }

    } catch (DaoException e) {
        logger.error("Erreur", e) ;
    }

But I get this :

Hibernate: select typecmplt0_.CD_TYPE_CMPLT as col_0_0_, typecmplt0_.LB_TYPE_CMPLT as col_1_0_ from TC_TYPE_CMPLT typecmplt0_
[Ljava.lang.Object;@4d2b11
[Ljava.lang.Object;@46a5c4
[Ljava.lang.Object;@2d09e0
[Ljava.lang.Object;@e38fca

Can anyone help, please ?

Upvotes: 1

Views: 202

Answers (2)

kostja
kostja

Reputation: 61538

You are creating a List<TypeCmplt> for your results. But the result list is an array of Object becaus you are making a projection query - extracting single attributes of the TypeCmplt. If you want to get the TypeCmpltentities proper, change the query String to

"select t from TypeCmplt t"

If you do want to extract several attributes of the entity, but to do it in a typesafe way, then constructor expressions are the way to go:

List<MyClass> dtos = cmpltTitreEM.createQuery("SELECT NEW com.example.MyClass( t.cdTypeCmplt, t.lbTypeCmplt) FROM TypeCmplt t").getResultList();

Upvotes: 1

Menno
Menno

Reputation: 12621

You are trying to print an Object called TypeCmplt. Try calling toString(). As @kostja said, you'd probably be better off overriding toString() in TypeCmplt.

Like this:

System.out.println(type.get(i).toString());

Or:

TypeCmplt t = type.get(i);
System.out.println(t.getLong() + ": " + t.getString());

In this case you should replace getLong() and getString() accordingly.

Upvotes: 1

Related Questions