-->
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: Criteria API
PostPosted: Wed Sep 29, 2004 2:02 am 
Newbie

Joined: Thu Sep 23, 2004 4:11 am
Posts: 12
Location: Gold Coast, Australia
Hibernate version: 2.0

I have the following relational mapping:
"A Property has an Address which has a Postcode which has a locality (of type String)".
These relationships are mapped using a many-to-one.
I'm trying to construct a query to select Property types by using the Criteria API (Chapter 12 of the Reference)
and specifying a value for the postcode. All of my attempts so far have failed.

Here is an example of what I am trying to achieve unsuccessfully:

Code:
Criteria crit = session.createCriteria(Property.class);
crit.createAlias("address", "a")
.add(Expression.eq("a.postcode.locality", "The Locality"));


This results in the following error:
Quote:
net.sf.hibernate.QueryException: could not resolve property: postcode.locality of: com.tmits.web.re.dao.Address.


I've tried everything that I can think of to achieve a solution.
Assistance is most appreciated.


Appendix
--------

Using XDoclet, the code looks something like the following.
It has been trimmed for brevity (this question is already long) but effort has made to not trim appropriate parts.
If anyone feels it is appropriate to construct a complete test case and leave it unaltered, I will do so.

Code:
public class Property
{
    private Long id;
    private Address address;

    public Property()
    {
        this(null);
    }

    public Property(Long id)
    {
        this.id = id;
    }

    /**
     * Returns the id attribute of this property.
     * This attribute is used as a unique identifier.
     *
     * @return The id attribute of this property.
     * @hibernate.id generator-class="native"
     */
    public final Long getId()
    {
        return id;
    }

    public final void setId(Long id)
    {
        this.id = id;
    }

    /**
     * Returns the address attribute of this property.
     *
     * @return The address attribute of this property.
     * @hibernate.many-to-one name="address"
     */
    public final Address getAddress()
    {
        return address;
    }

    public final void setAddress(Address address)
    {
        this.address = address;
    }
}

/**
* A Data Access Object (DAO) that represents an address.
*/
public class Address
{
    private Long id;
    private Postcode postcode;

    public Address()
    {
        this(null);
    }

    public Address(Long id)
    {
        this.setId(id);
    }

    /**
     * Returns the id attribute of this address.
     * This attribute is used as a unique identifier.
     *
     * @return The id attribute of this address.
     * @hibernate.id generator-class="native"
     */
    public final Long getId()
    {
        return id;
    }

    public final void setId(Long id)
    {
        this.id = id;
    }

    /**
     * Returns the postcode attribute of this address.
     *
     * @return The postcode attribute of this address.
     * @hibernate.many-to-one name="postcode"
     */
    public final Postcode getPostcode()
    {
        return postcode;
    }

    public final void setPostcode(Postcode postcode)
    {
        this.postcode = postcode;
    }
}


/**
* A Data Access Object (DAO) that represents a postcode.
*/
public class Postcode
{
    private Long id;
    private String locality;

    public Postcode()
    {
        this(null);
    }

    public Postcode(Long id)
    {
        this.setId(id);
    }

    /**
     * Returns the id attribute of this postcode.
     * This attribute is used as a unique identifier.
     *
     * @return The id attribute of this postcode.
     * @hibernate.id generator-class="native"
     */
    public final Long getId()
    {
        return id;
    }

    public final void setId(Long id)
    {
        this.id = id;
    }

    /**
     * Returns the locality attribute of this postcode.
     *
     * @return The locality attribute of this postcode.
     * @hibernate.property
     * @hibernate.column name="locality"
     */
    public final String getLocality()
    {
        return locality;
    }

    public final void setLocality(String locality)
    {
        this.locality = locality;
    }
}

_________________
SCJP 1.4, SCJD
Software Engineer, IBM Australia


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 29, 2004 7:49 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
You need to use the createCriteria() method to navigate the assocations, the Criteria API doesn't support nested properties (except for very specific examples like "id").

You'll need something like:
Code:
Criteria crit = session.createCriteria(Property.class);
crit.createCriteria("address").createCriteria("postcode").add(Expression.eq("locality", "The Locality"));


Be aware that you can't order-by or eager-fetch using attributes of the nested criteria. It's not stated clearly in the reference doco, but you need to be aware of it when you're decideing whether or not to use the criteria API for search-page type functionality. The criteria API looks like a good fit at first, but the lack of ordering/fetching support for nested criteria, plus the inability to count the result set without iterating it mean that you'll eventually need to drop back to HQL or SQL for any sufficiently complex searching.

I don't know if the createAlias() call you had needed to be replaced, I'm very iffy on what createAlias() actually does - it's not documented terribly well. So I usually just use createCrtieria().

_________________
Cheers,
Shorn.


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.