Solved

Hibernate HQL help

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now