Convert oracle to JPA createQuery - oracle

SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1 WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
In the above oracle query i have to got the follupnum and folluptype in the combination commnumber and commtype. But is it possible to write this query in JPA?

JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery.
You could probably also reword your query in terms of JPQL.
If you are using EclipseLink, it does support sub-selects in the FROM clause in JPQL,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Sub-selects_in_FROM_clause

JPA fully supports SQL queries. You can use createNativeQuery(sql) or #NamedNativeQuery
once click this link

Assuming you have Foreign Keys in place between CUSTOMER_MASTER and FOLLOWUP_GUIDES
i.e. Assuming
CUSTOMER_MASTER.FOLL_UP_NUM is FK to FOLLOWUP_GUIDES.COMMNUMBER
CUSTOMER_MASTER.FOLL_UP_TYPE is FK to FOLLOWUP_GUIDES.COMMTYPE
Then your query
SELECT *
FROM
CUSTOMER_MASTER t0,
(SELECT t1.COMMNUMBER, t1.COMMTYPE FROM FOLLOWUP_GUIDES t1
WHERE (t1.STAGE_ID = '5')) t2
WHERE (t0.FOLL_UP_NUM = t2.COMMNUMBER
and T0.FOLL_UP_TYPE=t2.COMMTYPE AND (t0.COMPANY_ID = 'C001'))
is equivalent to
SELECT t0.*
FROM CUSTOMER_MASTER t0 JOIN FOLLOWUP_GUIDES t1
WHERE t0.COMPANY_ID = 'C001'
AND t1.STAGE_ID = '5'
This can be done in JPA quite easily and looks very similar to this SQL (only using object names and field names instead of table names and attribute names).
JPA JPQL query will look something like this:
SELECT t0
FROM CustomerMaster t0 JOIN FollowupGuides t1
WHERE t0.companyId = "C001"
AND t1.stageId = "5"
Can also do with JPA subquery:
SELECT t0
FROM CustomerMaster t0
WHERE t0.companyId = "C001"
AND EXISTS (SELECT 1
FROM FollowupGuides t1
WHERE t1.commnumber = t0.follUpNum
AND t1.commtype = t0.FollUpType
AND t1.stageId = "5")
=B)

Related

Rewriting query with table join containing GROUP BY clause

Is it possible to rewrite the following query
SELECT CT.GROUP, CT.EMP_ID, HT.EFF_DT
FROM CURR_TABLE CT
JOIN (SELECT GROUP, EMP_ID, MAX(EFF_DT) AS EFF_DT
FROM HIST_TABLE
WHERE STAT = 'A'
GROUP BY GROUP, EMP_ID) HT ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID
WHERE CT.GROUP = :1
AND CT.EMP_ID = :2
in a way that is similar to CROSS JOIN style?
SELECT table1.column1, table2.column2...
FROM table1, table2 [, table3 ]
The reason is that I want to create such query in Peoplesoft, and the above can only be achieved by creating a separate view for the selection with the group by clause. I want to do this just in one query without creating additional views.
You may try writing your query as a single level join with an aggregation:
SELECT
CT.GROUP,
CT.EMP_ID,
MAX(HT.EFF_DT) AS EFF_DT
FROM CURR_TABLE CT
LEFT JOIN HIST_TABLE HT
ON CT.GROUP = HT.GROUP AND
CT.EMPID = HT.EMP_ID AND
HT.STAT = 'A'
WHERE
CT.GROUP = :1 AND
CT.EMP_ID = :2
GROUP BY
CT.GROUP,
CT.EMP_ID;
Note that GROUP is a reserved SQL keyword, and you might have to escape it with double quotes to make this query (or the one in your question) work on Oracle.

JPQL query with having oracle db

I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)

How to convert my TSQL query to LINQ

I am new to linq. I can't convert this SQL code to linq. Please help me. Thanks.
DECLARE #myHID BIGINT;
SET #myHID = 1;
WITH tblChild AS
(
SELECT *
FROM wbs.WBS w
WHERE w.ParentId = #myHID
UNION ALL
SELECT w2.*
FROM wbs.WBS w2
JOIN tblChild ON w2.ParentId = tblChild.hID
)
SELECT
tblChild.hID ,
Unit.ID, w3.wbsName + ' * ' + tblChild.wbsName as
structure ,
tblChild.FK_WbsBaseStructure_hID ,
tblChild.parentID ,
unitNumber ,
unitTitle ,
FK_UsageItem_ID,
usageTitle ,
nominalArea
FROM
tblChild
INNER JOIN
unit.Unit ON tblChild.hID = Unit.FK_WBS_hID
INNER JOIN
unit.UsageItem ON Unit.FK_UsageItem_ID = UsageItem.ID
LEFT JOIN
wbs.WBS w3 ON tblChild.parentID = w3.hID
Please convert this to linq code.
Thanks.
For translating SQL to LINQ query comprehension:
Translate FROM subselects as separately declared variables.
Translate each clause in LINQ clause order, leaving monadic operators (DISTINCT, TOP, etc) as functions applied to the whole LINQ query.
Use table aliases as range variables. Use column aliases as anonymous type field names.
Use anonymous types (new { }) for multiple columns
Left Join is simulated by using a into join_variable and doing another from from the join variable followed by .DefaultIfEmpty().
Replace COALESCE with the conditional operator and a null test.
SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
Proper FULL OUTER JOIN must be handled with an extension method.

entity framework - filtering query with nullable join keys

I have a query based on the following T SQL:
select t1.field1
t2.field1,
t2.field2,
t3.field1,
t3.field2
from t1 left outer join t2 on t1.t2key = t2.id
left outer join t3 on t1.t3key = t3.id
In Linq to Entities the query takes the form
var query = db.context.t1.include(“t2”).include(“t3”);
The table t1 has an unusual structure in that fields t2key and t3key are nullable. The nullable fields are preventing me from filtering by any of the t2 or t3 fields.
The only way out I can see so far is to return the results as a database view before I perform the filtering. Or is there another approach I should be taking here?
For what it's worth I ended up using Linq in the end. If anyone else is in the same boat this is the syntax that got around the nullable issue:
query = query.Where(x => x.t1field != null ? x.t1field.t3field.Contains(model.propvalue) : false).Select(x => x);

collation conflict

Is there any one who knows how we can solve collation issue in select linq query?
I'm getting this error when I want to select data in linq.
Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation
var lstData = from s in dataTrackDB.datas
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join b in dataTrackDB.brandDatas on i.brandcode equals b.brandcode
join m in dataTrackDB.mktDatas on s.mktcode equals m.mktcode
select new dataView {
Account=m.account,
brandcode=b.brandcode,
commodity=s.commodity,
date=s.date,
daysvalid=s.daysvalid,
mfrcode=b.mfrcode,
mktcode=s.mktcode,
price=s.price,
prodid=s.prodid,
statecode=s.statecode,
subcommodity=s.subcommodity,
supprecode=s.supprecode,
units =s.units
};
lstData = lstData.AsQueryable().Where(x => x.mfrcode == mfr );
return lstData.Take(100).ToList();
The problem is not in Linq but in your database
you can for example create a view that joins that way and the select the data in linq from the view
SELECT * FROM T1
INNER JOIN T2 ON
T1.Name COLLATE Latin1_General_CI_AS = T2.Name COLLATE Latin1_General_CI_AS
or select the data first in linq2sql separately for each table and then join it with linq2object
add COLLATE DATABASE_DEFAULT at the end of the query

Resources