【发布时间】:2011-08-13 07:31:28
【问题描述】:
假设我有两个 Hibernate POJO:Customer 和 Order。 Order 表具有三个相关列:customerId、totalItemsSold 和 totalCost。 Customer 表包含以下列:customerId、firstName、lastName 和 city。
我已经在 Order 和 Customer 表之间进行了多对一映射。我的订单 POJO 包含一个客户对象。我想做以下条件查询:
DetachedCriteria crit = DetachedCriteria.forClass(Order.class);
crit.add(Restrictions.eq("customer.city", "Chicago"));
getHibernateTemplate().findByCriteria(crit);
这总是抛出以下异常:
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: customer.city of: Order
如何使用 Criteria 根据客户所在城市搜索订单?我的订单 pojo 确实包含一个名为“Customer”的客户对象,并带有适当的 getter 和 setter .....
根据要求编辑(订单/客户类别):
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; }
}
这是 Order 类的 hbm 映射(包含 Customer 和 Order 之间的映射):
<many-to-one name="customer" class="Customer" column="customerId" />
【问题讨论】:
-
您可以发布您的订单和客户代码吗?
-
@Jeff - 他们已添加到我的帖子中
-
@David - 除了查询、异常消息和 POJO 代码之外,还请始终发布相关实体的完整 Hibernate 映射。谢谢