Spring Data with interface projection with Subquery - oracle

i'm working on a project with spring data jpa and Oracle data base.
i have a problem with spring data jpa in interface projection.
when i try the query in HQL-Console it returns the correct result,but when i use it in a request,it returns this line:
[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap#2f163567]
//Repository
#Query(value = "select m.codUseridPstme as codUseridPstme from OCMCharts o" +
" join o.ocmUnitsByChartsId u join u.ocmPostsByUnitsId p " +
"join p.ocmPostmemsByPostsId m where lower(trim(o.codCodeChrt)) =lower(trim(:chartCode))" +
" and p.flgIsmanagerPost=true " +
"and p.staPoststatePost='ACTIVE' and m.staMemberstatusPstme='ACTIVE' " +
"and u.codUnitcodeUnts in (select u.codUnitcodeUnts from OCMCharts o " +
"join o.ocmUnitsByChartsId u "+
"join u.ocmPostsByUnitsId p " +
"join p.ocmPostmemsByPostsId m " +
"where lower(trim(o.codCodeChrt)) =lower(trim(:chartCode)) and u.staStatusUnts='ACTIVE' and m.codUseridPstme=:userName)")
List<OCMUserNameDTO> getUnitManagerListByChartCodeAndUnitCodesIn(#Param("chartCode") String chartCode, #Param("userName") String userName);
//DTO
public interface OCMUserNameDTO {
String getCodUseridPstme();
}
//Console Query
select
ocmpostmem3_.COD_USERID_PSTME as col_0_0_
from
OCM.OCM_CHARTS ocmcharts0_
inner join
OCM.OCM_UNITS ocmunitsby1_
on ocmcharts0_.CHARTS_ID=ocmunitsby1_.CHRT_CHARTS_ID
inner join
OCM.OCM_POSTS ocmpostsby2_
on ocmunitsby1_.UNITS_ID=ocmpostsby2_.UNTS_UNITS_ID
inner join
OCM.OCM_POSTMEM ocmpostmem3_
on ocmpostsby2_.POSTS_ID=ocmpostmem3_.POST_POSTS_ID
where
lower(trim(ocmcharts0_.COD_CODE_CHRT))=lower(trim(?))
and ocmpostsby2_.FLG_ISMANAGER_POST=1
and ocmpostsby2_.STA_POSTSTATE_POST='ACTIVE'
and ocmpostmem3_.STA_MEMBERSTATUS_PSTME='ACTIVE'
and (
ocmunitsby1_.COD_UNITCODE_UNTS in (
select
ocmunitsby5_.COD_UNITCODE_UNTS
from
OCM.OCM_CHARTS ocmcharts4_
inner join
OCM.OCM_UNITS ocmunitsby5_
on ocmcharts4_.CHARTS_ID=ocmunitsby5_.CHRT_CHARTS_ID
inner join
OCM.OCM_POSTS ocmpostsby6_
on ocmunitsby5_.UNITS_ID=ocmpostsby6_.UNTS_UNITS_ID
inner join
OCM.OCM_POSTMEM ocmpostmem7_
on ocmpostsby6_.POSTS_ID=ocmpostmem7_.POST_POSTS_ID
where
lower(trim(ocmcharts4_.COD_CODE_CHRT))=lower(trim(?))
and ocmunitsby5_.STA_STATUS_UNTS='ACTIVE'
and ocmpostmem7_.COD_USERID_PSTME=?
)
)

thanks for answers.
i wrote a .toString() method and was wrong.it was made a conflict with return type of interface projection.
List<OCMUserNameDTO> users = ocmPostmemService.getManagerUsernameOfPersonByChartCodeAndUserName(chartCode, userName);
return new ResponseEntity<>(users.toString(), HttpStatus.OK);
//DTO
public interface OCMUserNameDTO {
String getCodUseridPstme();
}
thanks!

Related

repeated data in json response spring boot

I have this SQL query
#Query( value = "select tc_users.id as userid,tc_devices.id as deviceid, tc_devices.name, tc_devices.lastupdate as hora_fecha,\n" +
"tc_positions.speed as velocidad, tc_positions.latitude, tc_positions.longitude, tc_positions.devicetime\n" +
"from tc_devices\n" +
"join tc_users on tc_users.id = tc_devices.userid \n" +
"join tc_positions on tc_devices.id= tc_positions.deviceid and \n" +
"tc_positions.devicetime=(select max(devicetime) from tc_positions where tc_positions.deviceid= tc_devices.id) and tc_users.id= ?1 ;", nativeQuery = true)
List<resumen_entity> resu(Integer userid);
This SQL query return this data
result of query in SQL workbench
but when using it with my spring project it returns this json
result of query by springboot
a really need help with this problem
I try with another type of SQL query but spring returns the same result
#Query( value = "select distinct tc_users.id as userid,tc_devices.id as deviceid, " +
"tc_devices.name, tc_devices.lastupdate as hora_fecha, " +
"tc_positions.speed as velocidad, tc_positions.latitude, " +
"tc_positions.longitude, tc_positions.devicetime " +
"from tc_users, tc_devices,tc_positions where tc_users.id =tc_devices.userid " +
"and tc_devices.id= tc_positions.deviceid " +
"and tc_positions.devicetime=(select max(devicetime) " +
"from tc_positions where tc_positions.deviceid = tc_devices.id) and tc_users.id= ?1 ",
nativeQuery = true)
List<resumen_entity> resu(Integer userid);

I use #Query annotation in Jpql, I modified my query with additional "distinct", How to Fix it?

