please help to write the query to fetch specific column by joining multiple table in jpa - jpa

#Query("select new com.example.hallticket.model.AspirantDetailModel(
ah.applicantName,
ah.applicantEmailId,
ah.applicantHallticketNumber,
ac.applicantCredId,
casbG.shortName,
ecitym.cityName,
es.examDate,
es.examSlotName,
cm.centerCode,
cm.centerName,
cm.centerAddress)
from ApplicantHallticket ah
join ah.applicantCredential ac
join ah.examSlotwiseReportingtime eswrt
join ah.applicantCenterAllotment aca
join eswrt.casbGroup cg
join eswrt.examSlot es
join aca.examCenterMapping ecm
join ecm.centreExamslotMapping cesm
join ecm.regionCentreMapping rcm
join rcm.examCityMaster ecitym
join rcm.centerMaster cm
where ah.applicantHallticketNumber=?1")
public AspirantDetailModel getAspirantDetails(String hallticketNo);

Related

JPA join and join fetch one attribute at the same time

Is the following JPA query valid? FETCH join and join one attribute at the same time:
SELECT mag FROM Magazine mag LEFT JOIN FETCH mag.articles
LEFT JOIN mag.articles a WHERE mag.id = 1 and a.name like "%FOO"
Join the Article table twice?

PostgreSQL Inner Join Where Get IDs of 3 Products

