I want to write a complex hql query with relationships from 3 tables. It is necessary to make a selection of the selected columns so that they are located by ID
#Query("SELECT p.regId, p.method, p.tax, p.fee, p.netAmount, r.countSec, p.status " +
"FROM P p INNER JOIN R r INNER JOIN D d on p.regId = r.id AND p.regId = d.id")
List<P> findAllByRegId(String regId);
My compiler cannot execute the request, I don't understand what the problem is! Help, please
org.postgresql.util.PSQLException: ERROR: syntax error at or near "join"
The format for multiple joins is
SELECT columns ...
FROM table1 AS t1
INNER JOIN
table2 AS t2
ON t1.id = t2.id
INNER JOIN
table3 AS t3
ON t3.id = t1.id
Your join is totally wrong. You have to add ON keyword and map primary key and foreign key in first inner join.
Here is your join, which is wrong
SELECT columns FROM A a
INNER JOIN B b
INNER JOIN C c ON a.ColName = b.ColName AND a.ColName = c.ColName
Here is the correct syntex for multiple join
SELECT columns FROM A a
INNER JOIN B b ON a.ColName = b.ColName
INNER JOIN C c ON a.ColName = c.ColName
Here down is modified query
#Query("SELECT p.regId, p.method, p.tax, p.fee, p.netAmount, r.countSec, p.status " +
"FROM P p INNER JOIN R r ON p.regId = r.id INNER JOIN D d ON p.regId = d.id")
Related
I wrote this JPQL query and expect a result as List<Question>:
#Query("SELECT q FROM Question q WHERE q.id IN (SELECT qc.questions FROM QCard qc WHERE qc.id IN (SELECT ct.qCards FROM CTest ct WHERE ct.id=:id))")
These are my classes:
class CTest {
id, List<QCard>
}
class QCard{
id, List<Question>
}
class Question{
id
}
I expected all questions as return for given CTest.id.
But I got a compiler-error with message:
SQLSyntaxErrorException
I tried use ":" before "select" words but it had not helped.
What is wrong?
Test your SQL query in an SQL-client (e.g. Squirrel, DbVisualizer, etc.).
Assuming your foreign keys are named:
question_id in table QCard
card_id in table CTest
you could have an SQL with subselects like:
SELECT q.id
FROM Question q
WHERE q.id IN (
SELECT qc.question_id
FROM QCard qc
WHERE qc.id IN (
SELECT ct.card_id
FROM CTest ct
WHERE ct.id = 1 -- example test id
)
)
Does it return the expected result or are there syntax errors?
Then subsequently replace your subselects by JOINs.
Question and Cards
SELECT q.id, c.id
FROM Question q
JOIN QCard c ON c.question_id = q.id
Cards and Tests
SELECT c.id, t.id
FROM QCard c
JOIN CTest t ON t.card_id = c.id
All together
SELECT q.id, c.id, t.id
FROM Question q
JOIN QCard c ON c.question_id = q.id
JOIN CTest t ON t.card_id = c.id
Note: add WHERE clauses like WHERE t.id = 1 if needed.
Experiment with the FROM/JOIN order as it makes sense.
Then translate the running SQL query to JPQL. For example:
#Query("SELECT q"
+ " FROM CTest test"
// a test has many cards (1:n)
+ " JOIN QCard card ON card.id = test.card_id" // associated cards
// a card has many questions (1:n)
+ " JOIN Question q ON q.id = card.question_id" // associated questions
+ " WHERE test.id = :id")
public List<Question> findQuestionsByTestId(String id);
List<Question> findByIdIn(List<Long> idList); //In repository
or
String qlString = "select i from Item i where i.name IN :names";
Query q = em.createQuery(qlString, Item.class);
List<String> names = Arrays.asList("foo", "bar");
q.setParameter("names", names);
List<Item> actual = q.getResultList();
I did those example in my past work, check it, tnx
In your existing query, in inner queries you are selecting entities and checking IN against Id, which will definitely won't work. As you can't complete entry with the Id.
As you haven't shared your complete entity structure, assuming that you have two way relationship declared correctly in entities, here I am placing a reference query which uses the join:
#Query("SELECT q FROM Question q JOIN q.qCard qc WHERE q.qCardId = qc.id AND qc.cTestId = :id")
Where qCardId is the foreign key reference of QCard entity in Question entity and cTestId is the foreign key reference of CTest entity in QCard entity.
You can use this for your reference to update your query with joins.
I'm trying to compare if a single column of my table equals to what the subquery returns in multiple columns. I want to be able to return rows if table1.id is equal to either one of table2.first or table2.second. What would be the valid syntax to do it?
I've tried what's below ,but it's not valid syntax as I get "Result: sub-select returns 2 columns - expected 1."
SELECT table1.id, table1.attr1, table1.attr2 FROM table1 WHERE table1.id IN (SELECT table2.first, table2.second FROM table2 WHERE type != "..." AND type != "someType")
Perhaps you intend something like this:
SELECT
t1.id,
t1.attr1,
t1.attr2
FROM table1 t1
WHERE
EXISTS (SELECT 1 FROM table2 t2 WHERE t2.first = t1.id AND type <> 'someType') OR
EXISTS (SELECT 1 FROM table2 t2 WHERE t2.second = t1.id AND type <> 'someType');
Each EXISTS clause checks for a possible match of a table1.id value against a table2 column.
You don't need subquery if you want to select from t1 only if the conditions match. Try below query:
SELECT
t1.id,
t1.attr1,
t1.attr2
FROM table1 t1 , table2 t2
WHERE
(t1.id = t2.first OR t1.id = t2.second)
AND t2.type not in ('someType' , "...");
You can do it with an INNER JOIN:
SELECT DISTINCT t1.id, t1.attr1, t1.attr2
FROM table1 t1 INNER JOIN table2 t2
ON t2.first = t1.id OR t2.second = t1.id
WHERE t2.type <> "..." AND t2.type <> "someType"
The ON clause can be written as well like:
ON t1.id IN (t2.first, t2.second)
I used DISTINCT in case table1.id could match multiple rows in table2.
Select concat(substr(T_data,1,9),'001 ') AS Test_Data from DB1.T1 ;
Select * from DB1.T2 WHERE Test_Data = 'Test_Data';
I need to join the DB1.T1 and DB1.T2 based on Test_Data
Use an INNER JOIN (or, if you want nulls, an outer join)
Select t2.*
from DB1.T1 t1
INNER JOIN DB1.T2 t2 ON concat(substr(t1.T_data,1,9),'001 ') = t2.Test_Data
WHERE t2.Test_Data = 'Test_Data';
I have such domain: Country has cities, city has offices, offices has services. I need to find all countries with offices which provide specified service id. My current version is:
SELECT c FROM Country c
WHERE EXISTS(
SELECT ct FROM c.cities ct
WHERE EXISTS(
SELECT o FROM ct.offices o
WHERE EXISTS(
SELECT s.id FROM o.services s
WHERE s.id = :id
)
)
)
So, I'm new to HQL. What is best way to this? Is my version Ok? I was thinking about SELECT DISTINCT with LEFT JOIN too.
I don't think there is anything wrong with your query, but this might be more readable.
SELECT c FROM Country
WHERE EXISTS (
SELECT s.id FROM
c.cities ct
JOIN ct.offices o
JOIN o.services s
WHERE s.id = :id
)
I have a table and a class marked as #Entity with
#Table(name="Employee")
It has two fields, Id and Name
i want to do left join on same table like,
Select t2.name, count(*)
from Employee t1 left join Employee t2 on t1.Id = t2.Id
where t1.Name = 'asd'
group by t2.Name
I had a workaround, I converted that query into sub query
Select EmployeeID From Employee
Where ManagerID in (Select EmployeeID where performance >= 4 )
And used DetachedCriteria and i got it worked. But this will reduce my performance. SO i will try your method and see if i can get it worked
Ref
https://forum.hibernate.org/viewtopic.php?p=2389790