Simulating a database DELETE CASCADE - java

Above is how my database table structure looks like. B refers to A, C refers to A, D refers to B and so on. Now, I am running an offline job, that drops all constraints on these tables and deletes a few records by evaluating them in my java application. So for instance, delete from A WHERE id > 20, will be translated into a bunch of selects.
SELECT B.ID, A.ID FROM B,A WHERE B.FKEY = A.ID
SELECT C.ID, A.ID FROM C,A WHERE C.FKEY = A.ID
SELECT D.ID, A.ID FROM D,B,A WHERE B.FKEY = A.ID AND D.FKEY = B.ID
... so on..
.. finally.. SELECT H.ID, A.ID FROM A,B,D,F,H WHERE B.FKEY = A.ID AND D.FKEY = B.ID AND F.FKEY = D.ID AND H.FKEY = F.ID UNION SELECT H.ID, A.ID FROM A,C,E,G,H WHERE C.FKEY = A.ID AND E.FKEY = C.ID AND G.FKEY = E.ID AND H.FKEY = G.ID
Retrieving that data and verfifying for A.ID > 20 in code and then deleting what are not required is how my application performs delete (I know it might sound a little crazy, but this is how it is supposed to work).
Now the question I have here is this... How do I make this generic? My challenge is to identify tables like "H" that have multiple inheritance (or say that form cycles in the ER graph). I've tried looking into graph theory, but it is pretty confusing - way too much information for me to process.
In short, I would like to identify cycles in the ER graph and construct UNION queries for such tables.

Related

Oracle SQL Developer - JOIN on 2 queries with a one-to-many relationship

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.

Add =value in a left join clause using execute query

Working with grails 3.3.8 and Mysql DB 5.5.x version.
I have done this query:
String query = $/
select new Map( i1.id as id,i1.name as name)
from CustomerComposition as c1
inner join c1.instrument as i1
left join i1.analisys as a1,
Instrument as i2
left join i2.analisys as a2,
Instrument as i3
left join i3.analisys as a3
where
i1.id=i2.id and i1.id=i3.id and
c1.portfolio.id=:ptfId and a1.phase.id=:p1Id and a2.phase.id=:p2Id and a3.phase.id=:p3Id
/$
List composition = CustomerComposition.executeQuery(query,
[ptfId: ptfId, p1Id: phase[0], p2Id: phase[1], p3Id: phase[2]])
The left join doesn't work. Then I realize that it doesn't work because I put a clause inside the WHERE. I double check with simple SQL statment and indeed it works once moved the conditional out the where. A simple snip taken just for clarification, before and it did not work:
SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id
WHERE
customerpt0_.portfolio_id =1216
AND analisys4_.phase_id =111
and after and it works due to how left/right join works:
SELECT
instrument1_.id
FROM
customer_ptf_composition customerpt0_
INNER JOIN instrument instrument1_ ON customerpt0_.instrument_id = instrument1_.id
LEFT JOIN analisys analisys4_ ON instrument1_.id = analisys4_.instrument_id AND analisys4_.phase_id =111
WHERE
customerpt0_.portfolio_id =1216
Now my question is how can I put the "and field=value" next to the left join in GORM?
You can use the WITH clause to achive this:
left join i3.analisys a3 WITH a3.something = :someValue
See also: current Hibernate user guide: HQL explicit join

Inefficient JPA query using compound Where clause

