条件抛出 org.hibernate.QueryException:无法解析 属性 但 HQL 有效
Criteria throws org.hibernate.QueryException: could not resolve property but HQL works
我正在尝试使用条件检索 CompanyOnCampus 实体。它有一个组件 class 公司。映射是这样的:
<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+"%"));
或
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.add(Restrictions.ilike("company.name",pSearchString+"%"));
这里的结果是我得到了异常
org.hibernate.QueryException: could not resolve property company.name for class entities.pp.CompanyOnCampus ...
现在我尝试用 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();
结果出来了
创建 company
的别名将解决此问题。
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.createAlias("company","company");
使用公司的 createAlias 作为 criteria.createAlias("company","company") 即
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.createAlias("company", "company")
.add(Restrictions.ilike("company.name", pSearchString+"%"));
成功了。这表明对于驻留在引用的 table 中的 fields/columns,标准 API 需要提供明确的别名。
谢谢!
我正在尝试使用条件检索 CompanyOnCampus 实体。它有一个组件 class 公司。映射是这样的:
<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+"%"));
或
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.add(Restrictions.ilike("company.name",pSearchString+"%"));
这里的结果是我得到了异常
org.hibernate.QueryException: could not resolve property company.name for class entities.pp.CompanyOnCampus ...
现在我尝试用 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();
结果出来了
创建 company
的别名将解决此问题。
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.createAlias("company","company");
使用公司的 createAlias 作为 criteria.createAlias("company","company") 即
Criteria criteria = session.createCriteria(CompanyOnCampus.class);
criteria.add(Restrictions.eq("institute.instituteId", pUser.getInstituteInfo().getInstituteId()))
.createAlias("company", "company")
.add(Restrictions.ilike("company.name", pSearchString+"%"));
成功了。这表明对于驻留在引用的 table 中的 fields/columns,标准 API 需要提供明确的别名。
谢谢!