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.