I am trying to substitute my sql query that is for Oracle and it is as :
select trim(a.id) as id from tbl_insttype a where a.chi_city='1' order by cast(a.id as int) for update skip locked
Following is an equivalent hibernate substitution :
Query query=session.createQuery("select trim(a.id) as id from TblInsttype a where a.chiCity='1' order by cast(a.id as int) ");
query.setLockMode( "a",LockMode.UPGRADE);
but my log warns that
LOG:
12830 [main] DEBUG org.hibernate.hql.internal.ast.ErrorCounter - throwQueryException() : no errors
12846 [main] WARN org.hibernate.dialect.function.TemplateRenderer - HHH000174: Function template anticipated 4 arguments, but 1 arguments encountered
12846 [main] DEBUG org.hibernate.hql.internal.ast.QueryTranslatorImpl - HQL: select trim(a.id) as id from com.imageinfo.eclear.cfg.xml.TblInsttype a where a.chiCity='1' order by cast(a.id as int)
12846 [main] DEBUG org.hibernate.hql.internal.ast.QueryTranslatorImpl - SQL: select trim(tblinsttyp0_.ID) as col_0_0_ from EXPOTDBU_PHILIPPINES.TBL_INSTTYPE tblinsttyp0_ where tblinsttyp0_.CHI_CITY='1' order by cast(tblinsttyp0_.ID as number(10,0))
12846 [main] DEBUG org.hibernate.hql.internal.ast.ErrorCounter - throwQueryException() : no errors
12861 [main] WARN org.hibernate.loader.Loader - HHH000445: Alias-specific lock modes requested, which is not currently supported with follow-on locking; all acquired locks will be [UPGRADE]
12861 [main] WARN org.hibernate.loader.Loader - HHH000444: Encountered request for locking however dialect reports that database prefers locking be done in a separate select (follow-on locking); results will be locked after initial query executes
12877 [main] DEBUG org.hibernate.SQL - select trim(tblinsttyp0_.ID) as col_0_0_ from EXPOTDBU_PHILIPPINES.TBL_INSTTYPE tblinsttyp0_ where tblinsttyp0_.CHI_CITY='1' order by cast(tblinsttyp0_.ID as number(10,0))
Hibernate: select trim(tblinsttyp0_.ID) as col_0_0_ from EXPOTDBU_PHILIPPINES.TBL_INSTTYPE tblinsttyp0_ where tblinsttyp0_.CHI_CITY='1' order by cast(tblinsttyp0_.ID as number(10,0))
12861 says some clear words that i didnt get it right.and the sql query below it also doesnt have any for update skip locked in it.
I dont get what i am doing wrong,should i try some other things?
PLUS
it is giving the same error for different lockModes.
It says Alias-specific lock modes requested, which is not currently supported with follow-on locking, so I think you should try it without aliasing your table with a. (There is only one table, is it needed at all?)
Maybe the HQL parser not recoginze the alias, you have to change hql from select trim(a.id) as id from tbl_insttype a to
select a.id from tbl_insttype a .
I have tried the count(a.id) query does not support setLockMode, will throw could not locate alias to apply lock mode : a exception.
A simple select(a) from Entity a works.
Related
We can able to fetch Hibernate SQL statements and their bind parameters But we need to fetch the elapsed time for those sql statements as well.
Already tried with below logs:
org.hibernate.SQL = debug
org.hibernate.type = trace
Also tried with statistics parameters in persistence.xml:
org.hibernate.stat = debug
It gaves only execution time for named queries not the other DMLs generated through em.persist.
Please let me know any logs parameters to fetch the round trip of all SQL statements execution time
we are using hibernate version 5.3.7
I'm working on a migration from Hibernate 3 to Hibernate 5 version. Currently, I'm using 5.4.0.Final version.
I have a problem with one of the tests that uses a repository method deleteByAllPatientFlowGens:
public void deleteByAllPatientFlowGens(List<PatientFlowGen> list) {
if (!list.isEmpty()) {
getEntityManager().createNamedQuery(DELETE_BY_ALL_PATIENT_FLOW_GENS_QUERY)
.setParameter("patient_flow_gens", list)
.executeUpdate();
}
}
It should call the named query:
#NamedQuery(name = RequestedFlowGenSettings.DELETE_BY_ALL_PATIENT_FLOW_GENS_QUERY,
query = "delete from RequestedFlowGenSettings where patientFlowGen in :patient_flow_gens")
The method receives the list with two elements, but in logs I see that both parameters are binding as 1:
2019-03-19 04:44:16,855 DEBUG [main] SQL - delete from requested_flow_gen_settings where patient_flow_gen in (? , ?)
2019-03-19 04:44:16,858 TRACE [main] BasicBinder - binding parameter [1] as [BIGINT] - [2994]
2019-03-19 04:44:16,858 TRACE [main] BasicBinder - binding parameter [1] as [BIGINT] - [2995]
2019-03-19 04:44:16,864 ERROR [main] SqlExceptionHelper - The value is not set for the parameter number 2
With previous Hibernate version (3.6.3.Final) everything worked fine. There are logs:
2019-03-19 04:47:42,275 DEBUG [main] SQL - delete from requested_flow_gen_settings where patient_flow_gen in (? , ?)
2019-03-19 04:47:42,277 TRACE [main] BasicBinder - binding parameter [1] as [BIGINT] - 2996
2019-03-19 04:47:42,277 TRACE [main] BasicBinder - binding parameter [2] as [BIGINT] - 2997
P.S. I tried to use setParameterList method, but the problem was the same.
I've found the only way to fix the problem is to execute named query for each element from the list. So, here is my repository method:
public void deleteByAllPatientFlowGens(List<PatientFlowGen> list) {
if (!list.isEmpty()) {
list.forEach(patientFlowGen -> {
getEntityManager().createNamedQuery(DELETE_BY_ALL_PATIENT_FLOW_GENS_QUERY)
.setParameter("patient_flow_gens", patientFlowGen)
.executeUpdate();
});
}
}
I know, it is a quick dirty fix. But I have no choice )
I'm trying to know if the following example works with #Formula of hibernate, perhaps someone can help to solve the other ways, to use sql or queries.
#Formula("SELECT( NOW() > column_date - interval '1 minute' * column_a )")
private Boolean columnA;
When I do that example get the following error:
2019-03-03 05:29:57.568 ERROR [service_a,4fedd46e4085e86f,4fedd46e4085e86f,false] 608 --- [nio-8090-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: schema "table_a0_" does not exist Position: 5560
2019-03-03 05:29:57.581 INFO [service_a,4fedd46e4085e86f,4fedd46e4085e86f,false] 608 --- [nio-8090-exec-1] o.h.e.internal.DefaultLoadEventListener : HHH000327: Error performing load command :
what is it wrong on my sql expression?
There should be a proper SQL query in #Formula, something like:
#Formula("select NOW() > ( column_date + interval '1 minute' * column_a ) from some_table")
Of course assuming your dbms reads this syntax. some_table is a table, where column_date and column_a are.
The easiest would be to run SQL queries against your database and insert it into #Formula when successful.
So I am trying to delete a hibernate entity from the database but it's giving me back an error saying the table doesn't exist. It doesn't seem to have any problem finding the entity and saving changes or creating new entities - it only complains when deleting an entity.
This code results in an error saying the table doesn't exist.
try
{
Transaction txn = s.beginTransaction();
Resource r = (Resource) s.get(Resource.class, rid);
s.delete(r);
txn.commit();
}
This code pulls the entity out of the database and prints the ID with no problems.
try
{
Transaction txn = s.beginTransaction();
Resource r = (Resource) s.get(Resource.class, rid);
System.out.println(r.getId());
txn.commit();
}
Console output
Hibernate:
select
resource0_.id as id1_1_0_,
resource0_.bookable as bookable2_1_0_,
resource0_.description as descript3_1_0_,
resource0_.name as name4_1_0_
from
resources resource0_
where
resource0_.id=?
Hibernate:
delete
from
resources_resources
where
resources_id=?
2016-02-07 02:27:13.831 WARN 8396 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1146, SQLState: 42S02
2016-02-07 02:27:13.831 ERROR 8396 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper : Table 'bargaink_megtest.resources_resources' doesn't exist
2016-02-07 02:27:13.831 INFO 8396 --- [nio-8080-exec-8] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
I have no idea how this could happen when it can quite clearly see the table when adding new entities and updating existing entities. Can anyone explain what is going on here?
Thanks
One possible explanation is that maybe you would be that your hibernate.hbm2ddl.auto is set to create. If it's so, then please change it.
change:
<property name="hibernate.hbm2ddl.auto">create</property>
to
<property name="hibernate.hbm2ddl.auto">update</property>
in your hibernate.cfg.xml file.
Kindly post your hibernate.cfg.xml file if it the above solution doesn't work. We'll dig deeper into your code then.
I can't figure why I am getting this error and have been banging my head against a wall for a few hours now.
session.beginTransaction();
#SuppressWarnings("unchecked")
List<ActAsUser> actAsUser = (List<ActAsUser>) session.createCriteria(ActAsUser.class)
.add(Restrictions.eq("actAsID.userID", proxyID))
.add(Restrictions.eq("actAsID.targetID", targetID))
.list();
session.delete(actAsUser.get(0));
session.getTransaction().commit();
The code above gives me an error when trying to delete any row in the database that I have not added using Hibernate i.e. all the existing rows of the database that were entered through regular SQL.
Error: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
If I insert a row using Hibernate in my application I can then delete that row absolutely fine.
The thing I can't understand is that I get a user from the database first so I know that the user exists.
I've checked the Hibernate SQL and the only difference I can see is in the following when the select statement is run:
Working
/* criteria query */ select
this_.PROXY_USER_ID as PROXY1_12_0_,
this_.TARGET_USER_ID as TARGET2_12_0_,
this_.EFFCT_DATE as EFFCT3_12_0_,
this_.INCIDENT_NBR as INCIDENT4_12_0_,
this_.TARGET_USER_NAME as TARGET5_12_0_,
this_.EXPIRY_DATE as EXPIRY6_12_0_
Not Working
/* criteria query */ select
this_.PROXY_USER_ID as PROXY1_16_0_,
this_.TARGET_USER_ID as TARGET2_16_0_,
this_.EFFCT_DATE as EFFCT3_16_0_,
this_.INCIDENT_NBR as INCIDENT4_16_0_,
this_.TARGET_USER_NAME as TARGET5_16_0_,
this_.EXPIRY_DATE as EXPIRY6_16_0_
There is a difference in the numbers appended to the end of the columns, can anyone tell me what these actually mean and whether this could be the issue as this is the ONLY difference I can find between the two queries.
Just worked it out
Hibernate Query for Delete statemenet not working:
Hibernate:
/* delete com.package.ActAsUser */ delete
from
CONFIG_ACTAS_PROXY
where
PROXY_USER_ID=?
and TARGET_USER_ID=?
and EFFCT_DATE=?
13:39:01,460 TRACE BasicBinder:82 - binding parameter [1] as [VARCHAR] - A534651
13:39:01,460 TRACE BasicBinder:82 - binding parameter [2] as [VARCHAR] - A512058
13:39:01,460 TRACE BasicBinder:82 - binding parameter [3] as [DATE] - 2014-11-03
PROXY_USER_ID TARGET_USER_ID EFFCT_DATE
A534651 A512058 2014-11-03 04:24:44
It's the time set on the date that does it.
When adding rows through hibernate the date looks like:
2014-11-03 00:00:00
Other rows in my database look like
2014-11-03 04:24:44
When I edit them and make the time 00:00:00 the delete then works.
Changing the hibernate mapping to map to a timestamp type instead of date fixes the problem in my application too.