开发者

JPA/Criteria - Select (or multiselect) on SetAttribute

I am getting really frustrated trying to use JPA criterias (I am using Hibernate implementation).. I want to make a query where the select is on a Set of objects, but I can't make it work..

Here is my code:

public List<Object> findAllTypeGroupes(){
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object> criteriaQuery =  criteriaBuilder.createQuery(Object.class);

    /* FROM */
    Root<Contact> contactRoot = criteriaQuery.from(Contact.class);
    /* SELECT */
    criteriaQuery.select( contactRoot.get( Contact_.groupes ) );

    return entityManager.createQuery(criteriaQuery).getResultList();
}

@StaticMetamodel(Contact.class)
public abstract class Contact_ {
    public static volatile SingularAttribute<Contact, Integer> contactId;
    public static volatile SetAttribute<Contact, TypeGroupe> groupes;
}

If I try to make a select on "Contact_.groupes", where "groupes" is a set (c.f. metamodel: "SetAttribute"), I was expecting to get a List>, but I am actually getting a flatten list List... Hibernate is actually generating one single SQL query, when I was expecting to get a query for each item/set of my L开发者_Python百科ist....

But what I originally wanted was to make a query with a multiselect to get couple of fields, one of them beeing my Set.. If I try to make a multiselect with my "Contact_.groupes" and for instance "Contact.contactId" like here:

public List<Object[]> findAllWrapperByCriteria(ContactCriteria criteria){
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);

    /* FROM */
    Root<Contact> contactRoot = criteriaQuery.from(Contact.class);

    criteriaQuery.multiselect( contactRoot.get( Contact_.contactId ), contactRoot.get( Contact_.groupes ) );


    return entityManager.createQuery(criteriaQuery).getResultList();
}

Things are obviously not working eather and I am getting an exception this time, because Hibernate generated me a bad SQL Query:

select contact0_.CONTACT_ID as col_0_0_, . as col_1_0_, typegroupe2_.TYPE_GROUPE_ID as TYPE1_6_, typegroupe2_.LIBELLE as LIBELLE6_, typegroupe2_.MUT_TS as MUT3_6_, typegroupe2_.MUT_USER as MUT4_6_, typegroupe2_.REMARQUE as REMARQUE6_, typegroupe2_.VISIBLE as VISIBLE6_ from CONTACT contact0_ inner join CONTACT_GROUPE groupes1_ on contact0_.CONTACT_ID=groupes1_.CONTACT_ID inner join TYPE_GROUPE typegroupe2_ on groupes1_.GROUPE_TYPE_GROUPE_ID=typegroupe2_.TYPE_GROUPE_ID

As you can see just after the first coma: ". as col_1_0_" which isn't a valid SQL query..

So I really don't know how to do what I want.. On my object Contact, all fields are eager, because when I load a contact I close the transaction and thus the object get detached and won't allow me to get data lazy loaded. But I have a "search screen" where I am displaying a subset of all contacts fields.. So in order to dramatically increase perforances, I was willing to make a specific query for this part and load only what I need instead of a "SELECT * FROM Contact"...

Does somebody have any idea of what is going on and how to solve my problem ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