Hibernate joining two tables

I am very new to hibernate and this question might seem a little elementary, but here it goes:

I have a class

public List<PurchaseOrder> getPurchaseOrders

with a method that creates the query in question:

public Object doInHibernate(Session session)

throws HibernateException, SQLException{

Criteria query = session.createCriteria(PurchaseOrder.class);

I am able to query on anything inside the PurchaseOrder table.

However I would like to also query on the column suffix from the Office table.

I do not know if this requires any changes to the PurchaseOrder.hbm.xml or possibly even the Office.hbm.xml

I already have the foreign key indicated in PurchaseOrder.hbm.xml :

<!-- Office -->

<many-to-one name="office"

class="com.***.model.businessobject.Office"

column="OfficeID_FK" not-null="true" />

I tried this:

query.createCriteria("office","offices");

query.add(Expression.eq("suffix", suffix_substring));

When I add this to the query I get:

10:58:54,453 ERROR PurchaseOrderServiceImpl:163 - Error getting getPurchaseOrders with criteria

org.springframework.orm.hibernate.HibernateQueryException: could not resolve property: suffix of: com.***.model.businessobject.PurchaseOrder; nested exception is net.sf.hibernate.QueryException: could not resolve property: suffix of: com.***.model.businessobject.PurchaseOrder

net.sf.hibernate.QueryException: could not resolve property: suffix of: com.***.model.businessobject.PurchaseOrder

I realize suffix comes from com.***.model.businessobject.Office, how do I indicate that.

Any help would be appreciated

Dan

[2000 byte] By [dv8dana] at [2007-10-2 22:39:05]
# 1
Have you mapped the suffix property of the Office class?Note that you're querying objects, not tables. Unless Hibernate is told that the suffix column is mapped to a suitably named property, it has no way of determining this!
dcmintera at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Right now the office class which includes the suffix private property looks like this:

public class Office extends BaseBusinessObject {

private String description;

private Address address;

private Address mailAddress;

private String phone;

private String fax;

private String email;

private String suffix;

public String getSuffix() {

return this.suffix;

}

public void setSuffix(String suffix) {

this.suffix = suffix;

}

This was all in place before I began work for this project. What other steps would i have to follow to make sure that the office property is mapped properly and so that when i'm querying on the Purchaseorder table I can access the office suffix. Is the foreign key mapping to the office table in the hbm enough or do I have to set up some kind of an object inside the hbm which points directly at the suffix.

Message was edited by:

dv8dan

dv8dana at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

You need a mapping entry for the Office class. Do you have one? It will look something very approximately like this, and it might have its own Office.hbm.xml file, or it might be in another monolithic file:

<class name="foo.bar.Office">

<id type="int" name="id">

<generator class="assigned"/>

</id>

<property name="description" type="string"/>

...

<property name="suffix" type="string" column="suffix"/>

</class>

dcmintera at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Office.hbm.xml:

<hibernate-mapping>

<class name="com.***.model.businessobject.Office"

table="Office">

<id name="id" type="integer" unsaved-value="0">

<column name="OfficeID_PK" not-null="true" />

<generator class="identity" />

</id>

<property name="suffix" type="string" column="Suffix" not-null="false" />

dv8dana at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

I need to check this in Hibernate 2, which I don't currently have set up and running on my laptop - however, given how Criteria work in Hibernate 3 I suspect what you're doing wrong is applying the equals criteria to the wrong Criteria object.

So given this (is the createCriteria method really on the Query interface!? I could have sworn it was on Session even in H2)

query.createCriteria("office","offices");

query.add(Expression.eq("suffix", suffix_substring));

I think what you actually want is this:

query = query.createCriteria("office","offices");

query.add(Expression.eq("suffix", suffix_substring));

Certainly in H3 the appropriate query would be something like:

Criteria crit1 = session.createCriteria(PurchaseOrder.class);

Criteria crit2 = crit1.createCriteria("office","offices");

Criteria crit3 = crit2.add(Expression.eq("suffix","foo"));

List list = crit3.list();

dcmintera at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

Thank you , that last solution worked:

My code now appears this way:

if(is_account_string==true && is_suffix_included==true){

Criteria query_2 = query.createCriteria("office","offices");

Criteria query_3 = query_2.add(Expression.eq("suffix",suffix_substring));

return query_3.list();

}

else

{

return query.list();

}

dv8dana at 2007-7-14 1:56:19 > top of Java-index,Other Topics,Patterns & OO Design...