?
Solved

Hibernate HQL help

Posted on 2014-02-17
5
Medium Priority
?
386 Views
Last Modified: 2014-02-18
Hello there,

I have this block of code and I have this 2 HQL query version. Depending on the flag value I need to run either of the 2 queries. but I get this error when I run the condition where flag is equal to 1.please help.

=== 2014-02-18 08:47:18,121 [l0-0] DEBUG DataSourceDMI - Invocation threw exception
org.hibernate.QueryParameterException: could not locate named parameter [value]
      at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:99)
      at org.hibernate.engine.query.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:105)
      at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:437)
      at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:407)
      at com.kaizen.erp.server.bussinessLogic.warehouse.BranchRequestBussinessLogic.fetch(BranchRequestBussinessLogic.java:101)



if (dsRequest.getFieldValue("getApproved") != null && !dsRequest.getFieldValue("getApproved").toString().isEmpty())
		{
			flag = Long.parseLong(dsRequest.getFieldValue("getApproved").toString());
		} else
		{
			flag = (long) 2;
		}

		StringBuilder query = new StringBuilder("from BranchRequest ");
		Query result = hibernateSession.createQuery(query.toString());
		
		if (flag == 1)
		{		
			query.append(" where approveStatus = :value");
			result.setParameter("value", true);
			DebugTools.print(query.toString());
		} 
		else
		{
			DebugTools.print(query.toString());
			DebugTools.print("FLAG VALUE   " + flag);
		}

		//result = hibernateSession.createQuery(query.toString());
		List<BranchRequest> models = result.list();

Open in new window

0
Comment
Question by:zolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39866559
It is because you are creating the Query object on line 10 from the String contents that are currently in the StringBuilder so it is set at that point to only be "from BranchRequest". When you append the criteria on line 14, that effectively means nothing because the Query object has already been created. Perhaps you need something like this...
if (dsRequest.getFieldValue("getApproved") != null && !dsRequest.getFieldValue("getApproved").toString().isEmpty())
		{
			flag = Long.parseLong(dsRequest.getFieldValue("getApproved").toString());
		} else
		{
			flag = (long) 2;
		}

		StringBuilder query = new StringBuilder("from BranchRequest ");
		Query result;
		
		if (flag == 1)
		{		
			query.append(" where approveStatus = :value");
			result = hibernateSession.createQuery(query.toString());
			result.setParameter("value", true);
			DebugTools.print(query.toString());
		} 
		else
		{
			result = hibernateSession.createQuery(query.toString());
			DebugTools.print(query.toString());
			DebugTools.print("FLAG VALUE   " + flag);
		}

		List<BranchRequest> models = result.list();

Open in new window

0
 

Author Comment

by:zolf
ID: 39866574
thanks a lot for your help and explaining the reason. one another question related to this code. on line 1 to 7 where I have this if/else. do I need to have this else which is doing nothing but I had to add that else loop because I was getting error.so to overcome this error I have t add this else loop with some flag value 2 which I don't use it.

=== 2014-02-18 09:29:32,208 [l0-2] DEBUG DataSourceDMI - Invocation threw exception
java.lang.NullPointerException
      at com.kaizen.erp.server.bussinessLogic.warehouse.BranchRequestBussinessLogic.fetch(BranchRequestBussinessLogic.java:94)
0
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39866599
It is a little bit hard to tell without seeing the whole code (and without having a 100% clear idea on exactly what you are trying to achieve) but I believe the below should do the same as the code that you have above, but with the flag removed entirely. I think it looks neater, just make sure that it IS doing what you want it to...
		StringBuilder query = new StringBuilder("from BranchRequest ");
		Query result;
		
		if (dsRequest.getFieldValue("getApproved") != null && "1".equals(dsRequest.getFieldValue("getApproved").toString())) {
			query.append(" where approveStatus = :value");
			result = hibernateSession.createQuery(query.toString());
			result.setParameter("value", true);
		} else {
			result = hibernateSession.createQuery(query.toString());
		}
		DebugTools.print(query.toString());

		List<BranchRequest> models = result.list();

Open in new window


Also, if this as simple as what you are trying to do, I don't see why you need to use .setParameter() since the value you are setting is always "true". If you have just simplified the code to post on here then forget the next snippet, but if it is this simple, can't you just do this...
		StringBuilder query = new StringBuilder("from BranchRequest ");
		if (dsRequest.getFieldValue("getApproved") != null && "1".equals(dsRequest.getFieldValue("getApproved").toString())) {
			query.append(" where approveStatus = true");
		}
		DebugTools.print(query.toString());

		Query result = hibernateSession.createQuery(query.toString());
		
		List<BranchRequest> models = result.list();

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 39866622
cheers!!
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39867175
No worries! :)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question