-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria can't see composite-element property in map
PostPosted: Tue Jul 26, 2005 5:04 pm 
Newbie

Joined: Tue Jul 26, 2005 3:36 pm
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.8

Mapping documents:
Quote:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.idiominc.gm.datamodel.TextString" table="TEXT_STRING">
<meta attribute="class-description">
Represents a text strings row in the database.
@author WAS
</meta>

<id name="sysKey" type="int" column="TEXT_STRING_SYSKEY">
<meta attribute="scope-set">public</meta>
<generator class="increment"/>
</id>
.
.

<map name="names" table="TEXT_STRING_STR">
<key column="TEXT_STRING_SYSKEY" foreign-key="false"/>
<index column="LANG_SYSKEY" type="int" />
<composite-element class="com.idiominc.gm.datamodel.TextStringDataPair">
<property name="body" column="TEXT_STRING_BODY" not-null="true" />
<property name="abbr" column="TEXT_STRING_ABBR" />
</composite-element>
</map>


<!--<many-to-one name="searchStrings"
class="com.idiominc.gm.datamodel.TextStringStrings"
column="TEXT_STRING_SYSKEY" insert="false" update="false" foreign-key="false" /> -->

.
.
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Quote:
Session hbSession = HibernateUtil.getSession();
Criteria searchCriteria = hbSession.createCriteria(TextString.class);
searchCriteria.add( Expression.eq("approvalStatus.sysKey", status));
searchCriteria.add(Expression.ilike("names.abbr", abbrev));
try
{
log.debug(loc + "doing search");
textStrings = searchCriteria.list(); // get the TextStrings
return textStrings;
}
catch (HibernateException e)
{
log.error(loc + ":"+ e.toString());
throw new GmDataAccessException(loc, e);
}
}


Full stack trace of any exception that occurs:
Quote:
16:01:59,642 ERROR gm:131 - findTextStringObjects() :net.sf.hibernate.QueryException: could not resolve property: names.
abbr of: com.idiominc.gm.datamodel.TextString
<Jul 26, 2005 4:01:59 PM EDT> <Error> <HTTP> <BEA-101017> <[ServletContext(id=20704708,name=ws,context-path=/ws)] Root c
ause of ServletException.
java.lang.NullPointerException
at com.idiominc.gm.struts.action.objectstatus.SelectObjectsAction.executePerform(SelectObjectsAction.java:135)
at com.idiominc.gm.struts.action.GMBaseAction.execute(GMBaseAction.java:35)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1006)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:419)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:315)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6718)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3764)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2644)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:219)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:178)



