-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Using Criteria with a Collection of Components
PostPosted: Mon Jun 04, 2012 3:22 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
I am attempting to create a query against an object based off a collection of components on the object using the Criteria API, but I am not having any luck.

ManufacturerInfo Hibernate Mapping:
Code:
<set name="warranties" table="MANU_INFO_WARRANTY" node="warranties"
         embed-xml="true" lazy="true" cascade="all-delete-orphan">
         <key column="MANU_INFO_ID" />
         <composite-element class="Warranty">
            <property name="warrantyId" type="java.lang.String" column="WARRANTY_ID"
               length="200" />
            <property name="duration" type="java.lang.Float" column="DURATION"
               precision="22" scale="4" />
         </composite-element>
</set>


According to the documentation provided here. It says that you can treat a composite-element the same as a regular assocation, however I get an exception when I attempt that. Below are the test cases I have tried to get this working.

Code:
@Test
  public void testComponentCollection1() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    criteria.add(Restrictions.eq("elements.warrantyId", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.QueryException: could not resolve property: elements of: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
   at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1482)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentCollection1(SearchCriteriaTests.java:99)
 
  @Test
  public void testComponentcollection1a() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    criteria.add(Restrictions.eq("elements", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.QueryException: could not resolve property: elements of: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
   at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1482)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentcollection1a(SearchCriteriaTests.java:108)

  @Test
  public void testComponentCollection2() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    criteria.add(Restrictions.eq("warrantyId", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.QueryException: could not resolve property: warrantyId of: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
   at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1482)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:62)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1457)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentCollection2(SearchCriteriaTests.java:117)
 
  @Test
  public void testComponentCollection3() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    Criteria subcriteria = criteria.createCriteria("warranties");
    criteria.add(Restrictions.eq("warrantyId", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.MappingException: collection was not an association: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo.warranties
   at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:470)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:238)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:222)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:108)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:83)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentCollection3(SearchCriteriaTests.java:127)
   
 
  @Test
  public void testComponentCollection4() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    Criteria subcriteria = criteria.createCriteria("warranties");
    criteria.add(Restrictions.eq("elements", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.MappingException: collection was not an association: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo.warranties
   at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:470)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:238)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:222)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:108)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:83)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentCollection4(SearchCriteriaTests.java:137)
   

  @Test
  public void testComponentCollection5() throws Exception
  {
    Criteria criteria = session.createCriteria(ManufacturerInfo.class);
    Criteria subcriteria = criteria.createAlias("warranties", "warranty");
    criteria.add(Restrictions.eq("warranty.warrantyId", "M1"));
    List results = criteria.list();
    Assert.assertTrue(results.size() > 1);
  }

returns:
org.hibernate.MappingException: collection was not an association: com.lmco.sts.lss.cla.api.inventory.ManufacturerInfo.warranties
   at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:470)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:238)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:222)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:108)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:83)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
   at com.lmco.lss.test.unit.tests.impl.search.SearchCriteriaTests.testComponentCollection5(SearchCriteriaTests.java:147)
   


I understand why 1, 1a, and 2 failed, however I don't understand why the latter attempts failed. Do I have the collection defined improperly on the ManufacturerInfo mapping?

The documentation for this comes from the 4.1 version. Is it possible that hibernate fixed this in a version newer than 3.6 and still referred to it as being legacy? That was the only place I was able to find anything on querying a component collection using the Criteria API.

I also understand that the Criteria API is being deprecated in favor of the CriteriaQuery object, however I am unable to make that transition that at this point in time.


Top
 Profile  
 
 Post subject: Re: Using Criteria with a Collection of Components
PostPosted: Thu Jun 07, 2012 12:54 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
Sorry for the bump, but I really could use an answer on this issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.