我已经写了公司课程的标准。
以下是公司类别,companySearch类别和条件。但是条件列表是抛出异常。例外是“ org.hibernate.QueryException:无法解析属性:com.sesami.common.domain.Company的san.san”。如何访问Company.san.san?

公司类



public class Company extends DomainObject implements UserDetails {
private Long id;
private String companyName;
private CompanyType companyType;
private String description;
private String companyURL;
private String billToEmail;
private String hashPassword;
private SAN san;

@OneToOne(cascade = { CascadeType.ALL })
public SAN getSan() {
    return san;
}

public void setSan(SAN san) {
    this.san = san;
}
...
}


公司搜索



public class CompanySearch {
private String companyName;
private String email;
private Long san;
private String gstNumber;
......
public Long getSan() {
    return san;
}

public void setSan(Long san) {
    this.san = san;
}
...
}


标准



companyCriteria = this.getSession().createCriteria(
            Company.class);

    if (companySearch.getSan() != null
            && !"".equals(companySearch.getSan()))
        companyCriteria.add(Restrictions.eq("san.san",
                companySearch.getSan()));

    Integer count = ((Long) companyCriteria.setProjection(
            Projections.rowCount()).uniqueResult()).intValue();
    companyCriteria.setProjection(null);
    companyCriteria.setResultTransformer(Criteria.ROOT_ENTITY);
    companyCriteria
            .setFirstResult((pager.getPage() - 1) * pager.getPageSize())
            .setMaxResults(pager.getPageSize()).list();
    List<Company> companies = companyCriteria.list();

    PagedResultSet pr = new PagedResultSet();
    pr.setPager(pager);
    pr.setResultSet(companies);
    pr.setRowCount(count);
    return pr;

最佳答案

您必须使用子条件或别名创建San实体的联接:

companyCriteria.createAlias("san", "sanAlias");
companyCriteria.add(Restrictions.eq("sanAlias.san",
                    companySearch.getSan()));


要么

companyCriteria.createCriteria("san").add(Restrictions.eq("san",
                                          companySearch.getSan()));


Hibernate reference documentation甚至Criteria javadoc中对此都有很好的解释。

注意,这与Spring完全无关,与Hibernate无关。如果您在Spring文档中搜索了如何执行此操作,那也就不足为奇了。

10-06 06:29