-->
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 API Query By Example with multiple associations
PostPosted: Tue Jul 05, 2005 3:49 pm 
Newbie

Joined: Tue Jul 05, 2005 3:23 pm
Posts: 2
Hello,

I am attempting to do a Query By Example (QBE) criteria using a class that has two many-to-one associations. I want to use QBE to place criteria on the class, and I want to use QBE to place criteria upon the two associated objects.

Hibernate version: 3.0.5

The class is called ContactEmployee and it is defined as follows:

Code:
public class ContactEmployee {

    private Long id;
    private Contact contact;
    private Employee employee;

    public Long getId() {
        return id;
    }

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

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}


and it's mapping file is:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class
        name="myPackage.ContactEmployee"
        table="CONTACT_EMPLOYEE"
    >

        <id
            name="id"
            column="contact_employee_id"
            type="java.lang.Long"
        >
            <generator class="native">
                <param name="sequence">uid_contact_employee</param>
            </generator>
        </id>

        <many-to-one
            name="contact"
            class="myPackage.Contact"
            cascade="save-update"
            fetch="select"
            update="true"
            insert="true"
            column="contact_id"
        />

        <many-to-one
            name="employee"
            class="myPackage.Employee"
            cascade="save-update"
            fetch="select"
            update="true"
            insert="true"
            column="employee_id"
        />

    </class>

</hibernate-mapping>


The online documentation gives an example of what I'm wanting to do, but only with one association:

Code:
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .createCriteria("mate")
        .add( Example.create( cat.getMate() ) )
    .list();


I am trying to figure out how to do the same thing above, but in my case, in addition to the contact employee example, I want to add an example for the contact AND for the employee.

I have tried:

Code:
        Criteria criteria = getSession().createCriteria(ContactEmployee.class)
                .add(Example.create(contactEmployee))
                .createCriteria("contact").add(Example.create(contactEmployee.getContact()))
                .createCriteria("employee").add(Example.create(contactEmployee.getEmployee()));



which gives the following exception:

Code:
org.hibernate.QueryException: could not resolve property: employee of: myPackage.Contact
   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.BasicEntityPersister.toType(BasicEntityPersister.java:1094)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:188)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:175)
   at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:81)
   at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:69)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
   at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
   at org.hibernate.impl.CriteriaImpl$Subcriteria.list(CriteriaImpl.java:142)
....


The exception makes sense to me because I see where my code is specifying that the employee association is under the contact association. But what I am wanting to do is to specify that the contact and the employee are both under the contact employee, and I just can't figure out how to do this. So instead of:

ContactEmployee --> Contact --> Employee

I want

ContactEmployee --> Contact

and

ContactEmployee --> Employee

I have made other attempts with the Criteria API to achieve this and have searched around in the forums, but haven't found a clear answer to this question yet. Thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 7:49 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
You can't use QBE to query associations. Further, you only ever need to use one Example per criteria query to capture all your basic properties. You add associations on top of that using restrictions.

For example:

session.createCriteria(Balloon.class)
.add(Example.create(aBalloon))
.add(Restrictions.eq("foo", foo))
.add(Restrictions.eq("bar", bar))


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 11:00 am 
Newbie

Joined: Tue Jul 05, 2005 3:23 pm
Posts: 2
Actually, according to the Hibernate documentation, you can use QBE to query associations:

http://www.hibernate.org/hib_docs/v3/reference/en/html/querycriteria.html#querycriteria-examples

Code:
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .createCriteria("mate")
        .add( Example.create( cat.getMate() ) )
    .list();


And in my example, when I do:

Code:
        Criteria criteria = getSession().createCriteria(ContactEmployee.class)
                .add(Example.create(contactEmployee))
                .createCriteria("contact").add(Example.create(contactEmployee.getContact()))


that works. I can even query an association under the Contact object in my example using another QBE:

Code:
        Criteria criteria = getSession().createCriteria(ContactEmployee.class)
                .add(Example.create(contactEmployee))
                .createCriteria("contact").add(Example.create(contactEmployee.getContact()))
                .createCriteria("account").add(Example.create(contactEmployee.getContact().getAccount()));


The problem occurs when trying to use QBE to query multiple associations at the same "level". It's as if I can only navigate one path of the object graph using QBE.


Top
 Profile  
 
 Post subject: Same problem
PostPosted: Tue Jul 19, 2005 3:43 pm 
Newbie

Joined: Tue Jul 19, 2005 3:38 pm
Posts: 1
Hi there, i´m having the same problem when trying to search into more than one object belonging to the filtered class.

Is it a limitation of Hibernate or there is a way to get it working?

Thanks in advance,

Ignacio


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 4:47 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
dpmeyer wrote:
....
....
The problem occurs when trying to use QBE to query multiple associations at the same "level". It's as if I can only navigate one path of the object graph using QBE.


I think you can do it like this... although I haven't tested this exact scenario.

Code:
        Criteria criteria = getSession().createCriteria(ContactEmployee.class,"employee")
                .add(Example.create(contactEmployee))
                .createCriteria("contact").add(Example.create(contactEmployee.getContact()))
                .createCriteria("employee","account").add(Example.create(contactEmployee.getContact().getAccount()));


First you define an alias for the ContactEmployee criteria and then you use this as the "root" alias for account.


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.