JPA Inner and outer Join - jpa

I want to get list of events using following query.
I am able to do it through normal query at database level . But I am strugulling when writing JPA query.
Request you all to help me in writing JPA query.
select e.id from event e
left outer join
(sub_portfolio s inner join
(portfolio P inner join ACADEMY a on p.academy_id=a.id) on s.portfolio_id=p.id)
on e.sub_portfolio_id=s.id

Related

Which join type is used when using just JOIN in PostgreSql?

I prefer to indicate join types when I use it in database systems but when I switch to a new project, there is a single join is used. Generally I prefer to use LEFT JOIN or INNER JOIN according to my needs, but I have not found which JOIN type is considered when a single JOIN is used in PostgreSQL.
select p.uuid from Product s " +
join Category c on p.uuid = c.siteUuid
join Brand b on b.uuid = c.brandUuid
Inner Join is the default join when we use plain JOIN.
For better readablity of the queries, It is always preferred to write INNER JOIN
Reference:
https://www.postgresql.org/docs/current/queries-table-expressions.html#id-1.5.6.6.5.6.4.3.1.2

Convert LEFT JOIN query to Ecto

I have some queries I need migrate to Ecto and for maintainability reasons, I'd rather not just wrap them in a fragment and call it a day.
They have a lot of LEFT JOINs in them and as I understand from this answer, a left_join in Ecto does a LEFT OUTER JOIN by default. I can't seem to figure out how to specify to Ecto that I want a LEFT INNER JOIN, which is the default behavior for a LEFT JOIN in Postgresql.
To look at a toy example, let's say posts in our database can be either anonymous or they can have a creator. I have a query to get just enough info to make a post preview, but I only want non-anonymous posts to be included:
SELECT
p.id,
p.title,
p.body,
u.name AS creator_name,
u.avatar AS creator_avatar,
FROM posts p
LEFT JOIN users u ON p.creator_id = u.id;
I would translate that into Ecto as:
nonanonymous_posts =
from p in Post,
left_join: u in User, on: p.creator_id == u.id,
select: [p.id, p.title, p.body, u.name, u.avatar]
and Ecto spits out
SELECT
t0."id",
t0."title",
t0."body",
t1."name" AS creator_name,
t1."avatar" AS creator_avatar,
FROM "posts" AS t0
LEFT OUTER JOIN "users" as t1 ON t0."creator_id" = t1."id";
which will give back anonymous posts as well.
There is no such thing as LEFT INNER JOIN. There is only INNER JOIN and LEFT [OUTER] JOIN (OUTER part is optional, as LEFT JOIN must be outer join). So what you want is just :join or :inner_join in your Ecto query.

Using UNNEST in a custom Postres Query in Google Data Studio

I have a table that has multiple values saved in an array in one column. (I know that this is not normalized/optimal database structure.) I'm trying to write a query that can create rows for each value in the array. The query below is working for me in Tableau but not Google Data Studio (I'm using a custom query with the PostgreSQL connector). Are there any limitations/different syntax requirements when using UNNEST in Data Studio?
SELECT
e.name as event_name,
e.date as event_date,
l.full_name as leader_name,
p.full_name as participant_name
FROM
(
SELECT
event_id,
user_id,
UNNEST(participants_ids)::INTEGER as participant_id
from event_reports
) r
LEFT JOIN events e ON r.event_id = e.id
LEFT JOIN users l ON r.user_id = l.id
LEFT JOIN users p ON r.participant_id = p.id
Your inner query should probably have a lateral join:
SELECT e.event_id,
e.user_id,
p.participant_id
FROM event_reports AS e
CROSS JOIN
LATERAL unnest(e.participants_ids) AS p(as participant_id)
With a lateral join, you can reference something from the left side of the join on the right side. The cross join combines each event_reports row with rach participant ID that belongs to that row.

JPQL multiple level join fetches

JPQL how to write multiple level join fetches? For example, Order - OrderItem - Product.
select o from Order o left join fetch o.orderItems join fetch o.orderItems.product
Join fetch can not have identification variable. How to write join fetch attribute product of orderItem in the example above?

User defined join in Informatica with DB2

We are trying to replace source qualifier override with user defined joins and source filter.
for below user defined join in Informatica source qualifier:
{A INNER JOIN B ON a.dept_id= b.dept_id
b.load_date between 20170712174712000000 and 20170904152656000000
LEFT OUTER JOIN C ON a.emp_id = c.emp_id}
for this I'm getting SQL query as
FROM A,B,C WHERE {A INNER JOIN B ON A.dept_id = B.dept_id
AND b.load_date between 20170712174712000000 and 20170904152656000000
LEFT OUTER JOIN C
ON C ON a.emp_id = c.emp_id}
I have tried replacing INNER JOIN in override query with NORMAL JOIN, as I saw it somewhere that informatica translates normal to inner join.
The source database is DB2.
I don't know anything about informatica, but the resulting SQL syntax that you listed in the question is not valid for DB2. The biggest problem is that you have the JOIN in the WHERE clause rather than the FROM clause. Not real sure how to fix that in informatica though. Appropriate DB2 syntax though would be something like this:
FROM a
INNER JOIN b ON a.dept_id = b.dept_id
LEFT OUTER JOIN c ON a.emp_id = c.emp_id
WHERE
b.load_date BETWEEN 20170712174712000000 and 20170904152656000000
This assumes that b.load_date is not a timestamp field. If you are using a timestamp field, the format for the timestamps should be '2017-07-12 17:47:12.000000'
The T-SQL should be used in FROM clause not in the WHERE clause. Hence use:
FROM A INNER JOIN B ON a.dept_id = b.dept_id
LEFT OUTER JOIN C ON a.emp_id = c.emp_id