I have a tables with One-Many Relationships as follows
City->School->Teacher->Children
and my JPQL for retrieving children from a city is as below
#Query("Select c from Children c where c.teacher.school.city=:city")
Set<Children> findChildrenFromCity(#Param("city") City city);
This reference here about Where clause says that
"Compound path expressions make the where clause extremely powerful."
However, upon observing the logs I realise that the above query is doing strange things like
Generate multiple Selects instead of one Select
Some cross joins can be seen in the logs
I am trying to understand if I am defining my query correctly and if the compound Where is indeed so powerful, why is my query so inefficient.
You can use the following method:
Set<Children> findAllByTeacherSchoolCity(String city);
assuming, that your class Children has field Teacher teacher, Teacher has School school and School has String city.
In case there are differences, please ask in comments for clarification.
Try this
#Query("Select c from City city join city.schools s join s.teachers t join t.childrens c where city = :city")
Set<Children> findChildrenFromCity(#Param("city") City city);
This query is running exactly one Select query to fetch the Children entities. Check the below mentioned logs.
HIBERNATE: SELECT childrens3_.id AS id1_0_,
childrens3_.date_created AS date_cre2_0_,
childrens3_.date_updated AS date_upd3_0_,
childrens3_.NAME AS name4_0_,
childrens3_.teacher_id AS teacher_5_0_ FROM city city0_
INNER JOIN school schools1_
ON city0_.id = schools1_.city_id
INNER JOIN teacher teachers2_
ON schools1_.id = teachers2_.school_id
INNER JOIN children childrens3_
ON teachers2_.id = childrens3_.teacher_id WHERE city0_.id = ?
Now what you have is an n+1 issue. To fix such issue you can use join fetch instead of simple joins.
If you want use Query annotation try this approach
#Query("Select c from Children c join fetch c.teacher t join fetch t.school s join fetch s.city ct where ct.id = :id")

Hibernate Criteria: hibernate left join without association

I am a Hibernate newbie and i have this below query. It is working as i expected. These two tables are not associated. Is there a way to get the same result by using Criteria API or how can i run this query via Hibernate ? Any help would be appreciated.
SELECT p.title, c.content
FROM posts p
LEFT JOIN comments c ON p.id = c.post_id
WHERE p.status = 'A' AND (p.title iLIKE '%r%' OR c.content iLIKE '%r%');
Criteria API needs a path between entities, so I'm not sure this join could be done using Criteria API. Better do it with HQL if you have Hibernate >= 5.1:
select p.title, c.content
from org.example.Posts p
left outer join org.example.Comments c
on p.id = c.id
where p.status = 'A' AND (lower(p.title) LIKE '%r%' OR lower(c.content) LIKE '%r%');
Still, you could stick to using SQL queries with Hibernate or better still, create association between Posts and Comments.

LEFT JOIN freezes query execution

I have a pretty simple sql query:
SELECT o.description, oi.description, cu.name
FROM dbo.order o
LEFT JOIN dbo.orderitem oi o.orderItemId = oi.orderItemId
INNER JOIN dbo.customer cu on cu.customerId = o.customerId
WHERE cu.id = 12345
That query works, every time.
If I change the INNER JOIN to a LEFT JOIN...
LEFT JOIN dbo.customer cu on cu.customerId = o.customerId
then it always freezes when I call it with Java (via REST) and sometimes freezes if I am using my SQL Client. Sometimes it just works with the exact same search parameter and returns a few rows (<10) as expected.
Freezes means that it won't even start counting the execution time in my SQL client (SQuirreL) and that it will run into a timeout when I call it in Java.
But it will work again if I add a second WHERE clause like
SELECT o.description, oi.description, cu.name
FROM dbo.order o
LEFT JOIN dbo.orderitem oi o.orderItemId = oi.orderItemId
LEFT JOIN dbo.customer cu on cu.customerId = o.customerId
WHERE cu.id = 12345
AND o.someOtherId = 3456
or remove the cu.id completely.
Does anyone have any clue why that could happen?
I am not getting this line..
LEFT JOIN dbo.orderitem am oi o.orderItemId = oi.orderItemId
Dont you think it should be like:
LEFT JOIN dbo.orderitem oi on o.orderItemId = oi.orderItemId
Can you try?
This is because the left join on customer makes the where clause ineffective; the query would return all orders, with all orderitems, but only for orders of customer 12345, it would also return the customer name.
Maybe you want to do a right join instead, so you get the customer name even if he has no orders?

Categories

Resources