Hibernate specification not using parameters sql server - java

I have written a custom predicate that I make use of in a JpaSpecificationExecutor query. The generated SQL for this query does not make use of a parameter, and therefore the query cache has 2 entries (as the queries differ by 1 character!). Is the use of the aggregate function what is causing the difference I am seeing? (outlined below).
My application is using sqlserver 2012 for its database and I have monitored the queries via sqlserver management studio. The output of which I have observed making use of parameters for billingType and recordedDate but not recordedValue.
Below is the predicate code I have used:
Subquery<Entity> subQuery = query.subquery(Entity.class);
Root<Entity> subQueryRoot = subQuery.from(Entity.class);
subQuery.select(subQueryRoot.get("userId"));
Optional<Predicate> teamEquals = // Call to helper
Predicate isMinutes = builder.equal(subQueryRoot.get("billingType"), BillingType.MINUTES);
Predicate minutesDate = builder.greaterThan(subQueryRoot.get("recordedDate"), LocalDate.now().minus(Period.parse(params.getHoursPeriod())));
Predicate minutesThreshold = builder.greaterThan( subQueryRoot.get("recordedValue"), params.getHours() * 60 );
Predicate minutesRestriction = builder.and(isMinutes, minutesDate, minutesThreshold);
Predicate isDocuments = builder.equal(subQueryRoot.get("billingType"), BillingType.DOCUMENTS);
Predicate documentsDate = builder.greaterThan(subQueryRoot.get("recordedDate"), LocalDate.now().minus(Period.parse(params.getDocumentsPeriod())));
Predicate documentsThreshold = builder.greaterThan( subQueryRoot.get("recordedValue"), params.getDocuments() );
Predicate documentsRestriction = builder.and(isDocuments, documentsDate, documentsThreshold);
subQuery.where( builder.and(teamEquals.get(), builder.or( minutesRestriction, documentsRestriction ) ) );
return Optional.of(subQuery);
This results in 2 generated queries differing by 1 character.
E.g. (IN HQL)
SELECT * FROM User where id IN (SELECT userId FROM Entity WHERE billingType = #p1 AND recordedDate > #p2 AND recordedValue > 0)
vs.
SELECT * FROM User where id IN (SELECT userId FROM Entity WHERE billingType = #p1 AND recordedDate > #p2 AND recordedValue > 40)

This sounds like it is the following Hibernate bug https://hibernate.atlassian.net/browse/HHH-9576
Also there appears to be a workaround documented at Why Hibernate inlines Integer parameter list passed to JPA Criteria Query?

Related

Select Top 1 records from MS SQL using Spring Data JPA

I am using the below #Query annotation to get the first few record from MS-SQL. It's showing error saying "< operator > or AS expected..."
#Query("SELECT Top 1 * FROM NEVS010_VEH_ACTV_COMMAND C WHERE C.EVS014_VIN = :vin ORDER BY C.EVS010_CREATE_S DESC")
CommandStatus findCommandStatusByVinOrderByCreatedTimestampDesc(#Param("vin") String vin);
You can also use findFirst and findTop as mentioned in the Docs:
findFirstByVinOrderByCreatedTimestampDesc(String vin)
Since the query is SQL (and not JPQL) one needs to set nativeQuery = true in the annotation:
#Query(nativeQuery = true, value = "SELECT Top 1 * FROM NEVS010_VEH_ACTV_COMMAND C WHERE C.EVS014_VIN = :vin ORDER BY C.EVS010_CREATE_S DESC")
CommandStatus findCommandStatusByVinOrderByCreatedTimestampDesc(#Param("vin") String vin);
For custom Queries without using nativeQuery, the field ROWNUM can be used.
Ex (in Kotlin but the same idea works in Java):
#Query("""
SELECT a
FROM Account a
WHERE a.bla = :ble
AND ROWNUM = 1
ORDER BY a.modifiedDate DESC
""")
fun findWhatever(#Param("ble") someParam: String)
I haven't found that on any doc so far. I just tested and it worked for Oracle, MySQL and H2

How to Write Simple Hibernate Criteria Subquery

How can I write this SQL query as a Hibernate JPA Criteria (with Restrictions, etc) in Java ?
SELECT q.*
FROM queue AS q
WHERE q.executed = false AND
q.queued_on = (SELECT min(queued_on) FROM queue WHERE item_id = q.item_id);
I only managed to write the first part like this:
getBaseCriteria()
.add(Restrictions.eq("executed", false))
// Missing Second Where Filter Here
.addOrder(Order.asc("queuedOn"))
.list();
Try to create a separate criteria instance for the subquery and simply add as another restriction as follows:
DetachedCriteria subCriteria = DetachedCriteria.forClass(Queue.class, "sub")
.add(Restrictions.eqProperty("sub.itemId","main.itemId"))
.setProjection(Projections.projectionList().add(Projections.min("sub.queuedOn")));
session.createCriteria(Queue.class, "main")
.add(Subqueries.propertyEq("main.queuedOn", subCriteria ));
.add(Restrictions.eq("main.executed", false));
.addOrder(Order.asc("main.queuedOn"))
.list();

Hibernate: Pagination with setFirstResult and setMaxResult

I'm working on a Java EE project that uses Hibernate as ORM framework.
In order to paginate the results of queries, I'm using the .setFirstResult and .setMaxResult methods (Criteria API).
The problem is that the first page is displayed correctly but when I go to page 2, I have the first result displayed equal as the last result of page one.
By switching the logging level to debug I've managed to catch the SQL query that Hibernate builds. They are:
-- First page query (results from 1 to 10)
select * from ( select this_.DT_FINE_VAL as DT1_5_0_, this_.DT_INI_VAL as DT2_5_0_, this_.CD_TIPO_PERIODO as CD3_5_0_, this_.DT_AGGIORNAMENTO as DT4_5_0_, this_.DT_INSERIMENTO as DT5_5_0_, this_.CD_USERID_AGG as CD6_5_0_, this_.CD_USERID_INS as CD7_5_0_ from GPER0_POVS2.T_POVS2_PERIODI_FUNZ this_ order by this_.CD_TIPO_PERIODO desc ) where rownum <= 10;
-- Second page query (results from 11 to 20)
select * from ( select row_.*, rownum rownum_ from ( select this_.DT_FINE_VAL as DT1_5_0_, this_.DT_INI_VAL as DT2_5_0_, this_.CD_TIPO_PERIODO as CD3_5_0_, this_.DT_AGGIORNAMENTO as DT4_5_0_, this_.DT_INSERIMENTO as DT5_5_0_, this_.CD_USERID_AGG as CD6_5_0_, this_.CD_USERID_INS as CD7_5_0_ from GPER0_POVS2.T_POVS2_PERIODI_FUNZ this_ order by this_.CD_TIPO_PERIODO desc ) row_ where rownum <= 20) where rownum_ > 10;
It seems that the second query is "wrong".
I'm using Oracle as DBMS.
Could this be an Hibernate bug? Can someone help me?
Thanks.
EDIT:
This is the code:
Session currentSession = getCurrentSession();
Criteria criteria = currentSession.createCriteria(PeriodoFunz.class);
criteria.setResultTransformer(Criteria.ROOT_ENTITY);
Order order = paginationInfo.isAsc() ? Order.asc(paginationInfo.getOrderBy()) : Order.desc(paginationInfo.getOrderBy());
criteria.addOrder(order);
....
criteria = criteria.setFirstResult(paginationInfo.getFromRecord()).setMaxResults(paginationInfo.getPageSize());
List<PeriodoFunz> result = criteria.list();
It seems that your order criteria leads to a SQL query that is not stable (returns the same result rows in different order for the queries).
You can circumvent this by adding a second order criteria for a unique attribute, e.g. the ID:
Order order = paginationInfo.isAsc() ? Order.asc(paginationInfo.getOrderBy()) : Order.desc(paginationInfo.getOrderBy());
criteria.addOrder(order);
Order orderById = paginationInfo.isAsc() ? Order.asc("id") : Order.desc("id");
criteria.addOrder(orderById);

Order by attribute of foreign entity in ORMLite

How can I build a query in ORMLite so that I can use the orderBy function (using either the one with the raw string or the parametrized one) referencing an attribute of a different entity than the one of the dao I'm building the query from? My query is built like that:
// Inner query for performances
QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
performancesQB.selectColumns("performance_id");
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);
// Outer query for Order objects, where the id matches in the performance_id
// from the inner query
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.where().isNull("user_id").and().in("performance_id", performancesQB);
ordersQB.orderByRaw("performances.date DESC");
pastOrdersQuery = ordersQB.prepare();
And the exception I'm getting whenever I try to execute this query is:
android.database.sqlite.SQLiteException: no such column: performances.date:,
while compiling: SELECT * FROM `orders` WHERE
(`user_id` IS NULL AND `performance_id` IN
(SELECT `performance_id` FROM `performances` WHERE `date` < ? ) )
ORDER BY performances.date DESC
The only solution I see here is writing a raw query myself using a JOIN instead of a nested select. May this be a good solution?
ORMLite now supports simple JOIN queries. Here the docs on the subject:
http://ormlite.com/docs/join-queries
So your query would now look something like:
QueryBuilder<Performance, String> performancesQB = performanceDao.queryBuilder();
SelectArg performanceSelectArg = new SelectArg();
performancesQB.where().lt("date", performanceSelectArg);
performancesQB.orderBy("date", false);
// query for Order objects, where the id matches
QueryBuilder<Order, String> ordersQB = orderDao.queryBuilder();
ordersQB.join(performancesQB).where().isNull("user_id");
pastOrdersQuery = ordersQB.prepare();

JPA 2.0 - NVARCHAR in native query

The project that I'm working on has the following setup: JPA 2.0 (Hibernate 4 implementation) and SQL Server 2008 R2.
I need to select some data from an SQL view. In order to do this I use a native query, but I ran into some problems with the NVARCHAR fields. Basically, when using this piece of code:
String sql = "SELECT v.text_field as address FROM SOME_CUSTOM_VIEW v
Query q = entityManager.createNativeQuery(sql,"ItemDetailsMapping");
List<Object[]> result = q.getResultList();
The ItemDetailsMapping is declared like:
#SqlResultSetMapping(name = "ItemDetailsMapping", columns = { #ColumnResult(name = "address") })
I get an exception saying:
org.springframework.orm.hibernate3.HibernateSystemException: No Dialect mapping for JDBC type: -9; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: -9
Type -9 is actually the NVARCHAR type, which we are extensively using throughout the application and it works perfectly when we are using non-native queries. Why is it not working with native queries? I even used a custom dialect and registered the type, but it's still not working.
Thanks a lot for your help
You have to associate the data type NVARCHAR to String.When using Hibernate via Session interface, you can explcitly set a type of result with addScalar() instead (also accessible via unwrap() in JPA 2.0):
So modify your code as below,
String sql = "SELECT v.text_field as address FROM SOME_CUSTOM_VIEW v"
Query q = entityManager.createNativeQuery(sql,"ItemDetailsMapping");
q.unwrap(SQLQuery.class).addScalar("address ", StringType.INSTANCE);
List<Object[]> result = q.getResultList();
Read here for more information.
(Edit 7/1/15 -- Added quotation mark for clarity)
You can do it like this:
String myquery = "select cast(t2.name as varchar) column_name from sys.objects t1 inner join sys.columns t2 on t2.object_id = t1.object_id"+
" left join sys.indexes t3 on t3.object_id = t1.object_id and t3.is_unique = 1 left join sys.index_columns t4 on t4.object_id = t1.object_id and t4.index_id = t3.index_id and t4.column_id = t2.column_id where (upper(t1.type) = 'U' or upper(t1.type) = 'V') and upper(schema_name(t1.schema_id)) = 'dbo' and upper(t1.name) = 'TEST'";

Categories