Solved

Hibernate HQL help

Posted on 2014-02-17
5
379 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
  • 3
  • 2
5 Comments
 
LVL 35

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 35

Accepted Solution

by:
mccarl earned 500 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 35

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 65
oracle 11g 23 107
JavaFX TableView not displaying correctly 3 59
SequenceInputStream example 3 19
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

830 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