Solved

Hibernate HQL help

Posted on 2014-02-17
5
385 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 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 36

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

687 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