Solved

need clarification on sql query written in . java file

Posted on 2014-01-27
7
535 Views
Last Modified: 2014-02-05
hi  everyone i have query in my .java file these kind of queries basically we are using to fetch some records from db.
i want to understand these kind of queries written in many places.
is there any siter to understand such kind of queries?

here is the query:
String sql = "SELECT ticket FROM Destination ticket "
                              + " WHERE ticket.sendAttempts < :threshold AND "
                              + " ticket.currentStatus.distributionType = :distType AND (";
                  
                  for (int i = 0; i < statuses.size(); i++) {
                        sql += "ticket.currentStatus.ticketStatusType=:status" + i;
                        if (i != statuses.size() - 1)
                              sql += " OR ";
                  }
                  sql += ")";





can any one explain what this query will do and how can i learn such kind of queries
For me it is difficult to understand.
Thanks.
0
Comment
Question by:srikotesh
[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
7 Comments
 
LVL 3

Assisted Solution

by:smilieface
smilieface earned 100 total points
ID: 39814213
The string you have there will construct a SQL query that will return a collection of rows stored in a table Destination.

The reference "tickets" is an alias to the destination.

The "for" section is constructing an list of ticket statuses which are to be included.

I don't recognise what is happening to the "ticket.currentStatus.distributionType" as a 3 part name for a column isn't a regular SQL construction.
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 50 total points
ID: 39814223
More generally if you're looking to learn SQL, there are plenty of sites out there.

For example try this one:
http://www.sqlcourse.com

specifically this part deals with select statements:
http://www.sqlcourse.com/select.html

which is the kind of query you posted.

Hope that helps,

Doug
0
 
LVL 6

Accepted Solution

by:
Mahesh Bhutkar earned 250 total points
ID: 39817163
It seems that you are using Hibernate HQL here.

This is complex hql example which contains conditional joins on currentStatus object.
Here currentStatus has association with Destination.

It returns ticket as object with conditions
1) ticket.sendAttempts < :threshold
2) ticket.currentStatus.distributionType = :distType
   Join with Destination & currentStatus and condition on distributionType
3) ticket.currentStatus.ticketStatusType=:status
   Join with Destination & currentStatus and condition on ticketStatusType

You can refer HQL for more clarification from, http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 100 total points
ID: 39817430
Also, note that you're not actually asking about SQL nor even about a "query". What you're asking about is programming a loop, which is very different from SQL or a query. The loop is simply building a string named "sql" by adding characters to it each time through the loop.

It's only when the loop finishes and passes the sql variable to a query engine that a query gets involved.

The best way to learn what it's doing is to look at the sql String variable each time the loop runs. That might be done in a debug session, or by adding a statement in the loop to display or print the variable.

In order to explain a "query", we would need to see the final version of the statement that gets built (though it's pretty clear what's happening) and we'd have to know what query engine is used.

Tom
0
 
LVL 2

Author Comment

by:srikotesh
ID: 39828986
thanks bonzermahesh.

hi tliotta , i am using db2,and one more thing is i want to practice such kind of queries can you suggest any advice, the above urlsi have seen so far is all are sql queries whatever i posted that  is some thing  different  from sql query that is not simple sql query.

if it is like that i can search in google then i can understand.
0
 
LVL 6

Assisted Solution

by:Mahesh Bhutkar
Mahesh Bhutkar earned 250 total points
ID: 39829541
Using Java programming you are creating here dynamic HQL query.
0
 
LVL 2

Author Closing Comment

by:srikotesh
ID: 39835821
Thanks for your valuble suggestions
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses

617 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