Spring data jpa, pageable adds own specs to custom query - spring

i have a problem. I'm trying to do custom query with filtering by date usin time stamp, but im using Pageable and it adds to query after comma id desc limit and it causing error like below. I dont know how to get arroung this problem.
select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id LIMIT 999999, id desc limit ?
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 15:49:59.545 TRACE 896 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']
2021-03-05 15:49:59.556 WARN 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2021-03-05 15:49:59.556 ERROR 896 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : 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 'desc limit 300' at line 1
Custom query
#Query(nativeQuery = true, value = "select * from lights Where timestamp BETWEEN :startDate AND :endDate ORDER BY id LIMIT 999999")
Page<Lights> findBetweenDates(#Param("startDate") String startDate, #Param("endDate") String endDate, Pageable pageable);
Conrtoller class
#GetMapping("/lights")
public String viewHomePage(Model model, String startDate , String endDate) {
startDate = "'2021-03-04 15:27:07'";
endDate = "'2999-12-31 23:59:59'";
return findPaginated(1, "id", "desc",startDate,endDate, model);
}
#RequestMapping({"/lights/page/{pageNo}"})
public String findPaginated(#PathVariable(value = "pageNo") int pageNo,
#RequestParam("sortField" ) String sortField,
#RequestParam("sortDir") String sortDir,
#RequestParam(value = "startDate", defaultValue = "'2021-03-04 15:27:07'") String startDate,
#RequestParam(value = "endDate", defaultValue = "'2999-12-31 23:59:59'") String endDate,
Model model){
int pageSize = 200;
Page<Lights> page = lightsMapService.findPaginated(pageNo,pageSize,sortField,sortDir,startDate,endDate);
List<Lights> lightsList = page.getContent();
model.addAttribute("currentPage", pageNo );
model.addAttribute("totalPages", page.getTotalPages());
model.addAttribute("totalItems",page.getTotalElements() );
model.addAttribute("sortField", sortField );
model.addAttribute("sortDir", sortDir );
model.addAttribute("startDate",startDate);
model.addAttribute("endDate", endDate);
model.addAttribute("reverseSortDir", sortDir.equals("asc")?"desc":"asc");
model.addAttribute("lightsList", lightsList );
return "sensorsPages/lights";
}
After i delete LIMIT 99999 from query i'm not getting error enymore, but got this and its not working:
2021-03-05 16:09:47.789 DEBUG 3540 --- [nio-8080-exec-7] org.hibernate.SQL : select * from lights Where timestamp BETWEEN ? AND ? ORDER BY id, id desc limit ?
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - ['2021-03-04 15:27:07']
2021-03-05 16:09:47.789 TRACE 3540 --- [nio-8080-exec-7] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - ['2999-12-31 23:59:59']

Related

Laravel, auto serialization of "created_at" and "updated_at" bugged (included v9.0)

Here my config :
Client and server timezone is : "Europe/Paris"
App/config timezone is : "Europe/Paris"
mysql system_time_zone is : "Europe/Paris"
Here a simple table with auto created_at and another field "my_date" as timestamp like created_at :
insert into my_table (my_date) values ('2022-09-28 15:15:00')
-> i execute it at 15:15:34 precise
So, I found in phpmyadmin :
id my_date created_at
1 2022-09-28 15:15:00 2022-09-28 15:15:34
But when I serialize my object, laravel, give me that:
{ id: 1, my_date: '2022-09-28 15:15:00', created_at: '2022-09-28 13:15:34' }
13:15:34 at place of 15:15:34
I don't understand why the serialization of "created_at" is not the same than "my_date"
Why autogenerated "created_at" and "updated_at" doesn't serialise correctly ?

how to define models, validators, expiring URL, by using Following data?

id auto increment
Name : source name (unique) mk
Desc : small descripton to know about sourc
rooturl : domain Name that deployed this tinny url https://tm.py/
Active/Expired: Bool
Createdon
CreatedBy
Tinny URL tables
id, : auto
sourcecode : source table code mk
tinnycode : generated 6 digits code ( should be unique for source) A123h5
MainURL : actual url
tinnyURL: tinney https://tkm.py/A123h5
validity : no of days valid from created date
Createdon : auto
createdby : request by
RequestFrom : request header info ( Like IP,request url, user etc)
setTinnyURL( Source, actual URL, validity)
soruce :Mk
actual URL :
https://airbrake.io/blog/python/zerodivisionerror-2#:~:text=As%20you%20may%20suspect%2C%20the,or%20modulo)%20operation%20was%20zero.&text=We'll%20also%20see%20how,handling%20division%20by%20zero%20issues.
validity : 3
log ( "got tinney request for MK soruce", "IP",USr)
generate Tunney code 6 unique A123h5
{
status: 200
message": success
response:{
TinnyURL: "https://tkm.py/A123h5"
}
}
getTinnyURL(tinnyURL)
check tinnyurl in Tinny URL tables , value matched in tinnyURL field then retun actualURL

NativeSearchQuery returns all results even when passing a single identifier as part of query

List<String> ids = List.of(id);
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes(section).build())
.withIds(ids)
.build();
log.trace("query {}", query.toString());
SearchHits<Foo> searchHits = operations.search(query, Foo.class, index);
The above code snippet, generates the following query but its not clear why its fetching all Foos when I have clearly mentioned to use a single id using withIds(ids)
Sending request POST /foos/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=dfs_query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true with parameters:
Request body: {"from":0,"size":10000,"version":true,"_source":{"includes":["bp"],"excludes":[]}}
2021-01-15 10:12:45.075 TRACE 45572 --- [/O dispatcher 1] o.s.data.elasticsearch.client.WIRE : [6ba88ad6] Received raw response: 200 OK
2021-01-15 10:12:45.284 TRACE 45572 --- [nio-5555-exec-2] c.p.matrimony.service.BaseService : searchHitsConvertList input SearchHits{totalHits=235, totalHitsRelation=EQUAL_TO, maxScore=1.0, scrollId='null', searchHits={235 elements}, aggregations=null}
withIds() is used in another context (i.e. when retrieving multiple documents by ID via _mget).
In your case, you should simply proceed a bit differently, i.e. with a terms filter on the _id field:
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withSourceFilter(new FetchSourceFilterBuilder().withIncludes(section).build())
.withFilter(QueryBuilders.termsFilter("_id", ids))
.build();

