简体   繁体   English

使用ProjectionFactoryUtil时出现Liferay异常

[英]Liferay exception while using ProjectionFactoryUtil

Am using ProjectionFactoryUtil to get specific columns eg : i want to gett ID and Name from table DETAIL_TABLE 我正在使用ProjectionFactoryUtil获取特定列,例如:我想从表DETAIL_TABLE中获取ID和名称

am using Dynamic query 我正在使用动态查询

DynamicQuery dynQuery = DynamicQueryFactoryUtil.forClass(DetailTable.class);

ProjectionList projectionList = ProjectionFactoryUtil.projectionList();

projectionList.add(ProjectionFactoryUtil.property("ID"));
projectionList.add(ProjectionFactoryUtil.property("NAME"));

dynQuery.setProjection(projectionList);

after that am retriving the query using 之后,使用检索查询

List<DetailTable> detailList = DetailTableLocalServiceUtil.dynamicQuery(dynQuery);

but when am trying to iterate it am getting class cast exception at the following line 但是当尝试迭代时,在下面的行中获取类强制转换异常

for(DetailTable dt : detailList){
}

the exception is " Ljava.lang.Object; cannot be cast to com.detail.model.DetailTable " 例外是“ Ljava.lang.Object;无法转换为com.detail.model.DetailTable”

the Service util class returns the same DetailTable List ,i don't know why its giving me the class cast exception Service util类返回相同的DetailTable List,我不知道为什么它给了我类强制转换异常

The exception is not coming when am not using the Projections 不使用投影仪时不会出现异常

When you add projections in DynamicQuery , it returns you list of array of Object type. DynamicQuery添加投影时,它将返回Object类型的数组列表。

Here you are trying to cast that result with List<DetailTable> , thats why its throwing exception. 在这里,您尝试使用List<DetailTable>结果,这就是其抛出异常的原因。

Instead use List<Object[]> and then cast array items according to its type(db type). 而是使用List<Object[]> ,然后根据其类型(db类型)强制转换数组项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM