JPQL Criteria query to return details restricted by criteria on grand parent - jpa

A very simple setup:
[MASTER]
|
+
[DETAIL]
|
+
[DETAIL_DETAIL]
Asked:
return all detail_details codes
- that have a reference to detail,
- that of which has a reference to a master,
- that of which has a code of 'M2'
Effectively what I'm looking for is the JPQL Criteria equivalent of this:
select dd.code
from detail_detail dd
join detail d on dd.detail_id = d.id
join master m on d.master_id = m.id
where m.code = 'M2'
/

Related

How to Finding Array Differences using postgres SQL

I have a table in Postgres DB as below:
SOURCE
Entity
DiFF
PRD
E1,E2,E3,E4,E5
MC
E1,E2
GT1
E1,E2,E3
I Need to insert the differences Between PRD and MC,GT1 into the DIFF column using postgres SQL
Expected result
SOURCE
Entity
DiFF
PRD
E1,E2,E3,E4,E5
MC
E1,E2
E3,E4,E5
GT1
E1,E2,E3
E4,E5
This is a terrible data model, but anyways:
The following query will get the "difference" between the column separated lists (not arrays) for MC and GT1:
select source,
(select string_agg(xp.item, ',')
from data p
cross join unnest(string_to_array(p.entity, ',')) as xp(item)
where p.source = 'PRD'
and not exists (select *
from unnest(string_to_array(d.entity, ',')) as x(item)
where xp.item = x.item)) as diff
from data d
where source in ('MC', 'GT1')
Given your sample data this returns:
source | diff
-------+---------
MC | E3,E4,E5
GT1 | E4,E5
This can be used to UPDATE the table (not "insert"!)
update data
set diff = t.diff
from (
select source,
(select string_agg(xp.item, ',')
from data p
cross join unnest(string_to_array(p.entity, ',')) as xp(item)
where p.source = 'PRD'
and not exists (select *
from unnest(string_to_array(d.entity, ',')) as x(item)
where xp.item = x.item)) as diff
from data d
where source in ('MC', 'GT1')
) t
where data.source = t.source;

Sql to LINQ query LINQ Query convert

I have a SQL query I want to write in LINQ
Here is my Query
SELECT DISTINCT *
FROM [IHQDB].[dbo].[Table1] as t1
inner join Table2 as t2 on t2.Table2 =t1.ChangedItemID
inner join Table3 as t3 on t3.Table3 = t1.FromUserID
where (t1.FromUserID=1 And t2.ContentItemID= t1.ChangedItemID)
OR (t2.LastModifiedBy=1 or t2.CreatedBy=1 )
Hi now its working fine but My query little bit different on place of 1 I need my userID on base of their First Name and Last Name from M_User table.
How can I get UserId on Base of First Name + Last Name.
Here is my LINQ CODE For Retrieving User Name
linq4 = from q in context.T_ContentItems
join p in context.M_Users on q.CreatedBy equals p.UserID
where (advanceKeyword.Contains(p.EmployeeFirstName + " " + p.EmployeeLastName)) select q;
advancechk12 = linq4.ToList();
========================================================================
What I require is that wherever I have written the value "1" (e.g. t2.CreatedBy=1), I need to find the UserID. For simplicity, I am able to get the names of all the filtered users in the advancechk12. How do I retrieve the UserID's of the list of usernames returned in advancechk12
You have to replace below mentioned Linq query with your models name.I just used the same name of the T-Sql.
var t1List = (from t1 in db.Table1
join t2 in db.Table2 on t1.ChangedItemID equals t2.Id
join t3 in db.Table3 on t3.Id equals t1.FromUserID
where ((t1.FromUserID=1 && t2.ContentItemID= t1.ChangedItemID) || (t2.LastModifiedBy=1 or t2.CreatedBy=1))
select t1).Distinct().ToList();

Postgres SQL Cursor