Name and version of the database you are using:
Oracle 9.2.0.4.0
The generated SQL (show_sql=true):
Quote:
16:44:07,722 INFO Environment:483 - Hibernate 2.1.8
16:44:07,722 INFO Environment:512 - hibernate.properties not found
16:44:07,722 INFO Environment:543 - using CGLIB reflection optimizer
16:44:07,722 INFO Environment:572 - using JDK 1.4 java.sql.Timestamp handling
16:44:07,738 INFO Configuration:909 - configuring from resource: /hibernate.cfg.xml
16:44:07,738 INFO Configuration:881 - Configuration resource: /hibernate.cfg.xml
16:44:10,113 INFO Configuration:332 - Mapping resource: com\idiominc\gm\datamodel\TextString.hbm.xml
16:44:10,144 INFO Binder:229 - Mapping class: com.idiominc.gm.datamodel.TextString -> TEXT_STRING
16:44:10,144 INFO Binder:571 - Mapping collection: com.idiominc.gm.datamodel.TextString.names -> TEXT_STRING_STR
16:44:11,597 INFO Configuration:675 - processing foreign key constraints
16:44:11,707 INFO Dialect:86 - Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
16:44:11,738 INFO SettingsFactory:74 - Use outer join fetching: true
16:44:11,769 INFO NamingHelper:26 - JNDI InitialContext properties:{}
16:44:11,769 INFO DatasourceConnectionProvider:51 - Using datasource: GmDevDB
16:44:11,769 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
16:44:11,800 INFO TransactionManagerLookupFactory:38 - instantiating TransactionManagerLookup: net.sf.hibernate.transaction.WeblogicTransactionManagerLookup
16:44:11,800 INFO TransactionManagerLookupFactory:42 - instantiated TransactionManagerLookup
16:44:12,128 INFO SettingsFactory:114 - Use scrollable result sets: true
16:44:12,144 INFO SettingsFactory:117 - Use JDBC3 getGeneratedKeys(): false
16:44:12,144 INFO SettingsFactory:120 - Optimize cache for minimal puts: false
16:44:12,144 INFO SettingsFactory:126 - echoing all SQL to stdout
16:44:12,144 INFO SettingsFactory:129 - Query language substitutions: {}
16:44:12,144 INFO SettingsFactory:140 - cache provider: net.sf.hibernate.cache.EhCacheProvider
16:44:12,160 INFO Configuration:1130 - instantiating and configuring caches
16:44:12,300 INFO SessionFactoryImpl:119 - building session factory
16:44:14,379 INFO SessionFactoryObjectFactory:86 - Factory name: HibernateFactory
16:44:14,379 INFO NamingHelper:26 - JNDI InitialContext properties:{}
16:44:14,410 INFO SessionFactoryObjectFactory:91 - Bound factory to JNDI name: HibernateFactory
16:44:14,410 WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
16:44:14,410 INFO NamingHelper:26 - JNDI InitialContext properties:{}
16:44:14,535 DEBUG gm:45 - * gotSess Context: Sess=[net.sf.hibernate.impl.SessionImpl@196722d ]Thread [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] is open, tx=null, lang=null.
16:44:14,550 DEBUG gm:76 - * began Context: Sess=[net.sf.hibernate.impl.SessionImpl@196722d ]Thread [ExecuteThread: '14' for queue: 'weblogic.kernel.Default'] is open, tx=net.sf.hibernate.transaction.JDBCTransaction@17a82f1 isActive, lang=null.
16:44:15,410 DEBUG gm:79 - testCriteria: find TEXT_STRINGs.
16:44:15,425 DEBUG gm:106 - findTextStringObjects() parameters are:
16:44:15,441 DEBUG gm:107 - id: 1, status:3
16:44:15,441 DEBUG gm:108 - createdBy:JSINGER, modifiedBy:JSINGER
16:44:15,441 DEBUG gm:109 - TextStringQualifier: 4
16:44:15,441 DEBUG gm:110 - abbrev:fs%, text:first%
16:44:15,441 DEBUG gm:125 - findTextStringObjects() doing search
16:44:15,457 ERROR gm:131 - findTextStringObjects() :net.sf.hibernate.QueryException: could not resolve property: names.abbr of: com.idiominc.gm.datamodel.TextString

Query sql shown is mostly from a previous query in my application.

Debug level Hibernate log excerpt:

It seems that hibernate does not allow me to access properties of the component object TextStringDataPair that is in the map called "names" in a Criterion: e.g.
Quote:
searchCriteria.add(Expression.ilike("names.abbr", abbrev));
.

The TEXT_STRING_STR table looks like this:
Quote:
CREATE TABLE TEXT_STRING_STR
(
TEXT_STRING_SYSKEY NUMBER NOT NULL,
LANG_SYSKEY NUMBER NOT NULL,
TEXT_STRING_BODY VARCHAR2(1000 BYTE) NOT NULL,
TEXT_STRING_ABBR VARCHAR2(200 BYTE)
)


We are currently doing internationalization by taking the map and getting the string we want by names.get(langId) kind of coding.

I have found that by creating a separate hibernate mapping, "TextStringStrings", that I can create a criterion; however, this breaks a lot of code that we already have that retrieves the map and does the get(langId).

Any ideas how I can use Criteria to access the composite-element properties that are in the "names" map? Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 3:47 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hello,

Have you found an answer to your question? Or does anyone else know how to search by criteria on a map. I am having the exact same problem for the same need but with hibernate 3.0. Here is what I would like to do:
Code:
             DetachedCriteria crit = DetachedCriteria.forClass(Product.class);
             crit.add(Expression.like("productLocalizations.name", "a%"));
             instances = crit.getExecutableCriteria(session).list();


for this type of mapping:

Code:
...
       <map
            name="productLocalizations"
            table="PRODUCT_LOCALE"
            lazy="false"
            sort="org.dataisland.primitives.util.LocaleComparator"
            cascade="none"
            >

            <key
                column="PRODUCT_FK"
                >
            </key>

            <index
                column="LOCALE"
                type="locale"
                />

            <composite-element
                class="org.mdarad.samples.petstore.entities.ProductLocalization"
                >
                <property
                    name="name"
                    type="string"
                    update="true"
                    insert="true"
                    >
                    <column
                        name="NAME"
                        />
                </property>

                <property
                    name="description"
                    type="string"
                    update="true"
                    insert="true"
                    >
                    <column
                        name="DESCRIPTION"
                        />
                </property>

            </composite-element>

        </map>
...


As you can see, I want to search by criteria on the name attribute which is under the productLocalizations map wrapper.

Any ideas or suggestions would be greatly appreciated,

Thanks

François

PS: here is the error message I have right now:

Quote:
[java] 1) testLocalizationCriterion(org.mdarad.samples.petstore.struts.product.ProductTestCase)org.hibern
ate.QueryException: could not resolve property: productLocalizations.name of: org.mdarad.samples.petstore.enti
ties.Product
[java] at org.hibernate.persister.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapp
ing.java:43)
[java] at org.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:63)
[java] at org.hibernate.persister.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.jav
a:33)
[java] at org.hibernate.persister.BasicEntityPersister.toColumns(BasicEntityPersister.java:1053)
[java] at org.hibernate.loader.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:392)
[java] at org.hibernate.loader.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTransla
tor.java:360)
[java] at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:42)
[java] at org.hibernate.loader.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java
:305)
[java] at org.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:91)
[java] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1248)
[java] at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:294)
[java] at org.mdarad.samples.petstore.struts.product.ProductTestCase.testLocalizationCriterion(Produc
tTestCase.java:98)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 3:58 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
Hi again,

I have also tried the following :
Code:
             DetachedCriteria crit = DetachedCriteria.forClass(Product.class);
             crit.createCriteria("productLocalizations", "pl")
                     .add(Restrictions.like("productLocalizations.name", "a%"));


which returns the following error:

Quote:
[java] 1) testLocalizationCriterion(org.mdarad.samples.petstore.struts.product.ProductTestCase)org.hibern
ate.MappingException: collection was not an association: org.mdarad.samples.petstore.entities.Product.productL
ocalizations
[java] at org.hibernate.type.CollectionType.getAssociatedEntityName(CollectionType.java:284)
[java] at org.hibernate.loader.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java
:189)
[java] at org.hibernate.loader.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTrans
lator.java:173)
[java] at org.hibernate.loader.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:79)
[java] at org.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:68)
[java] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1248)
[java] at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:294)
[java] at org.mdarad.samples.petstore.struts.product.ProductTestCase.testLocalizationCriterion(Produc
tTestCase.java:101)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)


I don't know if this will help someone solve my problem but if so...great!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 3:04 pm 
Beginner
Beginner

Joined: Wed Jul 14, 2004 11:51 am
Posts: 43
I haven't figured out how to do it with the criteria api. If someone knows how PLEASE let me know how.

The following thread explains how to do it with HQL though:

http://forum.hibernate.org/viewtopic.php?t=928056&highlight=illegally+dereferenced+collection+element

That's what I will use for now but rather use the criteria api.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 2:32 am 
Beginner
Beginner

Joined: Fri Feb 11, 2005 12:03 pm
Posts: 48
Location: Kiel, Germany
According to the reference

http://www.hibernate.org/hib_docs/reference/en/html_single/#querycriteria-associations

this should be the right way:

frer wrote:
Code:
             DetachedCriteria crit = DetachedCriteria.forClass(Product.class);
             crit.createCriteria("productLocalizations", "pl")
                     .add(Restrictions.like("productLocalizations.name", "a%"));



Perhaps with this criteria:
Code:
             DetachedCriteria crit = DetachedCriteria.forClass(Product.class);
             crit.createCriteria("productLocalizations")
                     .add(Restrictions.like("name", "a%"));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.