I am using the Datatables jQuery plugin to display a List of orderpositions.
While debugging my application I stumbled upon the following inconsistency.
To retrieve the data, I used the #Query-Notation for my custom query:
#Query("select op from OrderPosition " +
"op where op.order.created >= ?1" +
" and op.depot in ?2" +
" and op.order.deliveryAddress.deliveryMode in ?3" +
" and op.pickStatus in ?4 and op.order.id like ?5 ")
Page<OrderPosition> findOrderpositionsByFilterStatus(Date date, List<Integer>depots, List<DeliveryMode> deliveryModes, List<ArticleStatusType> pickStatus, String sSearch, Pageable p);
The error occured is the following:
I have a set of 81 Orderpositions test data. In the frontend I am able to filter by several criteria. One of the criteria is the deliveryMode (express|standard). I display 10 Entries at a time. The total number of expressdeliveries is 6. When paging through the Pages only 2 express are displayed. When filtering explicitely on express I get all 6.
When I add some sort of ordering e.g:
#Query("select op from OrderPosition " +
"op where op.order.created >= ?1" +
" and op.depot in ?2" +
" and op.order.deliveryAddress.deliveryMode in ?3" +
" and op.pickStatus in ?4 and op.order.id like ?5 " +
"order by op.order.id desc")
Page<OrderPosition> findOrderpositionsByFilterStatus(Date date, List<Integer>depots, List<DeliveryMode> deliveryModes, List<ArticleStatusType> pickStatus, String sSearch, Pageable p);
I get all 6 of 'em.
What brings me to the question:
When using non annotated queries - either the generic findAll() or queries from method names- does Spring-Data-JPA use an order by-clause internally, when detecting a pageable? Or inverse: Do I have to add an order by-clause in each of my custom queries, when using a pageable?
Yes inded. Spring Data uses an OrderBy (with a default desc) if you do not specify further.
Look at the Logs:
SELECT t0.oid, t0.jpaversion, t0.amount, [...]
FROM [...]
WHERE [..] AND (t13.jpatype IS NULL OR t13.jpatype IN (?))
ORDER BY t0.oid DESC LIMIT ?, ?
Related
I have a spring boot app connected to oracle DB.
I am trying to order a list of records and select the top most record.
I wrote a JPA query as below but it fails.
#Query("SELECT id FROM UploadedFile uploadedFile "
+ "WHERE uploadedFile.p_file_type = 'branch' "
+ "and p_file_status='Processed' "
+ "and p_is_file_process_with_error = 0 "
+ "order by c_created_date desc "
+ "FETCH FIRST 1 rows only ")
public String findLatestBranchCodeFile();
The error received was
creating bean with name 'uploadedFileRepo': Invocation of init method
failed; nested exception is java.lang.IllegalArgumentException:
Validation failed for query for method public abstract
java.lang.String
com.rhb.pintas.repo.UploadedFileRepo.findLatestBranchCodeFile()!
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:
FETCH near line 1, column 204 [SELECT id FROM
com.rhb.pintas.entities.UploadedFile uploadedFile WHERE
uploadedFile.p_file_type = 'branch' and p_file_status='Processed' and
p_is_file_process_with_error = 0 order by c_created_date desc FETCH
FIRST 1 rows only ] -> [Help 1]
The issue seems to be with fetch,not sure whats wrong.
It seems you are mixing HQL and native query dialects:
If this will be a naviveQuery (like most of the columns would mention), then replace the entity name with table name and add nativeQuery option. And because You are using only a single table, You can skip the alias name:
#Query("SELECT id FROM uploaded_file "
+ "WHERE p_file_type = 'branch' and p_file_status='Processed' and "
+ "p_is_file_process_with_error = 0 "
+ "order by c_created_date desc "
+ "FETCH FIRST 1 rows only ", nativeQuery = true)
public String findLatestBranchCodeFile();
If You want to keep it as a HQL, then replace all column names with entity property names, like p_file_type > fileType (I guess column names). Secondly You will need to pass a Pageable parameter, to replace Your 'Fetch first' statement.
You can find more materials here:
Bealdung
NativeQ
StackOverflow
You are trying to execute SQL query, in this case you need to add nativeQuery=true attribute to #Query annotation
UPD.
got confused because FETCH FIRST - is a SQL syntax, for JPA way please check another solution - it is possible to return list with at most one element.
I guess, you can try passing pagable to limit result set size and unlimit your query:
public String findLatestBranchCodeFile(Pageable pageable); // pass new PageRequest(0,1)
I am very fresh java beginner and I am trying to implement possibility to review documents. User, who belongs to a certain group can review documents of particular types that are assigned to that group's review document types. User can not review own documents. When user belongs to many groups that can review many document types, sometimes I get about 1500~ documents that meet query condition. I tried paging, but still there are some visible latency in front page. When I fetch all documents (~1500) it takes about 3000+ ms. I am not sure if it is a lot or not. My JPA query is below.
#Query("SELECT d FROM Document d WHERE d.id IN (SELECT DISTINCT d.id FROM Document d" +
" JOIN d.documentType dt" +
" JOIN dt.reviewUserGroups rug" +
" JOIN rug.users u WHERE u.username = :username" +
" AND d.documentState = it.akademija.wizards.enums.DocumentState.SUBMITTED" +
" AND u <> d.author" +
" AND (lower(CONCAT(d.author.firstname, ' ', d.author.lastname)) like %:searchFor% " +
" OR lower(d.title) like %:searchFor%" +
" OR lower(d.description) like %:searchFor%" +
" OR lower(d.id) like %:searchFor%" +
" OR lower(dt.title) like %:searchFor%))")
Page<Document> getDocumentsForReview(#Param(value = "username") String username,
#Param(value = "searchFor") String searchFor,
Pageable pageable);
Please let me know what is the best way to improve performance, because I have some feeling that this type of search using "like" multiple times in query and joining through many Entities is not the right one.
Use fulltext search instead of like. Create Fulltext index for that, see http://www.h2database.com/html/tutorial.html?highlight=fulltext&search=fulltext#fulltext
Edit-
I'll add the use case to clear up the function of this.
The user will select two dates - a start date and an end date - these are then passed on and used to select the tables (each year has its own table). In one use case where the two given dates lie in the same year it's a simple query on that table alone.
However, if the two dates are different years I will need to join all tables (so 2011-2013 will be three tables connected, to search through) and thus, I want a dynamic fix to this. I know building up a query like below is against security - just thought something similar would work. As the system will get new tables each year I also dont want to have to manually add however many new queries for each case (2011-2016, 2014-2018, 2011-2019.. etc)
I have a question about whether it is possible to create a dynamic query as a String like below and then pass that through to service -> repository, and use that as a query?
for (int i = 0; i < yearCondition; i++) {
if (i == 0) {
query += "SELECT md.Device_ID, l.locationRef, " + reportRunForm.getStartDate() + " as 'From Date', "
+ reportRunForm.getEndDate() + " as 'To Date' "
+ "from mData.meterdata" + iDateStart.substring(0, 4)
+ " join MOL2.meters m on device_ID = m.meterUI "
+ "join MOL2.locations l on m.locationID = l.locationID "
+ "join MOL2.meterreg mr on m.meterID = mr.meterID "
+ "where mr.userID = ?1";
}
query += "UNION SELECT md.Device_ID, l.locationRef, " + reportRunForm.getStartDate() + " as 'From Date', "
+ reportRunForm.getEndDate() + " as 'To Date' "
+ "from mData.meterdata" + (Integer.parseInt(iDateStart.substring(0, 4))+i)
+ " join MOL2.meters m on device_ID = m.meterUI "
+ "join MOL2.locations l on m.locationID = l.locationID "
+ "join MOL2.meterreg mr on m.meterID = mr.meterID "
+ "where mr.userID = ?1";
}
I may have the wrong idea with how this works, and I know I could create and persist a query through entitymanager, but wanted to know whether doing it through the repository would be possible?
My thought was I'd build up the query like above, pass it through to service and then to repository, and bind it as value in #Query annotation but this doesn't seem possible. I'm likely approaching this wrong so any advice would be appreciated.
Thanks.
Edit -
Had a goof. Understand doing it at all like that is stupid, an approach to build up something similar is what I'm looking for that is still secure.
Maybe this annotations before your POJO can help
#org.hibernate.annotations.Entity(dynamicInsert = true)
for example two tables district and constituency ...
Dynamic query
query += "select *from constituency c where 1=1";
if(constituencyNumber!=null)
query +=" and c.constituency_number like '"+constituencyNumber+"%'";
query += " group by c.district_id";
OR
select *from constituency c where (c.constituency_number is null or c.constituency_number like '1%') group by c.district_id;
I'm building a REST service using Hibernate, Spring HATEOAS and Jackson. I am adding a method which returns a JSON representation of the results of a query like the one below;
SELECT ERRORS.DMN_NAM, CODES.MSG_TXT,
FROM SERV_ERR ERRORS, EVENT_CD CODES
WHERE ERRORS.SERV_RESP_CD_TXT = CODES.CD_TXT
GROUP BY ERRORS.DMN_NAM, ERRORS.SERV_NAM, CODES.MSG_TXT,
ERRORS.SERV_ERR_CNT, ERRORS.ERR_TS_NUM
ORDER BY ERRORS.DMN_NAM, CODES.MSG_TXT
I currently have two objects defined (ErrorsEntity and EventCodeEntity) which map to the tables SERV_ERR and EVENT_CD.
So the results of this query will be a list, but not of ErrorsEntity or EventCodeEntity but rather an amalgamation of the two entities.
Up to now, my queries have all returned objects that map directly to one table like so:
public List<ErrorsEntity> getErrors(double daysPrevious, double hoursToShow);
What's the best way to handle this in Hibernate where the results of a query aren't objects that are mapped to a single table and how can I write this query in HQL?
It's better to stick to an SQL query then, since HQL makes sense only when you plan on changing states from the resulted entities. In your case, the SQL is a better alternative, since it doesn't really follow the standard and you only want a projection anyway. You could remove the group by with distinct but it will require a derived table, which can be done in plain SQL anyway.
List dtos = s.createSQLQuery(
"SELECT " +
" ERRORS.DMN_NAM AS dmnNam, " +
" CODES.MSG_TXT AS msgTxt " +
"FROM SERV_ERR ERRORS, EVENT_CD CODES " +
"WHERE ERRORS.SERV_RESP_CD_TXT = CODES.CD_TXT " +
"GROUP BY " +
" ERRORS.DMN_NAM, " +
" ERRORS.SERV_NAM, " +
" CODES.MSG_TXT, " +
" ERRORS.SERV_ERR_CNT, " +
" ERRORS.ERR_TS_NUM " +
"ORDER BY " +
" ERRORS.DMN_NAM, " +
" CODES.MSG_TXT "
.addScalar("dmnNam")
.addScalar("msgTxt")
.setResultTransformer( Transformers.aliasToBean(MyDTO.class))
.list();
Make sure YourDTO has a matching constructor, and the types are the exactly like ee.dmn.nam and ece msgTxt.
Instead of group by I'd choose:
SELECT dmnNam, msgTxt
FROM (
SELECT DISTINCT
ERRORS.DMN_NAM AS dmnNam,
ERRORS.SERV_NAM,
CODES.MSG_TXT AS msgTxt,
ERRORS.SERV_ERR_CNT,
ERRORS.ERR_TS_NUM
FROM SERV_ERR ERRORS, EVENT_CD CODES
WHERE ERRORS.SERV_RESP_CD_TXT = CODES.CD_TXT
ORDER BY
dmnNam,
msgTxt
) as DATA
I'm getting this error and I can't find any reference to it:
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "all", found '(' near line 1, column 221 [select new EffectivePermissions(r.id, r.name, r.defaultValue, rc.value AS companyValue, ru.value AS userValue) from permissionsPackage.Entity.Permissions r left join permissionsPackage.Entity.CompanyPermissions rc fetch (r.id=rc.permissionId AND rc.companyId=2313 ) left join permissionsPackage.Entity.UserPermissions ru fetch (r.id=ru.permissionId AND ru.userId=1)]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
Below is the Query I'm using.
this.hql = "select new EffectivePermissions(r.id, r.name, r.defaultValue, rc.value AS companyValue, ru.value AS userValue) "
+ "from "
+ Permissions.class.getName()
+ " r "
+ "left join "
+ CompanyPermissions.class.getName()
+ " rc "
+ "fetch (r.id=rc.permissionId AND rc.companyId="
+ user.getCompany().getId()
+ " ) "
+ "left join "
+ UserPermissions.class.getName()
+ " ru "
+ "fetch (r.id=ru.permissionId AND ru.userId="
+ user.getId()
+ ")";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
permissions = query.list();
I have a class called EffectivePermission with the properties in the constructor and I'd like to get a list based on it.
From the Hibernate Reference chapter 14.3. Associations and joins:
A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select ... See Section 19.1, “Fetching strategies” for more information.
You'll also see there that it must be used after the joinkeyword as in from e1 left join fetch e2. It can also be used as in from entity fetch all properties (this is why Hibernate is expecting the all keyword).
Regarding your specific query, and as far as I know, with HQL you can not specify the join conditions. Hibernate will automatically perform the join using the conditions configured in the mapping. This is why it is necessary to have relationships mapped in order to use a HQL join.
And most importantly, remember that joins in HQL are quite different. You don't join two entities, you join an entity with one of its collection-valued properties (associations). Notice you can always do cartesian-like joins (pay attention to performance and always look at the generated queries and execution plans).
As a side note, it can be considered bad practice to append parameter values directly into the query (due to potential injection vulnerabilities). You should be using named parameters instead.