Creating Contact in xero from java code gives Bad Request

Below are logs which i got the error while creating contact in xero as i want to create multiple contact in xero i writing the api but i got the below error if i send the same request in postman it is working properly.
<pre>com.xero.api.XeroApiException: Bad Request
com.xero.api.XeroApiExceptionHandler.execute(XeroApiExceptionHandler.java:27)
com.xero.api.client.AccountingApi.createContact(AccountingApi.java:912)
in.techchefs.anyspace.common.service.impl.XeroServiceImpl.addContactToXero(XeroServiceImpl.java:274)
Below are code for calling the Contact API
public Contact addContactToXero(User user, String currencyCode) throws IOException {
logger.info("inside xeroservise User addContactToXero---->>"+currencyCode);
ApiClient client = new ApiClient();
accountingApi = AccountingApi.getInstance(client);
Contact contact = new Contact();
contact.setIsCustomer(true);
contact.setIsSupplier(true);
String businessName = user.getFirstName() + " " + user.getLastName() + " " + user.getId();
if (user.getBusinessInfo() != null && user.getBusinessInfo().getName() != null) {
businessName = user.getBusinessInfo().getName();
}
contact.setName(businessName);
contact.setFirstName(user.getFirstName());
contact.setLastName(user.getLastName());
contact.setContactNumber(user.getPhone());
contact.setEmailAddress(user.getEmail());
List<Address> arrayOfAddress = new ArrayList<Address>();
Set<UserAddress> userAddresses = user.getAddress();
for (Iterator iterator = userAddresses.iterator(); iterator.hasNext();) {
UserAddress userAddress = (UserAddress) iterator.next();
Address address = new Address();
address.setAddressLine1(userAddress.getStreet());
address.setAddressLine2(userAddress.getSuburb());
address.setAddressType(userAddress.getAddressType() == 1 ? AddressTypeEnum.DELIVERY : AddressTypeEnum.POBOX);
address.setRegion(userAddress.getState());
address.setPostalCode(userAddress.getPostalCode());
address.setCountry(userAddress.getCountry() == null ? "" : userAddress.getCountry().getTitle());
arrayOfAddress.add(address);
}
contact.setAddresses(arrayOfAddress);
contact.setContactStatus(ContactStatusEnum.ACTIVE);
logger.info("addContactToXero by USer contact--->>"+contact);
String accessToken=getAccessToken(currencyCode);
logger.info("addContactToXero by USer accessToken--->>"+accessToken);
XeroTokenStorage token=tokenStorageService.getByCurrencode(currencyCode);
logger.info("addContactToXero by USer token.getTenantId()--->>"+token.getTenantId());
Contacts contatcsAdded = accountingApi.createContact(accessToken,token.getTenantId(),contact);
List<Contact> contatcsAddedList = contatcsAdded.getContacts();
return contatcsAddedList.get(0);
//return contatcsAdded;
}
I give the logger for contact
class Contact {
contactID: null
contactNumber: 6666
accountNumber: null
contactStatus: ACTIVE
name: Marc Verano 1
firstName: Marc
lastName: Verano
emailAddress: info#anyspaces.com
skypeUserName: null
contactPersons: []
bankAccountDetails: null
taxNumber: null
accountsReceivableTaxType: null
accountsPayableTaxType: null
addresses: []
phones: []
isSupplier: true
isCustomer: true
defaultCurrency: null
xeroNetworkKey: null
salesDefaultAccountCode: null
purchasesDefaultAccountCode: null
salesTrackingCategories: []
purchasesTrackingCategories: []
trackingCategoryName: null
trackingCategoryOption: null
paymentTerms: null
updatedDateUTC: null
contactGroups: []
website: null
brandingTheme: null
batchPayments: null
discount: null
balances: null
attachments: []
hasAttachments: null
validationErrors: []
hasValidationErrors: null
}
i got the error from java is bad request and same request if i send through postman it is working. Pls tell me some solution on that
There is a clue in your question - "if I send the same request through postman it is working" - contact name and number need to be unique, so check that you are providing unique values for these fields.
However as droopsnoot mentioned in a comment, any validation errors will be returned from the Xero API, so interrogate the response and you will probably find the reason it is occurring.

