Hello,
I've a problem with the return values of named queries:
First here's a snippet of my mapping hbm.xml file:
Code:
<class name="com.ax.dashboard.entity.CloneDiffList">
<id name="cloneDiffListId" type="integer"/>
<property name="activeBug" type="integer"/>
<property name="sourceFileName" type="string" />
<property name="sourceStartLine" type="integer" />
<property name="sourceEndLine" type="integer" />
<property name="sourceFragmentSize" type="integer" />
<property name="targetFileName" type="string" />
<property name="targetStartLine" type="integer" />
<property name="targetEndLine" type="integer" />
<property name="targetFragmentSize" type="integer" />
<property name="cloneType" type="integer" />
<property name="groupId" type="integer" />
</class>
<query name="CloneDiffList.findByFileNameAndVersion">
<![CDATA[
SELECT version.cloneVersionId as cloneDiffListId, 1 as activeBug,
version.sourceFragment.fileName.name as sourceFileName, version.sourceFragment.startLine as sourceStartLine, version.sourceFragment.endLine as sourceEndLine,
version.sourceFragment.fragmentSize as sourceFragmentSize,
version.targetFragment.fileName.name as targetFileName, version.targetFragment.startLine as targetStartLine, version.targetFragment.endLine as targetEndLine,
version.targetFragment.fragmentSize as targetFragmentSize,
version.issue.cloneType as cloneType, version.issue.group.groupId as groupId
FROM CloneVersion as version
WHERE (version.sourceFragment.fileName.name = :fileName OR version.targetFragment.fileName.name = :fileName)
AND version.startVersion.versionId <= :versionId
AND version.endVersion.versionId >= :versionId
]]>
</query>
The query return List<Object> instead of List<CloneDiffList>.
This is my java code to execute the query:
Code:
public List<E> findByQuery(NamedQuery namedQuery) {
Query query = createNamedQuery(namedQuery);
List<E> result = query.getResultList();
return result;
}
// NamedQuery class just wraps the query name and the parameters
protected Query createNamedQuery(NamedQuery namedQuery) {
Query query = getEntityManager().createNamedQuery(namedQuery.getQueryName());
if (namedQuery.getMaxResults() > 0) {
query.setMaxResults(namedQuery.getMaxResults());
}
if (namedQuery.getFirstResult() > 0) {
query.setFirstResult(namedQuery.getFirstResult());
}
final Map<String, Object> parameters = namedQuery.getParameterMap();
// set parameters if exist
Set<Entry<String, Object>> rawParameters = parameters.entrySet();
for (Entry<String, Object> entry : rawParameters) {
query.setParameter(entry.getKey(), entry.getValue());
}
return query;
}
Thanks for help!