I have these 5 tables which is sale_order directly connected to sale_order_line that has the products of the order of course which is connected to product_product that is connected to product_template where a table called mrp_bom can connect to product template and a table connected to it which is bom_line. I am trying to Output the products that contain the word 836g
Here is what I have so far:
Select so.name,
pt.name,
sol.name
From sale_order so
Inner Join sale_order_line sol
On so.id = sol.order_id
Inner Join product_template pt
On sol.product_id = pt.id
Inner Join mrp_bom bom
On pt.id = bom.product_tmpl_id
Inner Join mrp_bom_line boml
On bom.id = boml.bom_id
Where boml.product_id = (Select id From product_template Where name Like '%836g%'
Order By so.name
This code outputs an error because I have 3 items with 836g. I tried changing the = in the condition into IN so it will grab all the ids it will return. But still it's giving me an error. This is the best I have so far, I have tried so many times, but I can't figure it out.
I have the feeling that something along these lines is what you actually want:
SELECT so.name,
pt.name,
sol.name
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN product_template pt
ON sol.product_id = pt.id
INNER JOIN mrp_bom bom
ON pt.id = bom.product_tmpl_id
INNER JOIN mrp_bom_line boml
ON bom.id = boml.bom_id
WHERE pt.name LIKE '%836g%' -- just add a WHERE condition directly in your query
ORDER BY so.name
OMG! I just had the answer. I don't think this will help anyone but i'll post the answer i have
SELECT so.name,
pt.name PT,
sol.name SOL,
(Select pp1.name_template From product_product pp1 Where boml.product_id = pp1.id) BOML,
boml.product_id
FROM sale_order so
INNER JOIN sale_order_line sol
ON so.id = sol.order_id
INNER JOIN product_template pt
ON sol.product_id = pt.id
INNER JOIN mrp_bom bom
ON pt.id = bom.product_tmpl_id
INNER JOIN mrp_bom_line boml
ON bom.id = boml.bom_id
WHERE (Select pp1.name_template From product_product pp1 Where boml.product_id = pp1.id) LIKE '%836g%'
ORDER BY so.name
Thank you all for the effort of helping me! Great community we got here! :D

I use inner join but records are duplicating though I use "Distinct" and "Group By" in SQL Server 2008 R2 why?

This is the query when I run this it gives me duplicate records but I remove the INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleID and the column PLOH.OderBookID, it gives records without duplicating but I need records with column name PLOH.OderBookID how can I solve this problem ?
SELECT
CPO.ID
,CPO.StyleID
,cpo.CustomerPo
,PLOH.OderBookID
,cpo.Process
,CPD.Qty
,cpd.RefID
,cpd.Uom
,UM.MeshType
,PRC.Name
,STC.NewStyleno
,SD.TypeID
,itm.Name Part
,SD.ColorID
,COL.Name Color
,SD.FabricID
,FAB.Name Faric
,SHD.ItemID
,ITD.Name Item
,SHD.Image
FROM CustomerPO_Header CPO
INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID
INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID
INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID
INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID
INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID
INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID
INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID
INNER JOIN UOM UM ON Um.ID=CPD.Uom
INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID
INNER JOIN process PRC ON PRC.ID=CPO.Process
INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleID
Plz help me
It's hard to test without knowing what data you have. However you could try moving the table that is causing your duplicates into a subquery instead of a join. so something like:
SELECT
CPO.ID
,CPO.StyleID
,cpo.CustomerPo
,(SELECT PLOH.OderBookID FROM planingOrderBookHeader PLOH WHERE sd.StyleID=PLOH.StyleID) AS OderBookID
,cpo.Process
,CPD.Qty
,cpd.RefID
,cpd.Uom
,UM.MeshType
,PRC.Name
,STC.NewStyleno
,SD.TypeID
,itm.Name Part
,SD.ColorID
,COL.Name Color
,SD.FabricID
,FAB.Name Faric
,SHD.ItemID
,ITD.Name Item
,SHD.Image
FROM CustomerPO_Header CPO
INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID
INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID
INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID
INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID
INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID
INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID
INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID
INNER JOIN UOM UM ON Um.ID=CPD.Uom
INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID
INNER JOIN process PRC ON PRC.ID=CPO.Process
Of course this may have performance implications.

JPQL left outer join does unnecessary joins

I've got the following JPQL :
SELECT a.b.id, a.b.name, a.c.id,a.c.name
left join a.b left join a.c
group by a.b.id,a.b.name,a.c.id,a.c.name
now b and c are both referencing the same table.
the generated SQL is doing the left join I asked, and another join for a.b.name and a.c.name
(which is unnecessary because the left join includes the name, and it retrieves more results than expected)
how do I make the SQL generated not include the unnecessary join?
1 solution came up is not select the names and retrieve them individually by a different query.. but it's not the most elegant way I suppose..
(btw I tried selecting a.b,a.c and group by a.b,a.c but it throws ORA not a group by expression because the generated sql retrieves all rows but group by is only by ID)
and the left join is necessary since I want to allow null values.
Thanks a lot.
SELECT a.b.id, a.b.name, a.c.id,a.c.name
The above implicitly creates an inner join between a abd b,a nd another inner join between a and c. The query should be
select b.id, b.name, c.id, c.name
from A a
left join a.b b
left join a.c c
The group by clause doesn't make any sense, since you have no aggregate in your select clause. group by would be useful if you had, for example
select b.id, b.name, c.id, c.name, count(c.foo)
from A a
left join a.b b
left join a.c c
group by b.id, b.name, c.id, c.name

Using multiple resultsets combined with joins in T-SQL

I'm currently using joins inside my stored procedures for outputting elements from different tables. An aggressive example
select a.*, b.*, c.*, d.*, e.*, f.* from tableA a
join tableB b on a.id = b.foreignid
join tableC c on b.id = c.foreignid
join tableD d on c.id = d.foreignid
join tableE e on d.id = e.foreignid
join tableF f on e.id = f.foreignid
where a.id = 1
It's getting pretty unhandy to work with when mapping the output to entities in my C# code, since I have to maintain a lot of boilerplate code.
Instead I would look into using multiple resultsets, so that I could map each resultset into an object type in code.
But how would I go around achieving this when I my case the different results would all relate to each other? The examples I've been able to find all revolved around selecting from different tables where the data were not related by foreign keys like mine. If I were to ouput my result in multiple resultsets the only thing I can come up with is something like this
select a.* from tableA
where a.id = 1
select b.* from tableB
join tableA a on a.id = b.foreignid
where a.id = 1
select c.* from tableC
join tableB b on b.id = c.foreignid
join tableA on a.id = b.foreginid
where a.id = 1
select d.* from tableD
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
select e.* from tableE
join tableD d on d.id = e.foreignid
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
select f.* from tableF
join tableE e on e.id = f.foreignid
join tableD d on d.id = e.foreignid
join tableC c on c.id = d.foreignid
join tableB b on b.id = c.foreignid
join tableA a on a.id = b.foreignid
where a.id = 1
But this is not cleaner, a lot more ineffecient (I would suppose, since there's alot more join statements)
Is it possible to use multiple resultset in this way I'm trying to? I just don't know how I would write the sql statements in the stored proc without having to do massive joins per resultset as in the example. And with the current solution I get an explosion of columns since I join them all together
You can actually return multiplte resultsets from a single SP and consume them in c#, check this post for instance : http://blogs.msdn.com/b/dditweb/archive/2008/05/06/linq-to-sql-and-multiple-result-sets-in-stored-procedures.aspx
It's a lesser known feature but sounds like what you're asking for. You don't have to join them and return them as a flattend rowset, just get the seperate rowsets and piece them together in memory.
Also you may want to read up on ORM frameworks, that could save you a lot of typing that you coud spend on features if it fits your needs.
https://stackoverflow.com/questions/249550/what-orm-frameworks-for-net-do-you-like-best
Regards GJ