首页 文章

Hibernate标准问题

提问于
浏览
0

假设我有两个Hibernate POJO:Customer和Order . Order表有三个相关列:customerId,totalItemsSold和totalCost . Customer表具有列:customerId,firstName,lastName和city .

我在Order和Customer表之间映射了多对一 . 我的订单POJO包含一个Customer对象 . 我想执行以下Criteria查询:

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”的Customer对象,带有适当的getter和setter .....

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; }
}

这是Order类的hbm映射(包含Customer和Order之间的映射):

<many-to-one name="customer" class="Customer" column="customerId" />

3 回答

  • 2

    你发布的内容似乎很好 . 也许你的hbm文件有错误?

    没有看到完整的hbm,我很想明确告诉你关于客户关系的超然标准:

    crit.createAlias("customer", "customer");
    
  • 1

    如果您只是按customerId搜索,那么您拥有的是:

    DetachedCriteria crit = DetachedCriteria.forClass(Order.class);
    crit.add(Restrictions.eq("customer.customerId", 12));
    getHibernateTemplate().findByCriteria(crit);
    

    可能有用 . 否则杰夫和上面提到的其他人,你必须像上面提到的那样加入Customer表

    crit.createAlias("customer", "customer");
    
  • 0

    Order类中的Customer实例变量是customer或customerId吗?

相关问题