Solved

need clarification on sql query written in . java file

Posted on 2014-01-27
7
534 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 27

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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

738 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