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;
Related
Hi i have a long query for a spring, java 8, hibernate, mysql application, which works as expected, except the last part, which sums the reserved items in store:
SELECT new example.package.RaktarKimutatasDTO(rk.cikkId.cikkName,
CONCAT(SUM(rk.mennyisegMe1),' ', rk.me1.unitCode, ' / ', SUM(rk.mennyisegMe2),' ', rk.me2.unitCode, ' / ', SUM(rk.mennyisegMe3),' ', rk.me3.unitCode),
CONCAT(AVG(rk.listaar), ' Ft'),
CONCAT(rk.mennyisegMe1*rk.listaar, ' Ft'),
rk.raktarId.raktarNeve, rk.cikkId.vtsz.vatCode,
SUM(CASE WHEN rk.vevoiEntity IS NOT NULL THEN rk.mennyisegMe1 ELSE 0.0 END))
FROM RaktarkeszletEntity rk
WHERE rk.raktarId.identifier IN ?1 AND rk.createDate<?2 AND (rk.bejovoszamlaEntity LIKE 'RB%' OR (rk.bejovoszamlaEntity LIKE 'BM%' AND rk.bevetel = TRUE))
GROUP BY rk.cikkId
The problem is, the SUM(CASE... ) part is always returns 0.
While if i use the (nearly) same query in in plain SQL, from Mysql workbench, it works as expected, sums the quantities for items which have "vevoiEntity" which is not null (so the reserved quantity).
vevoiEntity field in DB has int type, which can be null too.
The fields, called entity (except of course which used at FROM) are not entities, but simple integer. (previous developer had problems with naming too).
I've tried using more parentheses, tried " <> NULL" tried "vevoiEntity>0", etc but nothing helped, the case is always false, every other parts are perfect.
Why does it works differently in native SQL, and JPQL? I set hibernate.show_sql=true, and i saw that the generated query for the SUM CASE part, is just like the query that i used in workbench, so the translation seems to be ok.
plain SQL, (already working parts are left out) which works:
SELECT rk.cikk_id, SUM(CASE WHEN rk.vevoi_tetel_id IS NOT NULL THEN rk.mennyiseg_me_1 ELSE 0 END) as reserved FROM nast_raktarkeszlet rk GROUP BY rk.cikk_id;
or:
SELECT rk.cikk_id, CONCAT(SUM(rk.mennyiseg_me_1),' ', rk.me_1, ' / ', SUM(rk.mennyiseg_me_2),' ', rk.me_2, ' / ', SUM(rk.mennyiseg_me_3),' ', rk.me_3), CONCAT(AVG(rk.listaar), ' Ft'), CONCAT(rk.mennyiseg_me_1*rk.listaar, ' Ft'), rk.raktar_id, rk.cikk_id, SUM(CASE WHEN IFNULL(rk.vevoi_tetel_id, 0) >0 THEN rk.mennyiseg_me_1 ELSE 0 END) FROM nast_raktarkeszlet rk GROUP BY rk.cikk_id
generated query:
select
basecikkek1_.cikk_name as col_0_0_,
concat(sum(rak0_.mennyiseg_me_1),
' ',
men2_.unit_code,
' / ',
SUM(rak0_.mennyiseg_me_2),
' ',
men3_.unit_code,
' / ',
SUM(rak0_.mennyiseg_me_3),
' ',
men4_.unit_code) as col_1_0_,
concat(avg(rak0_.listaar),
' Ft') as col_2_0_,
concat(rak0_.mennyiseg_me_1*rak0_.listaar,
' Ft') as col_3_0_,
rak5_.raktar_name as col_4_0_,
vts7_.vat_code as col_5_0_,
sum(case
when rak0_.szallitolevel_tetel_id is not null
or rak0_.kimeno_tetel_id is not null
or rak0_.vevoi_tetel_id<>0 then rak0_.mennyiseg_me_1
else 0
end) as col_6_0_
from
nast_raktarkeszlet rak0_ cross
join
nast_cikkek basecikkek1_ cross
join
nast_vtsz vts7_ cross
join
nast_units men2_ cross
join
nast_units men3_ cross
join
nast_units men4_ cross
join
nast_raktarak rak5_
where
rak0_.cikk_id=basecikkek1_.id
and basecikkek1_.vtsz=vts7_.id
and rak0_.me_1=men2_.id
and rak0_.me_2=men3_.id
and rak0_.me_3=men4_.id
and rak0_.raktar_id=rak5_.id
and (
rak0_.raktar_id in (
? , ? , ? , ?
)
)
and rak0_.create_date<?
and (
rak0_.parentid like 'RB%'
or (
rak0_.parentid like 'BM%'
)
and rak0_.bevetel=1
)
group by
rak0_.cikk_id
OMG my bad. The WHERE conditions are exclude the records that have non null "vevoientity"
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 ;)
I have several SQL queries like this (about 10.000):
SELECT (COLUMN BODY)
FROM (TABLES BODY)
WHERE...
The where part is optional.
I need to extract the "COLUMN BODY" AND "TABLES BODY" from the query, but those are dynamic (I have found SQL functions and subqueries a lot)
Think as getting an vector with the divided query or something like that:
String result = [SELECT, "COLUMN BODY", FROM, "TABLES BODY", WHERE, ... ]
The purpose, I'm working in an "SQL translator" and I need extract those parts from that dynamic queries. I have tried string splitting and other but the result is so frustrating, so I'm looking for an alternative to achieve that. I'm working on a java project.
What is the best way to get that?
Edit
Those are examples of the queries:
SELECT COMPANIA, ANO, TIPO_COBRO, CODIGO, DESCRIPCION FROM SF_DESCRIPCIONES_ESTAND
SELECT AUT_FAMILIARES.COMPANIA, AUT_FAMILIARES.DCTO_EMPLEADO, AUT_FAMILIARES.SUCURSAL_EMPLEADO, AUT_FAMILIARES.IDENTIFICACION, AUT_FAMILIARES.SUCURSAL, AUT_FAMILIARES.CONSECUTIVO, AUT_FAMILIARES.DCTO_IDENTIDAD, AUT_FAMILIARES.PARENTESCO, AUT_FAMILIARES.NOMBRE, AUT_FAMILIARES.FECHANCTO, AUT_FAMILIARES.SEXO, AUT_FAMILIARES.OBSERVACIONES, AUT_FAMILIARES.POLIZA, AUT_FAMILIARES.SALUD, AUT_FAMILIARES.PORCENTAJE, AUT_FAMILIARES.OCUPACION, AUT_FAMILIARES.TELEFONO, AUT_FAMILIARES.POS, AUT_FAMILIARES.AUXILIO_EDUCATIVO, AUT_FAMILIARES.APELLIDO1, AUT_FAMILIARES.APELLIDO2, AUT_FAMILIARES.DIRECCION, AUT_FAMILIARES.ESTADO_ACTUAL, AUT_FAMILIARES.DCTO_IDENTIDAD_A, AUT_FAMILIARES.PARENTESCO_A, AUT_FAMILIARES.NOMBRE_A, AUT_FAMILIARES.SEXO_A, AUT_FAMILIARES.OBSERVACIONES_A, AUT_FAMILIARES.POLIZA_A, AUT_FAMILIARES.SALUD_A, AUT_FAMILIARES.PORCENTAJE_A, AUT_FAMILIARES.OCUPACION_A, AUT_FAMILIARES.TELEFONO_A, AUT_FAMILIARES.POS_A, AUT_FAMILIARES.AUXILIO_EDUCATIVO_A, AUT_FAMILIARES.APELLIDO1_A, AUT_FAMILIARES.APELLIDO2_A, AUT_FAMILIARES.DIRECCION_A, AUT_FAMILIARES.ESTADO_ACTUAL_A, AUT_FAMILIARES.ESTADO, AUT_FAMILIARES.DESTINATARIO, AUT_FAMILIARES.SUCURSAL_DESTINATARIO, AUT_FAMILIARES.TIPO_SOLICITUD, AUT_FAMILIARES.FECHA_APROBACION, AUT_FAMILIARES.ENVIADO, AUT_FAMILIARES.ACTUALIZADO, AUT_FAMILIARES.RUTA, AUT_FAMILIARES.CREATED_BY, AUT_FAMILIARES.DATE_CREATED, AUT_FAMILIARES.MODIFIED_BY, AUT_FAMILIARES.DATE_MODIFIED, AUT_FAMILIARES.N_IDENTIFICACION, AUT_FAMILIARES.EDAD, AUT_FAMILIARES.EDAD_A FROM AUT_FAMILIARES
SELECT DISTINCT CARGOS.ID_DE_CARGO, CARGOS.NOMBRE_DEL_CARGO FROM EV_EVALUADOR_EVALUADO EVAL INNER JOIN CARGOS ON EVAL.COMPANIA = CARGOS.COMPANIA AND EVAL.CARGO_EVALUADO = CARGOS.ID_DE_CARGO WHERE EVAL.COMPANIA = :COMPANIA AND EVAL.CLASE_EVALUACION = :CLASEEVALUACION AND CARGOS.ID_DE_CARGO >= :CODIGO ORDER BY CARGOS.ID_DE_CARGO
SELECT ROWNUM CODIGO, DECODE (ROWNUM, 1,'Activo',2,'Pensionado',3,'Retirado',4,'Suspensión Preventiva',5,'Activarlo para Ajustes Liquidación',6,'Comisión',7,'Encargos')NOMBRE FROM DUAL CONNECT BY ROWNUM < 8
SELECT * FROM ( SELECT A.*, ROWNUM RNUM FROM(SELECT * FROM (SELECT INVENTARIO.CODIGOELEMENTO, INVENTARIO.NOMBRELARGO FROM INVENTARIO WHERE INVENTARIO.COMPANIA = :COMPANIA AND INVENTARIO.TIPO NOT IN ('C') ORDER BY INVENTARIO.COMPANIA, INVENTARIO.CODIGOELEMENTO) WHERE (CASE WHEN CODIGOELEMENTO IS NULL THEN ' ' ELSE UPPER(CODIGOELEMENTO) END) LIKE UPPER(:CODIGOELEMENTO || '%') AND (CASE WHEN NOMBRELARGO IS NULL THEN ' ' ELSE UPPER(NOMBRELARGO) END) LIKE UPPER( '%' || :NOMBRELARGO || '%') ) A WHERE ROWNUM <= (:PAGINICIO + :PAGTAMANIO)) WHERE RNUM > :PAGINICIO
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 am trying to do a full text search in a postgres database using jOOQ. The following line works:
Result res = pgContext.select()
.from(products.PRODUCTS)
.where("to_tsvector(title || ' ' || description || ' ' || tags) ## to_tsquery('" + query + "')")
.fetch();
But when I add variable binding, to protect from SQL injection, I no longer get results:
Result res = pgContext.select()
.from(products.PRODUCTS)
.where("to_tsvector(title || ' ' || description || ' ' || tags) ## to_tsquery('?')", query)
.fetch();
Any ideas?
Thanks and good day
Since #posz did not post his comment as an answer, and it has been quite a while, I'll post his reply as an answer myself for clarity.
Try ... to_tsquery(?) ... -- the binding mark ? won't work inside a
literal.