-->
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.  [ 7 posts ] 
Author Message
 Post subject: Detached query with filters on association fields
PostPosted: Sun Jun 03, 2007 12:51 pm 
Newbie

Joined: Sun Jun 03, 2007 12:46 pm
Posts: 5
Location: Milan, Italy
Hello,
I have a problem with a DetachedQuery, which seems to be in contrast with Hibernate's documentation.
I'm using Hibernate 3.2 + Spring integration, and my datamodel looks like this:

- class PersonData:
- id, etc...
- contactDetails: ContactDetails

- class ContactDetails:
- firstName, lastName, etc..

I have a PersonDao class with a findByName() method, but it throws an exception when I try to execute the query.

Code:
- CODE START -

public List<PersonData> findByName(String name, SortParameter[] sortParams,
        int first, int count) {

    DetachedCriteria query = DetachedCriteria.forClass(PersonData.class);
    DetachedCriteria contactDetails = query
            .createCriteria("contactDetails");

    SimpleExpression lastNameLike = Restrictions.like("lastName", name,
            MatchMode.START).ignoreCase();
    SimpleExpression firstNameLike = Restrictions.like("firstName", name,
            MatchMode.START).ignoreCase();
    contactDetails.add(Restrictions.disjunction().add(lastNameLike).add(
            firstNameLike));
   
    // adds all the sort parameters
    addSort(query, sortParams);

    return getHibernateTemplate().findByCriteria(query, first, count);
}

- CODE END -


Any ideas ?

_________________
www.megadix.it


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 2:54 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Post the error stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 03, 2007 6:28 pm 
Newbie

Joined: Sun Jun 03, 2007 12:46 pm
Posts: 5
Location: Milan, Italy
pepelnm wrote:
Post the error stack trace.


Oops, you're right, please forgive me :)
Here it is:

Code:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: lastName of: studiodix.model.PersonData; nested exception is org.hibernate.QueryException: could not resolve property: lastName of: studiodix.model.PersonData
Caused by: org.hibernate.QueryException: could not resolve property: lastName of: studiodix.model.PersonData
   at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
   at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1327)
   at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
   at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1302)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:394)
   at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
   at org.hibernate.criterion.Junction.toSqlString(Junction.java:58)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:334)
   at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:68)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
   at org.springframework.orm.hibernate3.HibernateTemplate$35.doInHibernate(HibernateTemplate.java:974)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
   at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:964)
   at studiodix.dao.PersonDao.findByName(PersonDao.java:42)

[...]


_________________
www.megadix.it


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 7:31 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
What happens if you comment addSort(query, sortParams); line?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 10:16 am 
Newbie

Joined: Sun Jun 03, 2007 12:46 pm
Posts: 5
Location: Milan, Italy
Sorry, no change.
addSort() is just a utility method that calls addOrder() if "sortParams" is not null.

Perhaps I've found the reason why it fails: I've mapped ContactDetails as a component...but the documentation says it can be treated as an ordinary association!!
Quote from Hibernate Reference, chapter 8.2:
"Composite elements may appear in queries using the same syntax as associations to other entities."

Uhm, I think that more details are needed. Here are the mapping files:

PersonData.hbm.xml

Code:
<hibernate-mapping package="studiodix.model">
  <class name="PersonData" table="`PERSON_DATA`">
    <id name="id" column="`PERSON_ID`" type="long">
      <generator class="native" />
    </id>
    <component name="contactDetails" class="ContactDetails">
      <property name="title" column="`TITLE`" type="string" length="16" />
      <property name="firstName" column="`FIRST_NAME`" type="string" length="100" />
      <property name="lastName" column="`LAST_NAME`" type="string" length="100" />
      <property name="email" column="`EMAIL`" type="string" length="100" />
      <property name="phone1" column="`PHONE1`" type="string" length="64" />
      <property name="phone2" column="`PHONE2`" type="string" length="64" />
    </component>
  </class>
</hibernate-mapping>



Moreover, if I add "contactDetails." to the restrictions:

Code:
SimpleExpression lastNameLike =
    Restrictions.like("contactDetails.lastName", name, MatchMode.START).ignoreCase();
SimpleExpression firstNameLike =
    Restrictions.like("contactDetails.firstName", name, MatchMode.START).ignoreCase();


I get this absurd error:

Code:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: contactDetails of: studiodix.model.User; nested exception is org.hibernate.QueryException: could not resolve property: contactDetails of: studiodix.model.User
Caused by: org.hibernate.QueryException: could not resolve property: contactDetails of: studiodix.model.User
    at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
    at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1310)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:204)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:191)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:81)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:59)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    at org.springframework.orm.hibernate3.HibernateTemplate$35.doInHibernate(HibernateTemplate.java:974)
    at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:964)
    at studiodix.dao.PersonDao.findByName(PersonDao.java:40)


I don't want to fallback to HQL, because I've already put in place a lot of utility code to leverage Detached query mechanism...

_________________
www.megadix.it


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 1:22 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
I thought contactDetails was an association.

So then, does this work?:
Code:
public List<PersonData> findByName(
    String name, SortParameter[] sortParams, int first, int count
) {

    DetachedCriteria query = DetachedCriteria.forClass(PersonData.class);

    SimpleExpression lastNameLike = Restrictions.like(
        "contactDetails.lastName", name, MatchMode.START
    ).ignoreCase();

    SimpleExpression firstNameLike = Restrictions.like(
        "contactDetails.firstName", name, MatchMode.START
    ).ignoreCase();

    query.add(Restrictions.disjunction().add(lastNameLike).add(firstNameLike));
   
    // adds all the sort parameters
    addSort(query, sortParams);

    return getHibernateTemplate().findByCriteria(query, first, count);
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 6:10 am 
Newbie

Joined: Sun Jun 03, 2007 12:46 pm
Posts: 5
Location: Milan, Italy
pepelnm wrote:
I thought contactDetails was an association.

So then, does this work?:
Code:
public List<PersonData> findByName(
    String name, SortParameter[] sortParams, int first, int count
) {

    DetachedCriteria query = DetachedCriteria.forClass(PersonData.class);

    SimpleExpression lastNameLike = Restrictions.like(
        "contactDetails.lastName", name, MatchMode.START
    ).ignoreCase();

    SimpleExpression firstNameLike = Restrictions.like(
        "contactDetails.firstName", name, MatchMode.START
    ).ignoreCase();

    query.add(Restrictions.disjunction().add(lastNameLike).add(firstNameLike));
   
    // adds all the sort parameters
    addSort(query, sortParams);

    return getHibernateTemplate().findByCriteria(query, first, count);
}



Unfortunately not. At this point I think that my problem is somewhat related to a deeply hidden error in my mapping files, and that Hibernate just gives back some random error message due to a very unlucky coincidence :(
Oh god I'll have to review ALL my mapping files just to spot this error...
Thank you anyway for your patience, I'll give you credits ;)

Dimitri

_________________
www.megadix.it


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