报的错误 1. query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1] 2. query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]; nested exception is org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
hql里面不能用*,你把count(*)改成count(1)就可以了
还是不行啊 错误信息如下 1. unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1] 2. unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
换成count(1)还是不行啊,错误信息如上
Material m inner join fetch m.material_supplier 1、hql不需要join Class中的属性就可以where 1-1 and m.material_supplier .id=xxx
报的错误
1. query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
2. query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]; nested exception is org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=s,role=null,tableName=Supplier,tableAlias=supplier1_,origin=Material material0_,columns={material0_.supplier_id ,className=cn.com.su.domain.Supplier}}] [select count(*) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
错误信息如下
1. unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
2. unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 1 near line 1, column 14 [select count(1) from cn.com.su.domain.Material m inner join fetch m.material_supplier s where 1=1]
1、hql不需要join Class中的属性就可以where 1-1 and m.material_supplier .id=xxx
有material_supplier这个属性的
下面是Material类的配置文件
<hibernate-mapping>
<class name="cn.com.su.domain.Material">
<id name="material_id" type="java.lang.String" length="32">
<generator class="uuid"></generator>
</id>
<property name="material_name" type="java.lang.String" length="20"></property>
<property name="material_num" type="java.lang.String" length="20"></property>
<property name="material_brand" type="java.lang.String" length="20"></property>
<property name="material_standard" type="java.lang.String" length="20"></property>
<property name="material_price" type="java.lang.Long" length="8"></property>
<property name="material_desc" type="java.lang.String" length="500"></property>
<property name="material_unit" type="java.lang.String" length="10"></property>
<many-to-one name="material_type" class="cn.com.su.domain.Type" cascade="all">
<column name="id"></column>
</many-to-one>
<many-to-one name="material_supplier" class="cn.com.su.domain.Supplier" cascade="all">
<column name="supplier_id"></column>
</many-to-one>
</class>
</hibernate-mapping>
问题其实是我有个查询条件是Supplier类的,我想按条件查询Material的总数
try {
String countHql = "select count(*) " + hql;
int count = ((Long) getHibernateTemplate().find(countHql).get(0))
.intValue();
return count;
} catch (RuntimeException ex) {
log.error("find by getCountByHql", ex);
throw ex;
}
}
我是这样查询的