Solved

Hibernate HQL help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

947 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

22 Experts available now in Live!

Get 1:1 Help Now