Spring data mongodb: Optional #Query parameter no longer works

After upgrading to spring data mongodb 1.10.1, I am getting errors when running queries like:
#Query("{$and :["
+ "{ $or : [ { $where: '?0 == null' } , { 'field1' : ?0 } ] },"
+ "{ $or : [ { $where: '?1 == null' } , { 'field2' : ?1 } ] },"
+ "]}")
public Page<Entity> findAll(String param1, String param2)
Checking the error I see the parameter inside the where clause is not quoted and as I result I get:
org.springframework.data.mongodb.UncategorizedMongoDbException: Query
failed with error code 139 and error message 'ReferenceError:
test_param_value is not defined :
I have seen a few answers here recommending this way of handling optional parameters ((spring-data-mongo - optional query parameters?)) but it no longer works and I cannot seem to find anything in the release change log.
In case anyone else is interested, I managed to find a workaround after checking a similar ticket int the Spring Data project.
It seems the way I was checking for null parameters in the query is not a good practice. This is from a Spring developer comment: "placeholders are not designed to compose keys/values but bind parameters. Beyond that, placeholder use in quoted strings is always problematic in terms of escaping. Using SpEL should suit your needs"
So I ended up using SpEL to do the checks on parameters and it works fine. This is how it looks:
#Query("{$and :["
+ "?#{ [0] == null ? { $where : 'true'} : { 'field1' : [0] } },"
+ "?#{ [1] == null ? { $where : 'true'} : { 'field2' : [1] } },"
+ "]}")
public Page<Entity> findAll(String param1, String param2, Pageable pageable);

Resources