Reputation: 299
Suppose that I have two Hibernate POJOs: Customer and Order. The Order table has three relevant columns: customerId, totalItemsSold, and totalCost. The Customer table has columns: customerId, firstName, lastName, and city.
I've mapped a many-to-one between the Order and Customer tables. My Order POJO contains a Customer object. I want to do the following Criteria query:
DetachedCriteria crit = DetachedCriteria.forClass(Order.class);
crit.add(Restrictions.eq("customer.city", "Chicago"));
getHibernateTemplate().findByCriteria(crit);
This always throws the following exception:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: customer.city of: Order
How can I use Criteria to search for Orders based on the customer's city? My Order pojo does contain a Customer object named "Customer" with appropriate getter and setters.....
Edit (Order/Customer Class) as requested:
public class Customer {
private Integer customerId;
private String firstName;
private String lastName;
private String city;
public Integer getCustomerId() { return customerId; }
public void setCustomerId(Integer customerId) { this.customerId = customerId; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
}
public class Order {
private Customer customer;
private Integer totalItemsSold;
private Integer totalCost;
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
public Integer getTotalItemsSold() { return totalItemsSold; }
public void setTotalItemsSold(Integer totalItemsSold) { this.totalItemsSold = totalItemsSold; }
public Integer getTotalCost() { return totalCost; }
public void setTotalCost(Integer totalCost) { this.totalCost = totalCost; }
}
Here is the Order class's hbm mapping (which contains the mapping between Customer and Order):
<many-to-one name="customer" class="Customer" column="customerId" />
Upvotes: 0
Views: 1470
Reputation: 49
If you are just searching by customerId then what you have :
DetachedCriteria crit = DetachedCriteria.forClass(Order.class);
crit.add(Restrictions.eq("customer.customerId", 12));
getHibernateTemplate().findByCriteria(crit);
might work. Otherwise Jeff and others mentioned above, you have to have the join with Customer table like mentioned
crit.createAlias("customer", "customer");
Upvotes: 1
Reputation: 3707
What you've posted seems fine. Maybe there is an error in your hbm file?
Without seeing the full hbm, I'd be tempted to explicitly tell your detached criteria about the customer relationship:
crit.createAlias("customer", "customer");
Upvotes: 2
Reputation: 24910
Is the Customer instance variable in your Order class called customer or customerId?
Upvotes: 0