Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

need clarification on sql query written in . java file

Posted on 2014-01-27
7
Medium Priority
?
536 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 400 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 200 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 1000 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 
LVL 27

Assisted Solution

by:tliotta
tliotta earned 400 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 1000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …
Suggested Courses

670 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