Hibernate NewBie:多关联类的标准查询

时间:2013-06-21 13:51:42

标签: hibernate java-ee associations

我有两个班级:

@Entity
@Table(name = "Appeal_Header")
public class AppealHeader implements Serializable {

      private static final long serialVersionUID = -8402922611571578104L;

      @Id
      @Column(name = "Appeal_Header_Key", unique=true, nullable=false)
      private long id;

      @Column(name = "customer_cd", unique=true, nullable=false)
      private String customer;

      @ManyToOne(fetch=FetchType.EAGER)
      @JoinColumn(name="hes_invoice_header_id", insertable=false)
      private BillingHeader billingHeader;

      public long getId() {
          return id;
       }

      public void setId(long id) {
          this.id = id;
      }

      public BillingHeader getBillingHeader() {
          return billingHeader;
      }

      public void setBillingHeader(BillingHeader billingHeader) {
          this.billingHeader = billingHeader;
      }

      public String getCustomer() {
          return customer;
      }

      public void setCustomer(String customer) {
          this.customer = customer;
      }

}


@Entity
@Table(name = "billing_header")
public class BillingHeader implements Serializable {


       private static final long serialVersionUID = 503665425710114912L;

       @Id
       @Column(name="hes_invoice_header_id", insertable=false, updatable=false)
       private long id;

       @Column(name="claim_number", insertable=false, updatable=false, unique=false)
       private String claimNumber;

       @OneToMany(cascade={CascadeType.ALL})
       @JoinColumn(name="hes_invoice_header_id", insertable=false, updatable=false)
       private List<AppealHeader> appealHeader;

       public long getId() {
           return id;
       }

       public void setId(long id) {
           this.id = id;
       }

       public String getClaimNumber() {
           return claimNumber;
       }

       public void setClaimNumber(String claimNumber) {
           this.claimNumber = claimNumber;
       }

        public List<AppealHeader> getAppealHeader() {
           return appealHeader;
        }

        public void setAppealHeader(List<AppealHeader> appealHeader) {
           this.appealHeader = appealHeader;
        }

}

public void exececute() {

Criteria criteria = session.createCriteria(AppealHeader.class);

    if(!"".equals(form.getCustomer()))
        criteria.add(Restrictions.eq("customer",form.getCustomer()));
    if(!"".equals(form.getClaimNumber()))
        criteria.add(Restrictions.eq("billingHeader.claimNumber",form.getClaimNumber()));
}

现在,我正在执行此条件查询:

如果我按客户查询,我会收到所有申诉标题记录以及相关的结算标题。但是,如果我通过customer和billingHeader.claimNumber仅查询billingHeader.claimNumber。我得到了这个例外:

Exception: org.hibernate.QueryException: could not resolve property: billingHeader.claimNumber of: healthe.appeals.model.AppealHeader

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

由于您没有为您的休眠限制提供代码,我已经为您提供了Hibernate文档的示例,我认为它将指向您正确的方向:

15.4. Associations

List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%") )
    .createCriteria("kittens")
        .add( Restrictions.like("name", "F%") )
    .list();