String matching in HQL - string

I'm creating an HQL query to filter a grid of data:
string formatString = "Type = {0} AND {1} = '{2}' AND CompletedDate > '{3}'
AND CompletedDate < '{4}' AND UserName LIKE '{5}'";
HqlBindingSource.Where = string.Format(formatString, type, keyId, entityID,
fromDate, toDate, toDate, UserTextBox.Text);
The problem I'm having is the string matching on the UserName field. I'm used to working with SQL and I can't get it to match on a value using = or LIKE. Can anyone point me in the right direction for this?
Thanks

I had "toDate" listed twice, so UserTextBox.Text was never used in the query.

Related

How to get first String based on whitespace out of a full String using java8?

Let's Example I have String s = "Rahul Kumar" I need to have Rahul as a output using java8
Actual Requirement, I do have a list of Trip Object, I want to set driverName property as only first name to each Trip Object and return that list?
System.out.println( someStringValue.subSequence(0, someStringValue.indexOf(' ')));
I'm getting trouble to incorporate this code into the listOfTrip. If I'm doing like this,
List<CharSequence> list = listOfTrips.stream().map(e -> e.getDriverName().subSequence(0, someStringValue.indexOf(' '))).collect(Collectors.toList()); System.out.println(list);
Here, With this, The return type is wrong and it is not fetching only first name out of full name.
Below will give you the proper result:
List<CharSequence> list2 = listOfTrips.stream()
.map(m->m.getDriverName().substring(0,m.getDriverName().indexOf(' ')))
.collect(Collectors.toList());
Please try this also once:
String s = "Rahul Kumar";
Optional<String> beforeWhiteSpace = Pattern.compile(" ").splitAsStream(s).collect(Collectors.toList()).stream().findFirst();

Custom LIKE statement in Java Spring Data and Cassandra #Query

I need to query from a table using LIKE statement where parameters are optional.
This is custom query where I can filter employee table by which Non-Empty request param:
Select select = QueryBuilder.select().from("employee");
Where selectWhere = select.where();
if (!email.isEmpty()) {
selectWhere.and(QueryBuilder.like("email", "%" + email + "%"));
}
if (!firstName.isEmpty()) {
selectWhere.and(QueryBuilder.like("first_name", "%" + firstName + "%"));
}
//Note: User will provide either email or first_name or both.
However, the above custom query will need to manually map the Rows to the object which I find too tedious:
ResultSet rs = cassandraClient.getApplicationSession().execute( select );
return rs.all().stream().map(row -> row.getString("first_name")).collect( Collectors.toList() );
Is there a way where I can use a Query annotation like below, so it will return the entity directly?
#Query("SELECT * FROM employee WHERE email LIKE :email AND first_name LIKE :firstName")
Employee search(#Param(value="email" String email, #Param(value="firstName" String firstName)
I tried passing an empty parameter value, but I am getting the following error:
LIKE value can't be empty.

Putting string after a where clause in a soql query in salesforce

I have the filter logic parsed and put as a string in a variable called "where_clause". I have to use this where_clause in a query to fetch data.How can I use a string of this type after the where part of the query?I am working on salesforce with custom objects.
I suggest you check Dynamic SOQL.
In particular
String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');
If you have your Custom Object as MyCustomObject__c
String myTestString = 'TestName';
List<MyCustomObject__c> sobjList = (List<MyCustomObject__c>)Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');

JPQL didn't get parameter

There is my code. Can someone tell me what is wrong?
for(String motorProposalId : proposalMap.keySet()) {
MotorPolicy mp = policyMap.get(motorProposalId);
String query = "SELECT p From Payment Where p.referenceNo = :motorProposalId"+
" AND commenmanceDate >= :startDate AND commenmanceDate <= :endDate";
Query qu = em.createQuery(query);
qu.setParameter("commenmanceDate", mp.getCommenmanceDate());
qu.setParameter("motorProposalId", motorProposalId);
qu.setParameter("startDate", motorDailyCriteria.getStartDate());
qu.setParameter("endDate", motorDailyCriteria.getEndDate());
Query is syntactically incorrect because there is no named parameter commenmanceDate in query string. If commenmanceDate should be named parameter, then it must be prefixed with ':'.
Also then query likely have some semantic problems, because all values included to date comparisons are already known before executing query.

JPQL query --- how to use 'is null'

i use the following query in JPQL to query the people whose address column is empty.
List rl =
em.createQuery( "select o from Person as o where
o.address IS NULL" ).setFirstResult(
0).setMaxResults( 50).getResultList();
...
this line of code always return an empty list, obviously the table does has entries that match the condition.
class Person {
Address address;
String name;
... }
class Address {
String name;
... }
anyone knows what's wrong with this jpql statement?
thanks in advance.
As mentioned, address column is empty, then try using IS EMPTY expression instead of IS NULL.
em.createQuery( "SELECT o FROM Person o where (o.address.id IS NULL OR o.address.id = 0").setMaxResults(50).getResultList();
Check constraint according to id's datatype.
Also there is no need to mention setFirstResult(0) as it is not going to skip any results & without it, by default all matching results will be fetched.

Resources