Right use of maps as fields (MapPath)

532 views
Skip to first unread message

peter

unread,
Apr 23, 2014, 2:18:01 PM4/23/14
to quer...@googlegroups.com
Hey ... It's me... again :(

I struggle with a Map, which is a private field in my ItemDefinition class...

An ItemAttribute contains of an id, a name and a description.
An ItemDefinition can have several key[attribute] / value[String] pairs. These are saved as a Map.

        QItemDefinition item = QItemDefinition.itemDefinition;
        QItemAttribute attribute = QItemAttribute.itemAttribute;
        MapPath<ItemAttribute, String, StringPath> attributes = item.itemAttributes;

        StringPath alias = new StringPath("attr_");
        for (ItemAttribute at : findAll()) {
            JPAQuery query = new JPAQuery(entityManager).from(item).leftJoin(attributes, alias).where(attributes.containsKey(at));
            log.info("{}", query.list(item, alias));
        }

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [select itemDefinition, tmp
from ItemDefinition itemDefinition
  left join itemDefinition.itemAttributes as tmp
where containsKey(itemDefinition.itemAttributes,?1)].
[115, 161] The expression is not a valid conditional expression.
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildException(HermesParser.java:155)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.validate(HermesParser.java:334)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.populateQueryImp(HermesParser.java:278)
    at org.eclipse.persistence.internal.jpa.jpql.HermesParser.buildQuery(HermesParser.java:163)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:142)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1603)
    ... 47 more

.. how can I access the keys/values in this map... I'd like to order and filter them as well.... :(
.. I fear the solution is as simple as the last time?!?

timowest

unread,
Apr 24, 2014, 5:57:02 AM4/24/14
to quer...@googlegroups.com
Hi.

Sorry for the late answer. That might no be mapped incorrectly. I am not sure though, if this can be mapped correctly in JPQL.

JPQL doesn't provide standard access to keys and values of a map.

Timo

peter

unread,
Apr 25, 2014, 4:24:02 AM4/25/14
to quer...@googlegroups.com
... some kind of hack, workaround,... ^^
works for me like a charme =)
It's not really what I want, but ok for my purposes

    QItemDefinition item = QItemDefinition.itemDefinition;
    QItemAttribute attribute = QItemAttribute.itemAttribute;
    StringPath sp= new StringPath("attr_");
   
    List<Tuple> result = new JPAQuery(entityManager, new CustomTemplate())
        .from(item, attribute)
        .innerJoin(item.itemAttributes, sp)
        .where(item.itemAttributes.containsKey(attribute))
        .list(item, attribute, sp);

...
    private class
CustomTemplate extends EclipseLinkTemplates {

        public Template() {
            super();
            add(Ops.CONTAINS_KEY, "key(
attr_) = {1}");
        }
    }

Timo Westkämper

unread,
Apr 25, 2014, 4:26:08 AM4/25/14
to Querydsl on behalf of peter
Hi.

Thanks, I will see how well this syntax is supported.

Timo


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


John Aylward

unread,
Dec 5, 2016, 1:28:16 PM12/5/16
to Querydsl
Is there an update for this? I'm trying to reference a Map<Entity1,Entity2> in a query, but can't seem to find the right way to do it. the StringPath constructor has been made protected, so the answer in this thread is obsolete.



On Friday, April 25, 2014 at 4:26:08 AM UTC-4, Timo Westkämper wrote:
Hi.

Thanks, I will see how well this syntax is supported.

Timo
Reply all
Reply to author
Forward
0 new messages