Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Query on Map attribute from Embedded Object - ElementCollection from embedded Object

I haven't tested this, but try using IN instead of join in JPQL.  ie:
  "...from TestEntity a, IN(a.eAttr.sample) n where..."
There might be a problem with using an embeddable intermediary, so you might need to join the eAttr independently before trying to join its sample. 

The native expressions won't work because "eAttr.sample" isn't an attribute in TestEntity.  You need to use something like:
  expBuilder.get("eAttr").anyOf("sample")
And need to use _expression_.mapEntry() and _expression_.mapKey() to access the value/key portions within the _expression_ ie:
  Expresssion sample = expBuilder.get("eAttr").anyOf("sample");
  sample.mapKey().equal("xxxx").and(sample.mapEntry().equal("yyyy"));

Best Regards,
Chris
 


On 15/03/2011 3:16 PM, Alay, Aysun wrote:

Hi all,

My Entity (TestEntity) has one embedded attribute(eAttr) which contains a Map attribute (sample).  It is annotated with ElementCollection.

 

    @ElementCollection

    @CollectionTable(name = "TABLE_1",

            joinColumns = @JoinColumn(name = "PARENT_ID"))

    @MapKeyColumn(name = "C1")

    @Column(name = "C2")

    private Map<String, String> sample = new HashMap<String, String>();

 

You can find the details of the queries and the exceptions below. I justw anted to check if you have any experience on querying the embedded Map attributes.

 

I am trying to query on my “sample” attribute where C1 equals “xxxx” and C2 equals “yyyy”. I have first tried with JQPL like this

 

Query qry = odb_em.createQuery("select a from TestEntity a join a.eAttr.sample n where KEY(n) = :param1 and VALUE(n)= :param2");

qry.setParameter(“param1”, “xxxx”);

qry.setParameter(“param2”, “yyyy”);

List l = qry.getResultList();

System.out.println("Query Size:::" + l.size());

 

 

But I received this error

Exception in thread "main" Local Exception Stack:

Exception [EclipseLink-6070] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.QueryException

Exception Description: Invalid use of a query key [org.eclipse.persistence.mappings.DirectMapMapping[sample]] representing a "to-many" relationship in an _expression_.  Use anyOf() rather than get().

      at org.eclipse.persistence.exceptions.QueryException.invalidUseOfToManyQueryKeyInExpression(QueryException.java:735)

      at org.eclipse.persistence.internal.expressions.QueryKeyExpression.validateNode(QueryKeyExpression.java:887)

      at org.eclipse.persistence.expressions._expression_.normalize(_expression_.java:3009)

      at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:342)

      at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:612)

      at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:599)

      at org.eclipse.persistence.internal.expressions.DataExpression.normalize(DataExpression.java:336)

      at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:612)

      at org.eclipse.persistence.internal.expressions.QueryKeyExpression.normalize(QueryKeyExpression.java:599)

      at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)

      at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:476)

      at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)

      at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1300)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:491)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1553)

      at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:678)

      at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:625)

      at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:509)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:820)

      at org.eclipse.persistence.queries.DatabaseQuery.prepareCall(DatabaseQuery.java:1588)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:266)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:182)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:134)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:118)

      at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1373)

      at com.ciena.test.TestJpa.main(TestJpa.java:341)

 

Then I tried ExpressionBuilder and received the following exception

 

//Eclipse specific classes - getting JpaEntityManager

      JpaEntityManager eclipseLink_em = JpaHelper.getEntityManager(em);

 

      ExpressionBuilder expBuilder = new ExpressionBuilder();

      _expression_ ex = expBuilder.anyOf("eAttr.sample").mapKey().equal("xxxx");

      Query qry1 = eclipseLink_em.createQuery(ex.and(expBuilder.anyOf("eAttr.sample").equal("yyyy")), TestEntity.class);

       List l = qry1.getResultList();

      System.out.println("Query Size with EB:::" + l.size() + "  Value ::" + l.toString());

 

      em.close();

 

Exception [EclipseLink-6159] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.QueryException

Exception Description: Cannot find mapping for MapEntryExpression with base: [

Query Key eAttr.sample

   Base com.test.TestEntity].

Query: ReadAllQuery(referenceClass=TestEntity )

      at org.eclipse.persistence.exceptions.QueryException.noMappingForMapEntryExpression(QueryException.java:1456)

      at org.eclipse.persistence.internal.expressions.MapEntryExpression.getInterfaceContainerPolicy(MapEntryExpression.java:251)

      at org.eclipse.persistence.internal.expressions.MapEntryExpression.isAttribute(MapEntryExpression.java:233)

      at org.eclipse.persistence.internal.expressions.RelationExpression.isObjectComparison(RelationExpression.java:449)

      at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:475)

      at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)

      at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1300)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:491)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1553)

      at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:678)

      at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:625)

      at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:509)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:820)

      at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:470)

      at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:710)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1036)

      at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:380)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1122)

      at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2908)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1291)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1273)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1247)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:479)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:714)

      at com.ciena.test.TestJpa.main(TestJpa.java:354)

 

Exception in thread "main" Local Exception Stack:

Exception [EclipseLink-6159] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org.eclipse.persistence.exceptions.QueryException

Exception Description: Cannot find mapping for MapEntryExpression with base: [

Query Key eAttr.sample

   Base com.test.TestEntity].

Query: ReadAllQuery(referenceClass=TestEntity)

      at org.eclipse.persistence.exceptions.QueryException.noMappingForMapEntryExpression(QueryException.java:1456)

      at org.eclipse.persistence.internal.expressions.MapEntryExpression.getInterfaceContainerPolicy(MapEntryExpression.java:251)

      at org.eclipse.persistence.internal.expressions.MapEntryExpression.isAttribute(MapEntryExpression.java:233)

      at org.eclipse.persistence.internal.expressions.RelationExpression.isObjectComparison(RelationExpression.java:449)

      at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:475)

      at org.eclipse.persistence.internal.expressions.CompoundExpression.normalize(CompoundExpression.java:218)

      at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1300)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildNormalSelectStatement(ExpressionQueryMechanism.java:491)

      at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1553)

      at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:678)

      at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:625)

      at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:509)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:820)

      at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:470)

      at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:710)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1036)

      at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:380)

      at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1122)

      at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2908)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1291)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1273)

      at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1247)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:479)

      at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:714)

      at com.ciena.test.TestJpa.main(TestJpa.java:354)

 


_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top