return a boolean - jdbcTemplate - boolean

I would like to return a boolean value using in this method:
public Boolean isSizeOk(String transactionId){
String sqlQuery = "SELECT true FROM customer_pool WHERE id = "+ transactionID + " AND level = 13)";
//The next line is the problem.
//If I am returning a Boolean List, I can write
List <Boolean> sizeResult = jdbcTemplate.queryForList(sqlQuery, Boolean.class, transactionId);
//But since I only want a boolean value, what will the statement be?
Boolean sizeResult = jdbcTemplate......?
return sizeResult;
}
Kindly help. Thanks.

If you want to write a method that checks that a record exists in the database you can use the following code:
Integer cnt = jdbcTemplate.queryForObject(
"SELECT count(*) FROM customer_pool WHERE id = ? AND level = 13)", Integer.class, id);
return cnt != null && cnt > 0

Counting rows in SQL just in order to get simple information about non-emptiness of result may be unnecessary overkill, you want just ask result set for first row and finish. For simple queries by primary key or other index the performance might be similar, however, for complex queries, or full table scan queries it might be slow. In Spring I prefer simple utility method
public boolean exists(String sql, Object... args) {
boolean result = query(sql, args, new ResultSetExtractor<Boolean>() {
#Override
public Boolean extractData(ResultSet rs) throws SQLException,DataAccessException {
boolean result = rs.next();
return result;
}
});
return result;
}
(Google "sql exists vs count" for more info.)

What about
// Change query accordingly
String query = "SELECT 1 FROM " + tableName + " WHERE " + idColumnName + " = ? LIMIT 1";
try {
jdbcTemplate.queryForObject(query, new Object[]{id}, Long.class);
return true;
} catch (EmptyResultDataAccessException e) {
return false;
}

Case 1: In case you are returning boolean:
Just check the size of sizeResult List, if the size is greater than 0 return true else return false.
Case 2: If you are returning boolean list then return type must be a boolean List.You must
write the method as:
public List<Boolean> isSizeOk(String transactionId, int sizeLimit){
String sqlQuery = "SELECT true FROM customer_pool WHERE id = ? AND level = 13)";
List <Boolean> sizeResult = jdbcTemplate.queryForList(sqlQuery, Boolean.class, transactionId);
Boolean sizeResult = jdbcTemplate......?
return sizeResult;
}

Related

Add specification from a loop

In a spring boot 3 application I try to use specification
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
...
return pre;
};
return findAll(specification, page);
}
I need for every value in splittedValues to add a global specification
for (String splittedName: splittedValues) {
specification.or(splittedName);
}
and pass it to findAll
I don't understand how to do it
Edit
you solution seem to work but
that generate
where
1=1
or like e1_0.name "%bob%"
or like e1_0.name "%jame%"
It's there a way to get
where
1=1
and (
like e1_0.name "%bob%"
or like e1_0.name "%jame%"
)
Using or operator , you combine multiple conditions, checking whether the name of User contains one of the splittedValues. Predicate is then returned from specification.
I hope that was what you wanted, in case I didn't understand, please correct me. Here is an example if so, adapt to your own code.
Edit: To get 1=1 and (like e1_0.name "%bob%" or like e1_0.name "%jame%"), you can use cb.or method to combine the conditions, resulting nameConditions is then combined with pre using cb.and.
public Page<User> advancedSearch(UserSearch search, Pageable page) {
String[] splittedValues = search.name.split(" ");
Specification<User> hasPersonWithName = (Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
Predicate pre = cb.conjunction();
Predicate nameConditions = null;
for (String splittedName: splittedValues) {
if (nameConditions == null) {
nameConditions = cb.like(root.get("name"), "%" + splittedName + "%");
} else {
nameConditions = cb.or(nameConditions, cb.like(root.get("name"), "%" + splittedName + "%"));
}
}
if (nameConditions != null) {
pre = cb.and(pre, nameConditions);
}
return pre;
};
return findAll(hasPersonWithName, page);
}

how to transform string in integer in jaspersoft studio with the condition [duplicate]

