-->
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.  [ 3 posts ] 
Author Message
 Post subject: Using criterion to search related objects
PostPosted: Fri Jul 08, 2005 8:40 am 
Newbie

Joined: Thu Jul 07, 2005 12:21 pm
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:2.1.7

Mapping documents:

<class name="CodedRecordImpl" table="business_directory_coded">
<id name="Id" type="int" column="ID">
<generator class="assigned"/>
</id>
<property name="company_name" column="company_name" type="string" length="50"/>
<property name="address1" column="address1" type="string" length="150"/>
<property name="address2" column="address2" type="string" length="150"/>
<property name="address3" column="address3" type="string" length="150"/>
<property name="town" column="town" type="string" length="50"/>
<property name="county" column="county" type="string" length="50"/>
<property name="postcode" column="postcode" type="string" length="10"/>
<property name="web" column="web" type="string" length="150"/>
<property name="telephone" column="telephone" type="string" length="15"/>
<property name="postsect" column="postsect" type="string" length="10"/>
<property name="postdist" column="postdist" type="string" length="5"/>
<property name="postarea" column="postarea" type="string" length="2"/>
<property name="la_2002" column="la_2002" type="string" length="50"/>
<property name="CTPS" column="CTPS" type="char"/>
<property name="TPS" column="TPS" type="char"/>
<property name="FPS" column="FPS" type="char"/>
<property name="MPS" column="MPS" type="char"/>
<many-to-one name="primaryDescriptionDetails" class="PrimaryDescriptionImpl" column="sic_primary_code"/>
<many-to-one name="secondaryDescriptionDetails" class="SecondaryDescriptionImpl" column="sic_secondary_code"/>
<many-to-one name="tertiaryDescriptionDetails" class="TertiaryDescriptionImpl" column="sic_tertiary_code"/>
<many-to-one name="thomsonDescriptionDetails" class="ThomsonDescriptionImpl" column="thomson_code"/>
<many-to-one name="ward2004Details" class="Ward2004Impl" column="ward_2004"/>
</class>


<class name="PrimaryDescriptionImpl" table="1992_sic_primary_level1_description" lazy="true">
<id name="primary_code" type="char" column="primary_code">
<generator class="assigned"/>
</id>
<property name="primary_description" column="primary_description" type="string" length="150" />
</class>

<class name="SecondaryDescriptionImpl" table="1992_sic_secondary_description" lazy="true">
<id name="secondary_code" type="string" column="secondary_code">
<generator class="assigned"/>
</id>
<property name="secondary_description" column="secondary_description" type="string" length="150"/>
</class>

<class name="TertiaryDescriptionImpl" table="1992_sic_tertiary_description" lazy="true">
<id name="tertiary_code" type="string" column="tertiary_code">
<generator class="assigned"/>
</id>
<property name="tertiary_description" column="tertiary_description" type="string" length="150"/>
</class>

<class name="ThomsonDescriptionImpl" table="lu_thomson" lazy="true">
<id name="thomson_code" type="string" column="thomson_code">
<generator class="assigned"/>
</id>
<property name="thomson_description" column="thomson_description" type="string" length="150"/>
</class>

<class name="Ward2004Impl" table="lu_ward_2004" lazy="true">
<id name="ward_2004" type="string" column="ward_2004">
<generator class="assigned"/>
</id>
<property name="ward_2004_name" column="ward_2004_name" type="string" length="50"/>
<property name="la_2002" column="la_2002" type="string" length="50"/>
</class>

<query name="codedrecord.checkprimary">from PrimaryDescriptionImpl where primary_code = ?</query>
<query name="codedrecord.checksecondary">from SecondaryDescriptionImpl where secondary_code = ?</query>
<query name="codedrecord.checktertiary">from TertiaryDescriptionImpl where tertiary_code = ?</query>
<query name="codedrecord.checkthomson">from ThomsonDescriptionImpl where thomson_code = ?</query>
<query name="codedrecord.checkward">from Ward2004Impl where ward_2004 = ?</query>
<query name="codedrecord.clear">delete from CodedRecordImpl</query>

<query name="codedrecord.search.companyname">from CodedRecordImpl where company_name like ?</query>
<query name="codedrecord.search.pcode">from CodedRecordImpl where sic_primary_code like ?</query>
<query name="codedrecord.search.ward">from CodedRecordImpl where ward_2004 like ?</query>


<query name="primarydescription.getall">from PrimaryDescriptionImpl order by primary_description</query>

<query name="ward.getall">from Ward2004Impl order by ward_2004_name</query>

</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():

Session ses=super.getSession();
Criteria crit= ses.createCriteria(CodedRecord.class);


if(titleSearch!=null)crit.add( Expression.like("company_name",titleSearch ) );
if(addressSearch!=null)crit.add( Expression.like( "address1", "%"+addressSearch+"%" ) );
if(addressSearch!=null)crit.add( Expression.like( "address2", "%"+addressSearch+"%" ) );
if(addressSearch!=null)crit.add( Expression.like( "address3", "%"+addressSearch+"%" ) );
if(wardSearch!=null)crit.add( Expression.like( "ward_2004", wardSearch ) );
if(primarySearch!=null)crit.add( Expression.like( "sic_primary_code", primarySearch ) );

crit.addOrder( Order.asc("company_name"));
crit.setMaxResults(1000);

ArrayList al =(ArrayList) crit.list();



Full stack trace of any exception that occurs:
net.sf.hibernate.QueryException: could not resolve property: sic_primary_code of: com.scientia.busdir.beans.CodedRecordImpl
at net.sf.hibernate.persister.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:50)
at net.sf.hibernate.expression.AbstractCriterion.getColumns(AbstractCriterion.java:42)
at net.sf.hibernate.expression.SimpleExpression.toSqlString(SimpleExpression.java:40)
at net.sf.hibernate.loader.CriteriaLoader.<init>(CriteriaLoader.java:64)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3630)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:238)
at com.scientia.dao.hibernate.CodedRecordDAOImpl.AdvancedSearch(CodedRecordDAOImpl.java:107)


The search works fine when i preform it on the fields of CodedRecordImpl, but when i try to search on one of the fields that relates to a relationship i get the above problem...what i am i going wroung?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 8:57 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Firstly, you should be using Query By Example for this.

Secondly, explain what you believe should be the result of applying 'like' string matching to an entity association.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 08, 2005 9:04 am 
Newbie

Joined: Thu Jul 07, 2005 12:21 pm
Posts: 6
1
Query By Example for this? sorry what is this?


2
I agree my use of like here is incorrect, i think i assumed that if this worked:

<query name="codedrecord.search.pcode">from CodedRecordImpl where sic_primary_code like ?</query>


the above should also work. I want it to return a codedRecord when the mapped PrimaryDescriptionImpl has the primary_code of the query.


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