In my Repositories, I modified my query with additional "distinct" and it can't work
#Query(value = "select distinct i from Item i "
+"where i.store = ?1 and i.itemVariant IN ?2 "
+"and i.status = ?3 " )
Page<Item> findByStoreAndItemVariantInAndStatus
(Store store, List<ItemVariant> itemVariant ,byte status, Pageable pageable);
if I remove the additional modifying #Query, this works but the result is duplicated
I have fixed it. I change it using native query and changed distinct to group by, and it's works
#Query(value = "SELECT i.* "
+" FROM public.mst_item as i "
+" join public.mst_store as ist "
+" on i.store_id = ist.id "
+" join public.mst_item_classifiers as iv "
+" on iv.item_id = i.id "
+" where ist.id = :store and iv.name IN "
+" (select ic.name from public.mst_item_classifiers as ic "
+" where ic.name like concat('%',:itemVariant,'%') and ic.status = :status ) "
+" and i.status = :status "
+" group by ?#{#pageable}, i.id",
nativeQuery = true)
Page<Item> findByStoreAndItemVariantInAndStatus(#Param("store")Store store,#Param("itemVariant") String itemVariant , #Param("status") byte status, Pageable pageable);

CRUDRepository native query with parameter

I am using a native query in spring data JpaRepository like below :
#Query(value = "SELECT SUBSTRING_INDEX(u.email, '#', -1) as domain, COUNT(*) as domainCount r.invite_organization_id"
+ " FROM users u,_registrations r where u.user_id=r.user_id and r.invite_organization_id=?1"
+ " GROUP BY "
+ "SUBSTRING_INDEX(u.email, '#', -1) ORDER BY domainCount DESC", nativeQuery = true)
List<Object[]> countTopDomain(String orgId);
The above gives me the following exception :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'r.invite_organization_id FROM users u,registrations r where u.user' at line 1
How will I pass the value for invite_organization_id(in the query ) from the method countTopDomain() argument.
'r.invite_organization_id FROM srs_users u,srs_user_registrations r', have syntax error. ',' is missing after count (*) as domainCount
Try this one
#Query(value = "SELECT SUBSTRING_INDEX(u.email, '#', -1) as domain, COUNT(*) as domainCount, r.invite_organization_id"
+ " FROM srs_users u,srs_user_registrations r where u.user_id=r.user_id and r.invite_organization_id=?1"
+ " GROUP BY "
+ "SUBSTRING_INDEX(u.email, '#', -1) ORDER BY domainCount DESC", nativeQuery = true)
List<Object[]> countTopDomain(String orgId);

#Query not recognizing parameters if they are inside single quote in Spring Framework

I have a big problem with Spring Data in #Query.
I have the following query :
SELECT created_at::DATE "date",
count(*)
FROM
absence
WHERE
created_at::DATE between '2018-05-27' AND '2018-05-31'
GROUP BY created_at::DATE;
this query works in postgres without any problem now in spring to use it :
#Query("SELECT created_at\\:\\:DATE \"date\",count(*) FROM absence " +
"WHERE created_at\\:\\:DATE between '?1' AND '?2' " +
"GROUP created_at\\:\\:DATE", nativeQuery = true)
fun getAbsenceCount(beginDate: String,endDate: String): List<AbsenceCount>
This query doesn't work at all, the problem is that spring can't recognize the parather beginDate (?1) & endDate (?2).
I tried a lot of solution from stackoverflow like solution and solution 2 but I can't get rid of this problem.
I don't know if it's intented or it's a bug in spring.
Try to simply use the classic SQL cast function:
#Query(value = "" +
"select " +
" cast(a.created_at as date) as createdAt, " +
" count(*) as quantity " +
"from " +
" absence a " +
"where " +
" cast(a.created_at as date) between ?1 and ?2 " +
"group " +
" cast(a.created_at as date)" +
"", nativeQuery = true)
fun getAbsenceCount(beginDate: String, endDate: String): List<AbsenceCount>
where AbsenceCount is a projection like this (java):
public interface AbsenceCount {
LocalDate getCreatedAt();
Long getQuantity();
}

Android Room 2.1.0-alpha03 #DatabaseView Missing syntax check and view name

I'm using roomVersion = '2.1.0-alpha03' on the AndroidX branch
// Room (androidx)
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-rxjava2:$rootProject.roomVersion"
implementation "androidx.room:room-guava:$rootProject.roomVersion"
testImplementation "androidx.room:room-testing:$rootProject.roomVersion"
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
Here is my #DatabaseView query:
#DatabaseView("SELECT * FROM DataPointJoin " +
"INNER JOIN Device ON Device.device_uid = DataPointJoin.device_uid " +
"INNER JOIN Measurement ON Measurement.measurement_uid = DataPointJoin.measurement_uid " +
"INNER JOIN Location ON Location.location_uid = DataPointJoin.location_uid " +
"INNER JOIN User ON User.user_uid = DataPointJoin.user_uid " +
"INNER JOIN SyncRecord ON SyncRecord.sync_uid = DataPointJoin.uid ")
And here is an example of how I'm using it:
#Query("SELECT * FROM DataPoint WHERE DataPoint.uid = :uid")
Flowable<Optional<DataPoint>> get(String uid);
When using the #DatabaseView() to encapsulate a join into a View, and after clean and rebuild:
My #DatabaseView query isn't checked or highlighted like in #Query(), just a plain long string.
Compiles, but IDE can't resolve the View symbol.

Resources