我正在尝试使用条件检索CompanyOnCampus实体。它有一个组件类公司。映射是这样的:
<class name="entities.pp.Company" table="ie_company_info">
<id column="company_id" name="id"/>
<property column="company_name" name="name" type="string"/>
<property column="company_short_name" name="shortName" type="string"/>
<property column="company_description" name="description" type="string"/>
</class>
<class name="entities.pp.CompanyOnCampus" table="c2ie_companies">
<id column="c2ie_companie_id" name="id"/>
<many-to-one column="c2ie_company_id" name="company"/>
<many-to-one column="c2ie_institute_id" name="institute"/>
</class>
标准:
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.add(Property.forName("company").getProperty("name").like(pSearchString+"%"));
OR
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.add(Restrictions.ilike("company.name",pSearchString+"%"));
这里的结果是我得到了异常
org.hibernate.QueryException:无法解析类entities.pp.CompanyOnCampus的属性company.name ...
现在我尝试使用HQL:
String q = "FROM CompanyOnCampus company WHERE company.company.name LIKE :name AND company.institute.instituteId = :instiId";
Query hql = session.createQuery(q);
hql.setParameter("name", pSearchString+"%");
hql.setParameter("instiId", pUser.getInstituteInfo().getInstituteId());
List<CompanyOnCampus> companies = hql.list();
我在这里得到了结果。
答案 0 :(得分:0)
创建company
的别名将解决此问题。
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.createAlias("company","company");
答案 1 :(得分:0)
将公司的createAlias用作criteria.createAlias(&#34;公司&#34;,&#34;公司&#34;),即
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.createAlias("company", "company")
.add(Restrictions.ilike("company.name", pSearchString+"%"));
做了这个伎俩。这表明,对于驻留在引用表中的字段/列,Criteria API需要提供显式别名。
谢谢!