I have an many to many table which I am querying and want to return one column from it
Consider
Table 1
M to M (consisting of TABLE1_ID and TABLE2_ID)
Table2
I am in Table 2 entity and I want to return all the table 2 ids in the M to M table when I give it a table 1 id.
In my entity I have
#ElementCollection
#CollectionTable(name="M_TO_M_TABLE", joinColumns=#JoinColumn(name="TABLE1_ID"))
#Column(name="TABLE_2_ID")
private Set<Integer> tableTwoIds;
When I try and query this the JPA query that is produced is weird!
My query is
SELECT tab2 from Table2 tab2 where tab2.tableOneIds in (:idsPassedIn)
The error I get makes sense from the query that is generated. The error is
org.hibernate.exception.GenericJDBCException: Missing IN or OUT parameter at index:: 1
and the query is
select tab2.ID, tab2.NOTES
from TABLE_2 tab2, M_TO_M mToM
where tab1.ID=mToM.TAB_1_ID and (. in (? , ?))
and we have a . after the and rather than mToM.TAB_2_ID
Has anyone any ideas?
Thanks
Related
I have two queries that I'm trying to join together.
In first_query TABLE2.PROCESS_ID, every PROCESS_ID is unique in that table. In second_query though there are several PROCESS_ID's with the same number in TABLE3, so I think I have to do a one-to-many join. The join_query I have is giving me an error ORA-00933: SQL command not properly ended which I'm assuming has something to do with the one-to-many relationship with the JOIN.
I'm not really sure how to resolve this. Any help would be much appreciated!
first_query = """
SELECT TABLE1.RULE_ID, TABLE2.STATUS, TABLE2.ERROR_MESSAGE, TABLE2.PROCESS_ID
FROM TABLE2 LEFT JOIN
TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')
"""
second_query = """
SELECT RECORDS_PROCESSED, PROCESS_ID, STATUS
FROM TABLE3
"""
join_query = """
SELECT RULE_ID, STATUS, ERROR_MESSAGE, PROCESS_ID
FROM (first_query) as query_1
INNER JOIN (second_query) as query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY PROCESS_ID desc
"""
You can not select 4 columns and group by only one of them unles you include selected columns as part of aggregation fucntion(like max(), sum(),...). One of the options is this:
SELECT query_1.RULE_ID --1
, query_2.STATUS --2
, query_1.ERROR_MESSAGE --3
, query_1.PROCESS_ID --4
FROM (SELECT TABLE1.RULE_ID
, TABLE2.STATUS
, TABLE2.ERROR_MESSAGE
, TABLE2.PROCESS_ID
FROM TABLE2
LEFT JOIN TABLE1
ON TABLE1.RULE_ID = TABLE2.RULE_ID
WHERE TABLE1.RULE_NAME IN ('TEST1', 'TEST2')) query_1
INNER JOIN (SELECT RECORDS_PROCESSED
, PROCESS_ID
, STATUS
FROM TABLE3) query_2
ON query_1.PROCESS_ID = query_2.PROCESS_ID
GROUP BY query_1.RULE_ID
, query_2.STATUS
, query_1.ERROR_MESSAGE
, query_1.PROCESS_ID
Also please do consider using aliases like this(in your first query):
SELECT T1.RULE_ID
, T2.STATUS
, T2.ERROR_MESSAGE
, T2.PROCESS_ID
FROM TABLE2 T2
LEFT JOIN TABLE1 T1 ON T1.RULE_ID = T2.RULE_ID
WHERE T1.RULE_NAME IN ('TEST1', 'TEST2')
Also, apply the same logic with aliases on your final query or else you will have a different kind of error : "ORA-00918: column ambiguously defined"
Here is a small demo
CTE (i.e. the WITH factoring clause) might help.
WITH first_query
AS (SELECT table1.rule_id,
table2.status,
table2.error_message,
table2.process_id
FROM table2 LEFT JOIN table1 ON table1.rule_id = table2.rule_id
WHERE table1.rule_name IN ('TEST1', 'TEST2')),
second_query
AS (SELECT records_processed, process_id, status FROM table3)
SELECT a.rule_id,
a.status,
a.error_message,
a.process_id
FROM first_query a INNER JOIN second_query b ON a.process_id = b.process_id
GROUP BY you used is invalid; you can't group results by only one column. If results have to be unique, use select distinct. If you have to use group by, specify all columns returned by select (which leads you back to what distinct does), or see whether some column(s) have to be aggregates - in that case, group by makes sense.
Also, you should always use table aliases. Without them, query is invalid as database engine doesn't know which table those columns (if they share the same name) belong to.
Cannot work out why these two statements return a different number of results. They should be the same. The SQL version (correctly) returns 2 results and the HQL version returns (incorrectly) 3 results.
The output of the SQL returns 3 results of tb2.user with values 1, null and null. The where clause means this filters down to 2 results, removing the result with a tb2.user value of 1. However, the HQL version returns 3 results. I would like the HQL to return 2 results.
My SQL
SELECT * FROM table1 as tb1 LEFT JOIN table2 as tb2 ON tb1.user = tb2.blocked WHERE tb2.user <> 1 OR tb2.user is null;
My HQL
SELECT r FROM table1 tb1 LEFT JOIN table2 tb2 ON tb1.user.id = tb2.user.id WHERE tb2.user.id <> :userId OR tb2.user.id is null GROUP BY tb1
Any help on this is much appreciated!
You should not use left joined table's column in where condition .. this work as an inner join
you should move these condition in the related ON clause
and in the second query ( My HQL) you have an improper group by without aggregation function (so the query are not equivalent)
(when you need distinct result .. use Distinct clause )
SELECT r
FROM table1 tb1
LEFT JOIN table2 tb2 ON tb1.user.id = tb2.user.id
AND ( tb2.user.id <> :userId OR tb2.user.id is null )
I have formulated an SQL query that I need to translate into JPQL. The query contains a subquery in the SELECT clause, and also selects all rows from the same table that is contained in the subquery.
The following is an example of the SQL query that I am trying to translate:
SELECT table.* FROM TABLE table, (SELECT * FROM TABLE table1 WHERE ... ) table1 WHERE table.id >= table1.id
The part that I am having difficulties with is the table that I am creating using the subquery SELECT table.* FROM TABLE table, (SELECT * FROM TABLE table1 WHERE ... ) table1. The WHERE clause in the subquery is clear how to translate.
The way I currently have tried to translate the query is:
SELECT t FROM Table t, t1 FROM Table t1 WHERE ... WHERE t.id >= t1.id
The error I am getting when trying this is:
org.eclipse.persistence.jpa.jpql.parser.NullExpression cannot be cast to org.eclipse.persistence.jpa.jpql.parser.IdentificationVariable
Any help or suggestions would be appreciated :D
Well.... I guess I found the answer. It's not possible to implement such named queries as explained in this post: JPA/hibernate subquery in from clause
I will stick to a native query instead.
I have a problem with this query in JPA
Employee table
id,....,location_id (id 1,2,3,4 assigned to location id 1)
EmployeeMaster table
id,.....,date, employee_id(employee_id 1 and 2 having records in this table)
JPA query
select me,ms from EmployeeMaster ms right join ms.employee me where ms.date between ?2 and ?3 and me.location.id = ?4
Output
employeeMaster,employee1
employeeMaster,employee2
Because only two employees having records in EmployeeMaster table between dates
I want the output be like
employeeMaster,employee1
employeeMaster,employee2
null,employee3
null,employee4
Please help me to solve this
Thanks.
instead of retrieving employeeMaster retrieve employee and you will get your result.
I have 2 tables
The table .issues looks like this:
id tracker_id project_id
22 1 218
In the second table .trackers:
id name
1 Error
Is it possible to make one Select request to receive back the result with names of trackers instead of/with their ids? I need it to simplify the Java Request.
I want to get smth like this:
id (tracker_id) project_id tracker_name
22 (1) 218 Error
I tried this, but I know it doesn't make much sense:
Statement stmt1 = conn.createStatement();
ResultSet ticketsRows = stmt1.executeQuery("SELECT * FROM issues
WHERE tracker_id IN (SELECT id FROM trackers)");
Join the trackers table and select the name column from that table instead the tracker_id
SELECT i.id, t.name as tracker_name, i.project_id
FROM issues i
JOIN trackers t on i.tracker_id = t.id
Change your query to :
SELECT i.id, i.tracker_id, i.project_id, t.name AS tracker_name
FROM issues i
JOIN trackers t on i.tracker_id = t.id
this would give you exact answer to your question --
SELECT IS.ID, IS.tracker_id ,IS.project_id , TR.tracker_name
FROM issues IS , tracker TR
where
IS.tracker_id = TR.id