How can I convert a String to an int?
"1234" → 1234
String myString = "1234";
int foo = Integer.parseInt(myString);
If you look at the Java documentation you'll notice the "catch" is that this function can throw a NumberFormatException, which you can handle:
int foo;
try {
foo = Integer.parseInt(myString);
}
catch (NumberFormatException e) {
foo = 0;
}
(This treatment defaults a malformed number to 0, but you can do something else if you like.)
Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int:
import com.google.common.primitives.Ints;
int foo = Optional.ofNullable(myString)
.map(Ints::tryParse)
.orElse(0)
For example, here are two ways:
Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
There is a slight difference between these methods:
valueOf returns a new or cached instance of java.lang.Integer
parseInt returns primitive int.
The same is for all cases: Short.valueOf/parseShort, Long.valueOf/parseLong, etc.
Well, a very important point to consider is that the Integer parser throws NumberFormatException as stated in Javadoc.
int foo;
String StringThatCouldBeANumberOrNot = "26263Hello"; //will throw exception
String StringThatCouldBeANumberOrNot2 = "26263"; //will not throw exception
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot);
} catch (NumberFormatException e) {
//Will Throw exception!
//do something! anything to handle the exception.
}
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot2);
} catch (NumberFormatException e) {
//No problem this time, but still it is good practice to care about exceptions.
//Never trust user input :)
//Do something! Anything to handle the exception.
}
It is important to handle this exception when trying to get integer values from split arguments or dynamically parsing something.
Do it manually:
public static int strToInt(String str){
int i = 0;
int num = 0;
boolean isNeg = false;
// Check for negative sign; if it's there, set the isNeg flag
if (str.charAt(0) == '-') {
isNeg = true;
i = 1;
}
// Process each character of the string;
while( i < str.length()) {
num *= 10;
num += str.charAt(i++) - '0'; // Minus the ASCII code of '0' to get the value of the charAt(i++).
}
if (isNeg)
num = -num;
return num;
}
An alternate solution is to use Apache Commons' NumberUtils:
int num = NumberUtils.toInt("1234");
The Apache utility is nice because if the string is an invalid number format then 0 is always returned. Hence saving you the try catch block.
Apache NumberUtils API Version 3.4
Integer.decode
You can also use public static Integer decode(String nm) throws NumberFormatException.
It also works for base 8 and 16:
// base 10
Integer.parseInt("12"); // 12 - int
Integer.valueOf("12"); // 12 - Integer
Integer.decode("12"); // 12 - Integer
// base 8
// 10 (0,1,...,7,10,11,12)
Integer.parseInt("12", 8); // 10 - int
Integer.valueOf("12", 8); // 10 - Integer
Integer.decode("012"); // 10 - Integer
// base 16
// 18 (0,1,...,F,10,11,12)
Integer.parseInt("12",16); // 18 - int
Integer.valueOf("12",16); // 18 - Integer
Integer.decode("#12"); // 18 - Integer
Integer.decode("0x12"); // 18 - Integer
Integer.decode("0X12"); // 18 - Integer
// base 2
Integer.parseInt("11",2); // 3 - int
Integer.valueOf("11",2); // 3 - Integer
If you want to get int instead of Integer you can use:
Unboxing:
int val = Integer.decode("12");
intValue():
Integer.decode("12").intValue();
Currently I'm doing an assignment for college, where I can't use certain expressions, such as the ones above, and by looking at the ASCII table, I managed to do it. It's a far more complex code, but it could help others that are restricted like I was.
The first thing to do is to receive the input, in this case, a string of digits; I'll call it String number, and in this case, I'll exemplify it using the number 12, therefore String number = "12";
Another limitation was the fact that I couldn't use repetitive cycles, therefore, a for cycle (which would have been perfect) can't be used either. This limits us a bit, but then again, that's the goal. Since I only needed two digits (taking the last two digits), a simple charAtsolved it:
// Obtaining the integer values of the char 1 and 2 in ASCII
int semilastdigitASCII = number.charAt(number.length() - 2);
int lastdigitASCII = number.charAt(number.length() - 1);
Having the codes, we just need to look up at the table, and make the necessary adjustments:
double semilastdigit = semilastdigitASCII - 48; // A quick look, and -48 is the key
double lastdigit = lastdigitASCII - 48;
Now, why double? Well, because of a really "weird" step. Currently we have two doubles, 1 and 2, but we need to turn it into 12, there isn't any mathematic operation that we can do.
We're dividing the latter (lastdigit) by 10 in the fashion 2/10 = 0.2 (hence why double) like this:
lastdigit = lastdigit / 10;
This is merely playing with numbers. We were turning the last digit into a decimal. But now, look at what happens:
double jointdigits = semilastdigit + lastdigit; // 1.0 + 0.2 = 1.2
Without getting too into the math, we're simply isolating units the digits of a number. You see, since we only consider 0-9, dividing by a multiple of 10 is like creating a "box" where you store it (think back at when your first grade teacher explained you what a unit and a hundred were). So:
int finalnumber = (int) (jointdigits*10); // Be sure to use parentheses "()"
And there you go. You turned a String of digits (in this case, two digits), into an integer composed of those two digits, considering the following limitations:
No repetitive cycles
No "Magic" Expressions such as parseInt
Methods to do that:
Integer.parseInt(s)
Integer.parseInt(s, radix)
Integer.parseInt(s, beginIndex, endIndex, radix)
Integer.parseUnsignedInt(s)
Integer.parseUnsignedInt(s, radix)
Integer.parseUnsignedInt(s, beginIndex, endIndex, radix)
Integer.valueOf(s)
Integer.valueOf(s, radix)
Integer.decode(s)
NumberUtils.toInt(s)
NumberUtils.toInt(s, defaultValue)
Integer.valueOf produces an Integer object and all other methods a primitive int.
The last two methods are from commons-lang3 and a big article about converting here.
Whenever there is the slightest possibility that the given String does not contain an Integer, you have to handle this special case. Sadly, the standard Java methods Integer::parseInt and Integer::valueOf throw a NumberFormatException to signal this special case. Thus, you have to use exceptions for flow control, which is generally considered bad coding style.
In my opinion, this special case should be handled by returning an empty Optional<Integer>. Since Java does not offer such a method, I use the following wrapper:
private Optional<Integer> tryParseInteger(String string) {
try {
return Optional.of(Integer.valueOf(string));
} catch (NumberFormatException e) {
return Optional.empty();
}
}
Example usage:
// prints "12"
System.out.println(tryParseInteger("12").map(i -> i.toString()).orElse("invalid"));
// prints "-1"
System.out.println(tryParseInteger("-1").map(i -> i.toString()).orElse("invalid"));
// prints "invalid"
System.out.println(tryParseInteger("ab").map(i -> i.toString()).orElse("invalid"));
While this is still using exceptions for flow control internally, the usage code becomes very clean. Also, you can clearly distinguish the case where -1 is parsed as a valid value and the case where an invalid String could not be parsed.
Use Integer.parseInt(yourString).
Remember the following things:
Integer.parseInt("1"); // ok
Integer.parseInt("-1"); // ok
Integer.parseInt("+1"); // ok
Integer.parseInt(" 1"); // Exception (blank space)
Integer.parseInt("2147483648"); // Exception (Integer is limited to a maximum value of 2,147,483,647)
Integer.parseInt("1.1"); // Exception (. or , or whatever is not allowed)
Integer.parseInt(""); // Exception (not 0 or something)
There is only one type of exception: NumberFormatException
Converting a string to an int is more complicated than just converting a number. You have think about the following issues:
Does the string only contain numbers 0-9?
What's up with -/+ before or after the string? Is that possible (referring to accounting numbers)?
What's up with MAX_-/MIN_INFINITY? What will happen if the string is 99999999999999999999? Can the machine treat this string as an int?
We can use the parseInt(String str) method of the Integer wrapper class for converting a String value to an integer value.
For example:
String strValue = "12345";
Integer intValue = Integer.parseInt(strVal);
The Integer class also provides the valueOf(String str) method:
String strValue = "12345";
Integer intValue = Integer.valueOf(strValue);
We can also use toInt(String strValue) of NumberUtils Utility Class for the conversion:
String strValue = "12345";
Integer intValue = NumberUtils.toInt(strValue);
I'm have a solution, but I do not know how effective it is. But it works well, and I think you could improve it. On the other hand, I did a couple of tests with JUnit which step correctly. I attached the function and testing:
static public Integer str2Int(String str) {
Integer result = null;
if (null == str || 0 == str.length()) {
return null;
}
try {
result = Integer.parseInt(str);
}
catch (NumberFormatException e) {
String negativeMode = "";
if(str.indexOf('-') != -1)
negativeMode = "-";
str = str.replaceAll("-", "" );
if (str.indexOf('.') != -1) {
str = str.substring(0, str.indexOf('.'));
if (str.length() == 0) {
return (Integer)0;
}
}
String strNum = str.replaceAll("[^\\d]", "" );
if (0 == strNum.length()) {
return null;
}
result = Integer.parseInt(negativeMode + strNum);
}
return result;
}
Testing with JUnit:
#Test
public void testStr2Int() {
assertEquals("is numeric", (Integer)(-5), Helper.str2Int("-5"));
assertEquals("is numeric", (Integer)50, Helper.str2Int("50.00"));
assertEquals("is numeric", (Integer)20, Helper.str2Int("$ 20.90"));
assertEquals("is numeric", (Integer)5, Helper.str2Int(" 5.321"));
assertEquals("is numeric", (Integer)1000, Helper.str2Int("1,000.50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int("0.50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int(".50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int("-.10"));
assertEquals("is numeric", (Integer)Integer.MAX_VALUE, Helper.str2Int(""+Integer.MAX_VALUE));
assertEquals("is numeric", (Integer)Integer.MIN_VALUE, Helper.str2Int(""+Integer.MIN_VALUE));
assertEquals("Not
is numeric", null, Helper.str2Int("czv.,xcvsa"));
/**
* Dynamic test
*/
for(Integer num = 0; num < 1000; num++) {
for(int spaces = 1; spaces < 6; spaces++) {
String numStr = String.format("%0"+spaces+"d", num);
Integer numNeg = num * -1;
assertEquals(numStr + ": is numeric", num, Helper.str2Int(numStr));
assertEquals(numNeg + ": is numeric", numNeg, Helper.str2Int("- " + numStr));
}
}
}
You can also begin by removing all non-numerical characters and then parsing the integer:
String mystr = mystr.replaceAll("[^\\d]", "");
int number = Integer.parseInt(mystr);
But be warned that this only works for non-negative numbers.
Google Guava has tryParse(String), which returns null if the string couldn't be parsed, for example:
Integer fooInt = Ints.tryParse(fooString);
if (fooInt != null) {
...
}
Apart from the previous answers, I would like to add several functions. These are results while you use them:
public static void main(String[] args) {
System.out.println(parseIntOrDefault("123", 0)); // 123
System.out.println(parseIntOrDefault("aaa", 0)); // 0
System.out.println(parseIntOrDefault("aaa456", 3, 0)); // 456
System.out.println(parseIntOrDefault("aaa789bbb", 3, 6, 0)); // 789
}
Implementation:
public static int parseIntOrDefault(String value, int defaultValue) {
int result = defaultValue;
try {
result = Integer.parseInt(value);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int endIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex, endIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
As mentioned, Apache Commons' NumberUtils can do it. It returns 0 if it cannot convert a string to an int.
You can also define your own default value:
NumberUtils.toInt(String str, int defaultValue)
Example:
NumberUtils.toInt("3244", 1) = 3244
NumberUtils.toInt("", 1) = 1
NumberUtils.toInt(null, 5) = 5
NumberUtils.toInt("Hi", 6) = 6
NumberUtils.toInt(" 32 ", 1) = 1 // Space in numbers are not allowed
NumberUtils.toInt(StringUtils.trimToEmpty(" 32 ", 1)) = 32;
You can use new Scanner("1244").nextInt(). Or ask if even an int exists: new Scanner("1244").hasNextInt()
You can use this code also, with some precautions.
Option #1: Handle the exception explicitly, for example, showing a message dialog and then stop the execution of the current workflow. For example:
try
{
String stringValue = "1234";
// From String to Integer
int integerValue = Integer.valueOf(stringValue);
// Or
int integerValue = Integer.ParseInt(stringValue);
// Now from integer to back into string
stringValue = String.valueOf(integerValue);
}
catch (NumberFormatException ex) {
//JOptionPane.showMessageDialog(frame, "Invalid input string!");
System.out.println("Invalid input string!");
return;
}
Option #2: Reset the affected variable if the execution flow can continue in case of an exception. For example, with some modifications in the catch block
catch (NumberFormatException ex) {
integerValue = 0;
}
Using a string constant for comparison or any sort of computing is always a good idea, because a constant never returns a null value.
In programming competitions, where you're assured that number will always be a valid integer, then you can write your own method to parse input. This will skip all validation related code (since you don't need any of that) and will be a bit more efficient.
For valid positive integer:
private static int parseInt(String str) {
int i, n = 0;
for (i = 0; i < str.length(); i++) {
n *= 10;
n += str.charAt(i) - 48;
}
return n;
}
For both positive and negative integers:
private static int parseInt(String str) {
int i=0, n=0, sign=1;
if (str.charAt(0) == '-') {
i = 1;
sign = -1;
}
for(; i<str.length(); i++) {
n* = 10;
n += str.charAt(i) - 48;
}
return sign*n;
}
If you are expecting a whitespace before or after these numbers,
then make sure to do a str = str.trim() before processing further.
For a normal string you can use:
int number = Integer.parseInt("1234");
For a String builder and String buffer you can use:
Integer.parseInt(myBuilderOrBuffer.toString());
Simply you can try this:
Use Integer.parseInt(your_string); to convert a String to int
Use Double.parseDouble(your_string); to convert a String to double
Example
String str = "8955";
int q = Integer.parseInt(str);
System.out.println("Output>>> " + q); // Output: 8955
String str = "89.55";
double q = Double.parseDouble(str);
System.out.println("Output>>> " + q); // Output: 89.55
int foo = Integer.parseInt("1234");
Make sure there is no non-numeric data in the string.
Here we go
String str = "1234";
int number = Integer.parseInt(str);
print number; // 1234
I am a little bit surprised that nobody mentioned the Integer constructor that takes String as a parameter.
So, here it is:
String myString = "1234";
int i1 = new Integer(myString);
Java 8 - Integer(String).
Of course, the constructor will return type Integer, and an unboxing operation converts the value to int.
Note 1: It's important to mention: This constructor calls the parseInt method.
public Integer(String var1) throws NumberFormatException {
this.value = parseInt(var1, 10);
}
Note 2: It's deprecated: #Deprecated(since="9") - JavaDoc.
Use Integer.parseInt() and put it inside a try...catch block to handle any errors just in case a non-numeric character is entered, for example,
private void ConvertToInt(){
String string = txtString.getText();
try{
int integerValue=Integer.parseInt(string);
System.out.println(integerValue);
}
catch(Exception e){
JOptionPane.showMessageDialog(
"Error converting string to integer\n" + e.toString,
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
It can be done in seven ways:
import com.google.common.primitives.Ints;
import org.apache.commons.lang.math.NumberUtils;
String number = "999";
Ints.tryParse:
int result = Ints.tryParse(number);
NumberUtils.createInteger:
Integer result = NumberUtils.createInteger(number);
NumberUtils.toInt:
int result = NumberUtils.toInt(number);
Integer.valueOf:
Integer result = Integer.valueOf(number);
Integer.parseInt:
int result = Integer.parseInt(number);
Integer.decode:
int result = Integer.decode(number);
Integer.parseUnsignedInt:
int result = Integer.parseUnsignedInt(number);
This is a complete program with all conditions positive and negative without using a library
import java.util.Scanner;
public class StringToInt {
public static void main(String args[]) {
String inputString;
Scanner s = new Scanner(System.in);
inputString = s.nextLine();
if (!inputString.matches("([+-]?([0-9]*[.])?[0-9]+)")) {
System.out.println("Not a Number");
}
else {
Double result2 = getNumber(inputString);
System.out.println("result = " + result2);
}
}
public static Double getNumber(String number) {
Double result = 0.0;
Double beforeDecimal = 0.0;
Double afterDecimal = 0.0;
Double afterDecimalCount = 0.0;
int signBit = 1;
boolean flag = false;
int count = number.length();
if (number.charAt(0) == '-') {
signBit = -1;
flag = true;
}
else if (number.charAt(0) == '+') {
flag = true;
}
for (int i = 0; i < count; i++) {
if (flag && i == 0) {
continue;
}
if (afterDecimalCount == 0.0) {
if (number.charAt(i) - '.' == 0) {
afterDecimalCount++;
}
else {
beforeDecimal = beforeDecimal * 10 + (number.charAt(i) - '0');
}
}
else {
afterDecimal = afterDecimal * 10 + number.charAt(i) - ('0');
afterDecimalCount = afterDecimalCount * 10;
}
}
if (afterDecimalCount != 0.0) {
afterDecimal = afterDecimal / afterDecimalCount;
result = beforeDecimal + afterDecimal;
}
else {
result = beforeDecimal;
}
return result * signBit;
}
}
One method is parseInt(String). It returns a primitive int:
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
The second method is valueOf(String), and it returns a new Integer() object:
String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);
public static int parseInt(String s)throws NumberFormatException
You can use Integer.parseInt() to convert a String to an int.
Convert a String, "20", to a primitive int:
String n = "20";
int r = Integer.parseInt(n); // Returns a primitive int
System.out.println(r);
Output-20
If the string does not contain a parsable integer, it will throw NumberFormatException:
String n = "20I"; // Throws NumberFormatException
int r = Integer.parseInt(n);
System.out.println(r);
public static Integer valueOf(String s)throws NumberFormatException
You can use Integer.valueOf(). In this it will return an Integer object.
String n = "20";
Integer r = Integer.valueOf(n); // Returns a new Integer() object.
System.out.println(r);
Output-20
References
https://docs.oracle.com/en/

How to write Criteria Query Builder for select all from List?

I need a query which will return only results which are having all values from the list.
Something like:
SELECT *
FROM ads_tags
WHERE tag_value("a", "b", "c");
I know that query is not good, but the point is that I want to filter ads but only ads which are having all tags from the list.
With my code, I am getting all ads that have at least one tag from the list. That is because I am using IN interface.
#Override
public List<AdsDTO> findAll(AdsSubGroup adssubgroup, Long userId, String status, String adsType,
String businessType, Long adsGroupId, String region, Integer fromPrice,
Integer toPrice, Boolean fixedPrice, Boolean freeDelivery, Boolean productWarranty,
Boolean urgentSales, Boolean hasImage, Integer pageNumber, Integer pageSize, List<String> tags) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Ads> query = builder.createQuery(Ads.class);
Root<Ads> ads = query.from(Ads.class);
// query.orderBy(builder.desc(ads.get("adsDate")));
List<Predicate> predicates = new ArrayList<>();
Join<Ads, JwtUser> adsUsersJoin = ads.join("users");
Join<Ads, AdsSubGroup> adsAdsSubGroupJoin = ads.join("adssubgroup");
Join<Ads, Tag> tagsJoin = ads.join("adsTags");
In<List<String>> in = builder.in(tagsJoin.get("name"));
if (tags != null && tags.size() > 0) {
// in.value(tags);
tags.forEach(tag - > in.value(tags));
/*
* for (String tag : tags) { in.value(tag);
*
* }
*/
predicates.add(in);
}
query.select(ads);
query.distinct(true);
query.where(predicates.toArray(new Predicate[0]));
if (!(pageNumber == null && pageSize == null)) {
TypedQuery<Ads> typedQuery = em.createQuery(query);
typedQuery.setFirstResult((pageNumber - 1) * pageSize);
typedQuery.setMaxResults(pageSize);
List<Ads> adsList = typedQuery.getResultList();
return AdsConverter.convertToAdsDTO(adsList);
} else {
List<Ads> adsList = em.createQuery(query).getResultList();
return AdsConverter.convertToAdsDTO(adsList);
}
}
What I need to write instead of In interface to get all ads which have ALL tags from the list, not at least one?
You need a join for each element of type Tag in your list.
It's hard to fix your code because it's not a minimal example, however the following should help:
CriteriaQuery<Ads> query = builder.createQuery(Ads.class);
Root<Ads> ads = query.from(Ads.class);
List<Predicate> predicates = new ArrayList<>();
for (String tag : tags) {
Join<Ads, Tag> tagsJoin = ads.join("adsTags");
predicates.add(builder.equal(tagsJoin.get("name"), tag));
}

How to access JPQL query return value (List)

This is my method
public void makeDeposit(String WristID, double deposit) {
List resultList = em.createQuery(
"SELECT c FROM Tbleaccountcb005916 c WHERE c.wristid LIKE :wId")
.setParameter("wId", WristID)
.setMaxResults(1)
.getResultList();
int aNo = (int) resultList.get(0);
Tbleaccountcb005916 makeDeposit = em.find(Tbleaccountcb005916.class, aNo);
double balance = makeDeposit.getBalance();
double Mdeposit = balance + deposit;
makeDeposit.setBalance(Mdeposit);
em.persist(makeDeposit);
}
I would like to access resultList value. This image show the resultList values (debugged)
I need to access resultList value (accountno = value = 2) and use it.
I tried this but it's not working:
int aNo = (int) resultList.get(0);
The query you've used returns a list of Tbleaccountcb005916 since you're returning cin the query statement.
List resultList = em.createQuery(
"SELECT c FROM Tbleaccountcb005916 c WHERE c.wristid LIKE :wId")
.setParameter("wId", WristID)
.setMaxResults(1)
.getResultList();
You can return c.accountno instead or cast the resultList items to Tbleaccountcb005916and access it's value.
Thanks to #Samuel Kok I found an answer for this. I just change the query to "c.accountno"
#Override
public void makeDeposit(String WristID, double deposit) {
List resultList;
resultList = em.createQuery(
"SELECT c.accountno FROM Tbleaccountcb005916 c WHERE c.wristid LIKE :wId")
.setParameter("wId", WristID)
.setMaxResults(1)
.getResultList();
int aNo = (int) resultList.get(0);
Tbleaccountcb005916 makeDeposit = em.find(Tbleaccountcb005916.class, aNo);
double balance = makeDeposit.getBalance();
double Mdeposit = balance + deposit;
makeDeposit.setBalance(Mdeposit);
em.persist(makeDeposit);
}
Screen Shot of debugger
enter image description here

How to use the Postgres any-clause with JPA/Hibernate native queries (array parameters)

So we've got a whole lot of Postgres SQL queries stored in files and used from PHP. The task is to replace PHP with Java. We want to reuse as much of the queries "as is" to keep the migration path short. I can't get the Array parameters to work.
Here's a query example:
update user_devices
set some_date = now()
where some_id in (
select distinct some_id from user_devices
where user_id = any(:userIDs) and device_id = any(:deviceIDs)
and exists (select 1 from users where user_id = any(:userIDs) and customer_id = :customerID)
);
Note the "any" clauses, which cause the problem, because they expect an array type.
This is how we used them from PHP:
$this->allValues['userIDs'] = '{' . implode ( ",", $userIdNodes ) . '}';
$this->allValues['deviceIDs'] = '{' . implode ( ",", $deviceIdNodes ) . '}';
$this->allValues['customerID'] = customerID;
$this->db->runQuery ( $this->getQuery ( 'my_query' ), $this->allValues );
So as parameters the array types look like "{111,222}".
This is what I tried in Java:
Integer customerID = 1;
int[] userIDs = new int[]{111,222};
int[] deviceIDs= new int[]{333,444};
//List<Integer> userIDs = Arrays.asList(111,222);
//List<Integer> deviceIDs= Arrays.asList(333,444);
//java.sql.Array userIDs = toArray("integer", new int[]{111,222}));
//java.sql.Array deviceIDs= toArray("integer", new int[]{333,444}));
//java.sql.Array userIDs = toArray("integer", Arrays.asList(111,222)));
//java.sql.Array deviceIDs= toArray("integer", Arrays.asList(333,444)));
//String userIDs = "{111,222}";
//String deviceIDs= "{333,444}";
//String userIDs = "ARRAY[111,222]";
//String deviceIDs= "ARRAY[333,444]";
Query nativeQuery = em.createNativeQuery(queryString);
nativeQuery.setParameter("userIDs", userIDs);
nativeQuery.setParameter("deviceIDs", deviceIDs);
nativeQuery.setParameter("customerID", customerID);
//nativeQuery.setParameter(createParameter("userIDs",java.sql.Array.class), userIDs);
//nativeQuery.setParameter(createParameter("userIDs",java.sql.Array.class), deviceIDs);
//nativeQuery.setParameter(createParameter("customerID", Integer.class), customerID);
query.executeUpdate();
//[...]
private Array toArray(String typeName, Object... elements) {
Session session = em.unwrap(Session.class); // ATTENTION! This is Hibernate-specific!
final AtomicReference<Array> aRef = new AtomicReference<>();
session.doWork((c) -> {
aRef.set(c.createArrayOf(typeName, elements));
});
return aRef.get();
}
private <T> Parameter<T> createParameter(final String name, final Class<?> clazz) {
return new Parameter<T>() {
#Override
public String getName() {
return name;
}
#Override
public Integer getPosition() {
return null; // not used
}
#Override
public Class<T> getParameterType() {
return (Class<T>) clazz;
}
};
}
None of these will work I will get one of these exceptions:
When using the "toArray" method:
Caused by: org.hibernate.HibernateException: Could not determine a type for class: org.postgresql.jdbc4.Jdbc4Array
at org.hibernate.internal.AbstractQueryImpl.guessType(AbstractQueryImpl.java:550)
at org.hibernate.internal.AbstractQueryImpl.guessType(AbstractQueryImpl.java:534)
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:519)
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:487)
at org.hibernate.jpa.internal.QueryImpl$ParameterRegistrationImpl.bindValue(QueryImpl.java:247)
at org.hibernate.
Or when using int[] or Strings, I'll get:
Caused by: org.postgresql.util.PSQLException: ERROR: op ANY/ALL (array) requires array on right side
Position: 137
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:366)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.postgresql.ds.jdbc23.AbstractJdbc23PooledConnection$StatementHandler.invoke(AbstractJdbc23PooledConnection.java:453)
at com.sun.proxy.$Proxy274.executeUpdate(Unknown Source)
at com.sun.gjc.spi.base.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:125)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:582)
Using Wireshark I found this when both APIs are talking to the database:
Image: Comparison of database calls with Wireshark
select oid, typname from pg_type where oid in (0, 23, 1043) order by oid;
oid |typname
------+-------
23 |int4
1043 |varchar
Has anyone managed to use array-parameters with native queries using Hibernate as backend for the JPA EntityManager? If so: How?
Change your query from where user_id = any(:userIDs) to where user_id IN (:userIDs), and change the userIDs array to a collection e.g. List<Long>. You will have to additionally protect it empty lists, but it will work.
I was able to work around this problem by unwrapping the Hibernate session from the EntityManager and use a JDBC PreparedStatement, which eats the java.sql.Array parameters without any complaint.
The NamedParameterStatement used in the example below is described here (I've modified it to my needs). It delegates to a PreparedStatement.
The rest of the code goes a little something like this:
public int executeUpdate(...){
//....
Integer customerID = 1;
java.sql.Array userIDs = toArray("integer", new int[]{111,222}));
java.sql.Array deviceIDs= toArray("integer", new int[]{333,444}));
final AtomicInteger rowsModifiedRef = new AtomicInteger();
final Session session = em.unwrap(Session.class); // ATTENTION! This is Hibernate-specific!
session.doWork((c) -> {
try (final NamedParameterStatement statement = new NamedParameterStatement(c, queryString)) {
statement.setObject("deviceIDs", userIDs);
statement.setObject("userIDs", userIDs);
statement.setObject("customerID", userIDs);
rowsModifiedRef.set(statement.executeUpdate());
}
});
return rowsModifiedRef.get();
}
private Array toArray(String typeName, Object... elements) {
Session session = em.unwrap(Session.class); // ATTENTION! This is Hibernate-specific!
final AtomicReference<Array> aRef = new AtomicReference<>();
session.doWork((c) -> {
aRef.set(c.createArrayOf(typeName, elements));
});
return aRef.get();
}