-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JDBC style '?' parameters
PostPosted: Thu Feb 16, 2012 8:38 pm 
Newbie

Joined: Wed Jul 13, 2011 4:12 pm
Posts: 3
Hello, I have a JDBC query where each question mark represents a parameter which is later
set by Query.setLong(int paramIndex, Long value). The hibernate javadoc and it says that it
supports JDBC style '?' parameters. Is this true? I'm also having difficulties getting the count "count(c2.*)". Seems to be my syntax is wrong. Could somebody please help me out here. See below for code snippet.

Code:
      @SuppressWarnings("unchecked")
    protected List<CategoryNode> findByCriterion(Criterion criterion) {
        List<Category> cats = session.createCriteria(Category.class).add(criterion).list();
        Map<Integer, CategoryNode> childNodes = new LinkedHashMap<Integer, CategoryNode>();
        for (Category cat : cats) {
            CategoryNode childNode = new CategoryNode();
            childNode.setCategory(cat);
            childNodes.put(cat.getId(), childNode);
        }
        StringBuilder questions = new StringBuilder();
        for (int i = 0; i < childNodes.size(); ++i) {
            if (i != 0) {
                questions.append(", ");
            }
            questions.append("?");
        }


        Query query = session.createSQLQuery(
                "select c1.id, count(c2.*) "
                + "from Category c1 "
                + "left join Category c2 on c2.parentCategoryId = c1.id "
                + "where c1.id in (" + questions + ") "
                + "group by c1.id");

        int i = 0;

        for (Iterator<CategoryNode> it =
            childNodes.values().iterator(); i < childNodes.size(); ++i) {
            query.setLong(i + 1,
            it.next().getCategory().getId());
        }

        for (Iterator<Object[]> it = query.iterate(); it.hasNext();) {
            Object[] result = it.next();
            Integer childId = (Integer) result[0];
            Integer grandChildCount = (Integer) result[1];
            CategoryNode childNode = childNodes.get(childId);
            childNode.setHasChildren(grandChildCount != 0);
            childNode.setIsLeaf(grandChildCount == 0);
        }

        return new ArrayList<CategoryNode>(childNodes.values());
    }


Top
 Profile  
 
 Post subject: Re: JDBC style '?' parameters
PostPosted: Fri Feb 17, 2012 3:17 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
The hibernate javadoc and it says that it
supports JDBC style '?' parameters. Is this true?


Yes, hibernate does support positional parameters.
But the recommendation of hiberante is to not use positional parameters, use named parametes instead.
The reason: Using positional parameters, every change of the position of the bind parameters requires a change to the parameter binding code. This leads to fragile and maintenance-intensive code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.