I'm a SQL Server guy and I have a need to write some dynamic SQL in Postgres. Here's what I need. The dynamic SQL would be dependent upon integers produced by this query:
SELECT local_channel_id
FROM d_channels dc
INNER JOIN channel c ON c.id = dc.channel_id
AND c.name LIKE '%__Achv'
Using this, I need to build and execute a select and subsequent union select on the below query substituting the the values produced by the above query where indicated below by {X} (4 places):
SELECT
dmc.message_id,
dmm.received_date,
dmm.server_id,
dc.channel_id,
dmcm."SOURCE",
dmcm."TYPE",
dmm.status,
dmc.content
FROM
d_mc{X} dmc
INNER JOIN
d_mm{X} dmm ON dmc.message_id = dmm.message_id
INNER JOIN
d_channels dc ON dc.local_channel_id = {X}
INNER JOIN
d_mcm{X} dmcm ON dmcm.message_id = dmc.message_id
AND dmcm.metadata_id = 0
WHERE
dmm.connector_name = 'Source'
AND dmc.content_type = 1 --Raw
AND date(dmm.received_date) + interval '7' < now()
Can anybody help with this? I'm truly clueless when it comes to Postgres.

Entity SQL selecting from more then 3 tables

I am new to Entity framwork and currently trying hard to get used this programming paradigm. I have this query which i want to write in Entity SQL.
SELECT f.id, f.personName, c.Category, j.busCode, s.description, f.StartDate, (SELECT COUNT(*) FROM Analysis WHERE id = f.id) As numOfAnalysis
FROM forms f
INNER JOIN Jobs j ON f.id = j.id
INNER JOIN category c ON j.categoryid = c.categoryid
INNER JOIN stage s ON f.stageid = s.stageid
WHERE j.busCode NOT IN ('xyz', 'YYY')
ORDER BY startDate
I can get records from two tables but as soon as i add third table using the navigation property, i get error table category is not loaded in the current context. I am using .net 3.5. Keep in mind that EDM V2 doest not have foreign keys and i think the only way to traverse through the table relationship is navigation property.
Any help will be deeply appreciated.
Thanks
Coder74
If you use should be able to mount this Linq query.
I tried to put together, but in the rush and no such test is complicated.
You can use this reference as support: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
var result = (from f in forms
join j in Jobs on f.id equals j.id
join c in Category on j.categoryid equals c.categoryid
join s in stage on f.stageid equals s.stageid
join a in Analysis on a.id equals f.id
where !(new int[] { 'xyz', 'YYY' }).Contains(j.busCode)
orderby f.StartDate
select {
id =f.id,
personName = f.personName,
Category = c.Category,
busCode = j.busCode,
description = s.description,
StartDate = f.StartDate,
numOfAnalysis = a.Count()
}).ToList()
Julio Spader
W&S Soluções de Internet

How to access a OnetoMany join table in JPQL?

I want to write a lttl complicated query in JPQL where i access a OneToMany Join table. I get QuerySyntaxException: Pan_PanRes is not mapped.
QUERY -
query = "SELECT p FROM Pan p WHERE p.id IN " +
"(SELECT p_id FROM Pan_PanRes p_prs WHERE prs_id IN " +
"(SELECT r.id FROM PRS r where r.pant = :pant))"+
" ORDER BY pr.clD"
i tried implementing this concept in MYSQL. It works fine. So i know i am not calling the join table in right way. How should it be called then?
I would like to add MYSQL statement which works fine -
mysql> select * from pan where id not in
(select pan_id from pan_panres where panres_id in
(select id from panres where pant_id = 3));
Thanks...
Solved it myself -
query = "SELECT p FROM Pan p WHERE p.id IN " +
"(SELECT p.id FROM p.panRes prs WHERE id IN " +
"(SELECT r.id FROM PanRes r where r.pant = :pant))"+
" ORDER BY pr.clD"
Where panRes is the oneToMany variable name i have used in Pan Class.