[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2774) Call ResultTransformer.transformList() even if the list argument is an empty list

Christian Bauer (JIRA) noreply at atlassian.com
Thu Aug 9 07:02:13 EDT 2007


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2774?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_27779 ] 

Christian Bauer commented on HHH-2774:
--------------------------------------

Have fun: 

    public <N extends NestedSetNode> NestedSetNodeWrapper<N> findNestedSetNodes(N startNode, Comparator<NestedSetNodeWrapper<N>> comparator) {
        StringBuffer queryString = new StringBuffer();

        queryString.append("select").append(" ");
        queryString.append("count(n1.id) as nestedSetNodeLevel").append(", ");
        queryString.append("n1 as nestedSetNode").append(" ");
        queryString.append("from ").append(startNode.getTreeSuperclassEntityName()).append(" n1, ");
        queryString.append(startNode.getTreeSuperclassEntityName()).append(" n2 ");
        queryString.append("where n1.nsThread = :thread and n2.nsThread = :thread").append(" ");
        queryString.append("and n1.nsLeft between n2.nsLeft and n2.nsRight").append(" ");
        queryString.append("and n2.nsLeft > :startLeft and n2.nsRight < :startRight").append(" ");

        queryString.append("and n1.menuItem = true").append(" ");

        queryString.append("group by").append(" ");
        for (int i = 0; i < startNode.getTreeSuperclassPropertiesForGrouping().length; i++) {
            queryString.append("n1.").append(startNode.getTreeSuperclassPropertiesForGrouping()[i]);
            if (i != startNode.getTreeSuperclassPropertiesForGrouping().length-1) queryString.append(", ");
        }
        queryString.append(" ");

        queryString.append("order by n1.nsLeft");

        Query nestedSetQuery = getSession().createQuery(queryString.toString());
        nestedSetQuery.setParameter("thread", startNode.getNsThread());
        nestedSetQuery.setParameter("startLeft", startNode.getNsLeft());
        nestedSetQuery.setParameter("startRight", startNode.getNsRight());

        NestedSetNodeWrapper<N> startNodeWrapper = new NestedSetNodeWrapper<N>(startNode, comparator, 0l);
        nestedSetQuery.setResultTransformer( new NestedSetResultTransformer<N>(startNodeWrapper) );

        return (NestedSetNodeWrapper<N>)nestedSetQuery.list().get(0);
    }

    public class NestedSetResultTransformer<N extends NestedSetNode> implements ResultTransformer {
        NestedSetNodeWrapper<N> rootWrapper;
        NestedSetNodeWrapper<N> currentParent;


        public NestedSetResultTransformer(NestedSetNodeWrapper<N> rootWrapper) {
            this.rootWrapper = rootWrapper;
            currentParent = rootWrapper;
        }

        public Object transformTuple(Object[] objects, String[] aliases) {

            if (!"nestedSetNodeLevel".equals(aliases[0]))
                throw new RuntimeException("Missing alias 'nestedSetNodeLevel' as the first projected value in the nested set query");
            if (!"nestedSetNode".equals(aliases[1]))
                throw new RuntimeException("Missing alias 'nestedSetNode' as the second projected value in the nested set query");
            if (objects.length != 2) {
                throw new RuntimeException("Nested set query needs to return two values, the level and the nested set node");
            }

            Long nestedSetNodeLevel = (Long)objects[0];
            N nestedSetNode = (N)objects[1];

            NestedSetNodeWrapper<N> newNodeWrapper = new NestedSetNodeWrapper<N>(nestedSetNode, currentParent.getComparator(), nestedSetNodeLevel);
            System.out.println("### TYRING TO PLACE: " + newNodeWrapper);

            if (!newNodeWrapper.getWrappedNode().getParent().getId().equals(currentParent.getWrappedNode().getId())) {
                NestedSetNodeWrapper<N> foundParent = findParentInTree(newNodeWrapper.getWrappedNode().getParent().getId(), currentParent);
                if (foundParent != null) {
                    currentParent = foundParent;
                } else {
                    return null; // Continue
                }
            }
            System.out.println("### PLACING: " + newNodeWrapper + " UNDER PARENT: " + currentParent);
            newNodeWrapper.setParent(currentParent);
            currentParent.getChildren().add(newNodeWrapper);
            currentParent = newNodeWrapper;

            // TODO: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2774
            return rootWrapper;
        }

        private NestedSetNodeWrapper<N> findParentInTree(Long parentId, NestedSetNodeWrapper<N> startNode) {
            if (!parentId.equals(startNode.getWrappedNode().getId()) && startNode.getParent() != null) {
                return findParentInTree(parentId, startNode.getParent());
            } else if (parentId.equals(startNode.getWrappedNode().getId())) {
                return startNode;
            } else {
                return null;
            }
        }

        public List transformList(List list) {
            // TODO: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2774
            list.clear();
            list.add(rootWrapper);
            return list;
        }


    }


> Call ResultTransformer.transformList() even if the list argument is an empty list
> ---------------------------------------------------------------------------------
>
>                 Key: HHH-2774
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2774
>             Project: Hibernate3
>          Issue Type: Improvement
>          Components: core
>            Reporter: Christian Bauer
>            Priority: Trivial
>
> I have a case where a ResultTransformer is doing all the work in transformTuple() and always returns null from that method. The transformed target state is held internally in the ResultTransformer instance (I basically turn many tuples into one). Now I can't get this state out of the ResultTransformer because transformList() is not called after transformTuple(). 
> It would be better if transformList() is called even if the list of tuples returned by transformTuple() is empty.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list