I have a native query in plsql like this;
create or replace package body DBPK_Monitoring is
PROCEDURE monitoring_Search(cur OUT SYS_REFCURSOR,) is
query VARCHAR(30000);
whereClause VARCHAR(10000);
BEGIN
whereClause := whereClause || ' and ( cnt.contract_status!=7)';
query := ' select itemTitle as "itemTitle", numberOfRow as "numberOfRow" ,
orderNumber as "orderNumber",createDate as "createDate",
priorityNumber as "priorityNumber"
from buy_buy_order bbo
inner join buy_buy_order_item bbi
on bbi.buy_order_id = bbo.id
left outer join core_User_Role shoUserRole
on shoUserRole.id = bbi.buy_Expert_Id
' || whereClause || ' order by ' || orderBy ||
') e WHERE e.rowNO> ' || lower_Bound || ' and e.rowNO<=' ||
upper_Bound;
OPEN cur FOR query
end monitoring_Search;
end DBPK_Monitoring;
I use of this query in repository with hql like this :
Session session = getSession();
Query query = session.getNamedQuery("monitoring_Search").setResultTransformer(Transformers.aliasToBean(MonitoringDto.class));
int firstResultIndex = searchOption.getPageNumber() * searchOption.getPageSize();
query.setInteger("lower_Bound", firstResultIndex);
query.setParameter("upper_Bound", firstResultIndex + searchOption.getPageSize());
query.setString("orderBy", searchOption.getOrder());
query.setParameter("buyOrderNumber", buyOrderNumber);
List<MonitoringDto> list = query.list();
int count = (list.size() > 0L) ? list.get(0).getNumberOfRow() : 0;
return new QueryResult<MonitoringDto>(searchOption.getPageNumber(), count, searchOption.getPageSize(), list);
When I run program, I get this error:
java.lang.illegalargumentexception no query defined for that name
[monitoring_Search]
How can I fix this?
you have to define monitoring_search as a Hibernate named query , you can see the detail about this in this link : https://www.baeldung.com/hibernate-named-query
have a nice day and kiss people around you ;)
Related
I need to get a very long string from Oracle DB in Spring App, but using a function returning clob leads to internal server error 500. How to get a custom query result containing Clob?
PL/SQL function returning a very long string in clob:
create or replace function getVeryLongString(id_ in number)
return clob as
CURSOR cur IS select x.TARGET || ' pX=' || x.px as tpx -- to get concat string in order of PX desc
from (
select distinct a.ID,
a.TARGET,
MAX(a.PX) OVER (PARTITION BY a.TARGET, a.ID) as px
from BA_ACTIVITYDATA A
where A.TARGET is not null
and a.ID = id_
order by px desc
) x;
l_data clob := '';
begin
FOR fld IN cur
LOOP
l_data := l_data || fld.tpx || ' | ';
end loop;
return rtrim( l_data, ' | ');
end
;
I use this function in Query in Java Spring app:
public interface TargetRepository extends JpaRepository<List, Long> {
#Query(
value = "select t1.fieldA,\n" +
" t1.fieldB,\n" +
" t2.fieldC,\n" +
" getVeryLongString(t3.value1) as fieldD\n" +
"from tableA t1\n" +
"join tableB t2 on t1.id = t2.id\n" +
"join tableC t3 on t3.id = t2.id\n" +
"where t.id = :id",
nativeQuery = true
)
DataTypePX getDataById(Long id);
}
TargetActivity interface looks like:
public interface DataTypePX {
String getFIELDA();
String getFIELDB();
String getFIELDC();
String getFIELDD();
}
Using the above code leads to 500 ERROR.
to fix it I need to change function getVeryLongString as returning varchar2 - it works, but it leads to cutting of result string only to 4000 symbols:
create or replace function getVeryLongString(id_ in number)
return varchar2 as
CURSOR cur IS select x.TARGET || ' pX=' || x.px as tpx -- to get concat string in order of PX desc
from (
select distinct a.ID,
a.TARGET,
MAX(a.PX) OVER (PARTITION BY a.TARGET, a.ID) as px
from BA_ACTIVITYDATA A
where A.TARGET is not null
and a.ID = id_
order by px desc
) x;
-- l_data varchar2(32767) := '';
l_data clob := '';
begin
FOR fld IN cur
LOOP
l_data := l_data || fld.tpx || ' | ';
end loop;
return rtrim( substr(l_data, 1, 4000), ' | ');
end;
My query is
sql = "SELECT pmmr.REQUEST_NO , pmel.event_datetime Event_Datetime,Pmmr.Form_No Form_No, nvl(Pmf.Form_Name, Pmmr.Form_No) formName, pmmr.MRN, nvl (p.FIRST_NAME || DECODE(p.FAMILY_NAME, NULL, '', ' ' || p.FAMILY_NAME),pmmr.MRN) PATIENT_NAME,pmmr.ASSIGNED_TO,"
+" pmmr.DRUG_GENERIC_NAME,pmmr.LAST_STATUS, nvl(initcap(( hr1.FIRST_NAME || ' ' || hr1.LAST_NAME)),pmmr.LAST_PERFORMER_ID) LastActionBy,"
+" nvl(hr2.DEPARTMENT || ' - ' || hr2.SECTION_NAME,'') ORGANIZATION_UNIT, nvl(initcap(( hr2.FIRST_NAME || ' ' || hr2.LAST_NAME)),pmmr.REQUESTER_ID) RequesterName, pmmr.REQUEST_DATE,"
+" pmmr.item_cost"
+" FROM PHRM_MFRP_MEDICATION_REQUEST pmmr"
+" join PHRM_MFRP_EVENT_LOG pmel on pmmr.REQUEST_NO = pmel.REQUEST_NO"
+" left outer join Hr_Employee hr1 on Pmmr.Last_Performer_Id = Hr1.Employee_Number"
+" left outer join Hr_Employee hr2 on Pmmr.Requester_Id = Hr2.Employee_Number"
+" left outer join EAPPTMT.PATIENT p on Pmmr.Mrn = P.Mrn"
+" left outer join Phrm_Mfrp_Form pmf on Pmmr.Form_No = Pmf.Form_No";
if (status.equals("Pending")){
}
else if(status.equals("Approved")){
sql += " WHERE pmmr.BRANCH_ID = ? AND pmel.EVENT_STATUS IN ('Pharmacy Director Approved')"
+" and Pmmr.Form_No = 1";
sql +=" and pmel.event_datetime >= (?,'dd/MM/yyyy') and pmel.event_datetime <= (?,'dd/MM/yyyy')";
sql+=" order by LAST_STATUS, EVENT_DATETIME DESC ";
} else if(status.equals("Disapproved")){
sql += " WHERE pmmr.BRANCH_ID = ? AND pmel.EVENT_STATUS IN ('Pharmacy Director Disapproved')"
+ "and Pmmr.Form_No = 1";
sql +=" and pmel.event_datetime >= (?,'dd/MM/yyyy') and pmel.event_datetime <= (?,'dd/MM/yyyy')";
sql+=" order by LAST_STATUS, EVENT_DATETIME DESC ";}
stmt = Con.prepareStatement(sql);
stmt.setString(2, startDate);
stmt.setString(3, endDate);
//tmt.setString(3, status);
stmt.setInt(1, branchId);
rs = stmt.executeQuery();
I get the error java.sql.SQLSyntaxErrorException: ORA-01797: this operator must be followed by ANY or ALL whenever i run it,
What does it mean and how can I fix it ?
Your code is very messy and should be cleaned up. When asking the questions here you usually should create a MRE in which the concrete problem is highlighed, instead of pasting your whole code base here and letting us find your bugs.
Anyways since I got interested on this I spent a moment of googling and found your question already answered here.
It says the ORA-01797 is related to the query missing the to_date() constant.
From looking at your code I can see several places where it's missing like the following one:
sql +=" and pmel.event_datetime >= /*to_date missing here!*/ (?,'dd/MM/yyyy') and
pmel.event_datetime <= /*to_date missing here!*/ (?,'dd/MM/yyyy')";
Try to fix it and then it should work. Hope it'll help you
SUBSTRING(COALESCE(
CASE
WHEN c.legal_type IS NULL THEN
' '
WHEN c.legal_type = 'INDIVIDL' THEN
c.ssn
ELSE c.tax_identifier
END
, ' '), 1, 9), c.birth_date,
CASE
WHEN c.legal_type IS NULL THEN
?
WHEN c.legal_type = 'INDIVIDL' THEN
c.first_name
ELSE ?
END
, c.middle_name, substring((
CASE
WHEN c.legal_type IS NULL THEN
' '
WHEN c.legal_type = 'INDIVIDL' THEN
c.last_name
ELSE c.business_name
END), 1, 25) surname, ?, c.company, c.operations_area, car.relationship_id, ?, ? FROM s_xref_e49_e30_e33 car INNER JOIN s_customer_e49 c ON car.customer_id = c.customer_id INNER JOIN s_credit_reporting_control_e23 crc ON car.relationship_id = crc.relationship_id INNER JOIN
(
SELECT car.account_id,
b.metro2_base_id
FROM s_metro2_base_e25 b
INNER JOIN s_credit_reporting_control_e23 crc
ON crc.credit_reporting_control_id = b.credit_reporting_control_id
INNER JOIN s_xref_e49_e30_e33 car
ON crc.relationship_id = car.relationship_id
WHERE b.metro2_report_id = ?
AND b.metro2_base_id >= ?
AND b.metro2_base_id <= ?) m2 ON m2.account_id = car.account_id WHERE car.relationship_type != 'PRIMARY'
AND
car.has_legal_relationship = 'Y')
trying to write query without COALESCE AND SUBSTRING AND CASE CONACT SINCE ENCRYPTON DOESNT SUPPORT THESE
I have an issue when executing the following SQL statement using JAVA on a ORACLE database, it works with DBeaver/SQLdeveloper but during the java process I get the following error. Any ideas ?
SQL Statement :
SELECT MIN(SUBSTR(t1.MONTH,1,4)) || 'A' || 'B' || 'CDE FGR' || ' ' || 'AT'
FROM table1 t1, table2 t2
WHERE t1.toto=t2.toto
AND t1.tata=t2.tata
AND t1.titi=t2.titi
AND t2.tutu = 'IMPACT_EUROPE_FLAG'
SQL error during java process :
org.springframework.jdbc.BadSqlGrammarException: Attempt to process next row failed; bad SQL grammar
nested exception is java.sql.SQLException: Invalid column name
Tables :
TABLE1 TABLE2
--------------
MONTH TOTO
TOTO TATA
TATA TITI
TITI TUTU
TUTU
My java process, only the reader and stepbuilder :
private Step insertBaseToFile() {
return stepBuilderFactory.get("insertBaseToFile").<GenericRecord, GenericRecord>chunk(100).reader(baseReader())
.writer(fileWriter()).build();
}
private JdbcCursorItemReader<GenericRecord> baseReader() {
JdbcCursorItemReader<GenericRecord> databaseReader = new JdbcCursorItemReader<>();
String sql = null;
sql = " SELECT MIN(SUBSTR(t1.MONTH,1,4)) || 'A' || 'B' || 'CDE FGR' || ' ' || 'AT' " +
"FROM table1 t1, table2 t2 " +
"WHERE t1.toto=t2.toto " +
"AND t1.tata=t2.tata " +
"AND t1.titi=t2.titi " +
"AND t2.tutu = 'IMPACT_EUROPE_FLAG'"
databaseReader.setDataSource(dataSource); //dataSource object from my class using JSON conf to get logs in to DB
databaseReader.setSql(sql);
return databaseReader;
}
My guess is that this is happening because the name of the column in your result is going to be:MIN(SUBSTR(T1.MONTH,1,4))||'A'||'B'||'CDEFGR'||''||'AT'
Try:
SELECT MIN( ... ) AS mycolumn
My bad.
Everything works well it was an error from me using wrong variables names.
I am trying to update a simple row using Query Annotation with JPA to Oracle 11g
This is my code:
#Transactional()
#Modifying
#Query(value="UPDATE Irregularities SET IRREST_ID = 0 WHERE IRREGS_ID = 1006", nativeQuery = true)
int updateState();
This was working fine but for some reason is not working anymore. No error, just hangs.
If I try to run the same query with Oracle SQL Developer in the same BDD works fine.
Could be a lock table problem? Why works in SQL Developer but not in Springboot?
Thank you very much for any help that you can provide.
You can find locks with that query (run it as sysdba)
select
(select username || ' - ' || osuser || ' - ' || machine from v$session where sid=a.sid) blocker,
a.sid || ', ' ||
(select serial# from v$session where sid=a.sid) sid_serial,
' is blocking ',
(select username || ' - ' || osuser || ' - ' || machine from v$session where sid=b.sid) blockee,
b.sid || ', ' ||
(select serial# from v$session where sid=b.sid) sid_serial
from v$lock a, v$lock b
where a.block = 1
and b.request > 0
and a.id1 = b.id1
and a.id2 = b.id2;