I'm trying to pass an array of string as a parameter to my query but I get the following error
ERROR: operator does not exist: text ~~ record Dica: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Here is my query
select * from table where value like any (array[?1]);
when I run it using hibernate the query is like:
select * from table where value like any (array[('%foo%', '%bar%', '%baz%')]);
There's a best way to pass my array as parameter?? I think that is important to say that my array is dynamic so i can't fiz it in my query.
use setParameterList method.
String queryStr = "select * from table where value in :valueList";
Query query = SessionFactory.getCurrentSession().createQuery(queryStr);
query.setParameterList("valueList", new Object[]{"%foo%","%bar%","%baz%"});
Please note that I used in clause and it doesn't support wildcard characters such as %.
First use createNativeQuery instead of createQuery as syntax is native to PSQL.
Second query syntax should be select * from table where value like any (array?1) as ?1 will be substituted by ['%foo%', '%bar%', '%baz%'], so your end query will match required PSQL syntax.
select * from table where value like any (array['%foo%', '%bar%', '%baz%'])
Firstly, your syntax is wrong.
Instead of:
select * from table where value like any (array[?1]);
You should use:
select * from table where value like any (:vals);
You cannot use array[?] or array[:var] to construct the variable. That is invalid syntax.
Secondly for Hibernate 5.2 up to 5.4 you can just add this dependency and you can use the most common object arrays directly. Primitive arrays are not supported and are not supposed to be.
Related
I have a db2 table t1 where one of the columns is populated by utilizing a scalar function that takes two string params.
function(string foo, string bar)
In prepare the sql code as follows:
UPDATE t1 SET col1 = function(?, 'foobar') WHERE t1.id = ?
When i now add a parameter to the prepared statement i get an error that the first parameter is of the wrong type.
for (Entry entry : entries) {
preparedStatement.setString(1, entry.getDesc());
preparedStatement.setInt(2, entry.getId());
}
I already tried wrapping the first param in single quotes but that didn't work. Did anyone else have this problem and has come to a solution?
The main problem is that i'm now sure how the db2 engine prepares the first string parameter for the scalar function so that the error is thrown in the first place.
Try function(cast(? as varchar(xxx)), 'foobar'), where xxx the corresponding length of the 1-st string parameter of the function. The function's 1-st parameter must be of varchar data type in this case. If its 1-st parameter is of another data type, then use the corresponding cast and set<Datatype>.
You need to do it, since there may be multiple functions with the same name and number of parameters, but with different data types. And Db2 must know which function to use exactly during the query compilation (prepare).
i have stuck :)
does anyone know how query postgresql in jsonb
i have table USER
INT id,
Varchar name,
jsonb categoryId
the example data in categoryId field like this = [1,2,3,4,5]
I have tried this query which works:
select *
from user where categoryId #> '2'::jsonb ;
but how to query with multiple params like
select *
from user
where categoryId #> '1,3,4'::jsonb
and i will implement this to hibernate jpa/jpql-predicate, but i want to know native query first
Thankyou so much
In SQL you can use the ?| operator for this
select *
from "user"
where categoryid ?| array['1','3','4'];
That will return rows where at least one of the values is contained in the JSON array. If you want to find those that contain all values, use the ?& operator instead.
To use the #> operator you would need to use a JSON array on the right hand side:
select *
from "user"
where categoryid #> '[1,3,4]'::jsonb
Note that user is a reserved keyword. You will have to enclose it in double quotes every time you want to refer to the table. I would highly recommend to find a different table name.
I have a type as:
CREATE TYPE status_record AS
(
id bigint,
status boolean
);
A procedure that does some processing with an array of type as input parameter as:
CREATE OR REPLACE FUNCTION update_status(status_list status_record[])
RETURNS text AS
$BODY$
DECLARE
BEGIN
--does some processing
return 'SUCCESS';
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Finally I queried the procedure as:
select *
from update_status(cast(ARRAY[(385,false),(387,false)] as status_record[]));
Everything works fine in pgadmin. Later when I tried to call the same using Hibernate native SQL Query Ka Boom!!! The following is displayed:
org.postgresql.util.PSQLException:
ERROR: array value must start with "{" or dimension information
Final question: both ARRAY[--something] and {--something} does the same job?
Use an array literal (text representation of the array), since the array constructor ARRAY[...] has to be evaluated by Postgres:
SELECT update_status('{"(1,t)","(2,f)"}'::status_record[]);
Maybe even without the explicit cast:
SELECT update_status('{"(1,t)","(2,f)"}');
There were similar cases on SO before:
Pass array from node-postgres to plpgsql function
How to pass custom type array to Postgres function
Try to put the array and type initialization into a string, maybe you can then get around the problems with the obfuscation layer (aka ORM):
select update_status(cast('{"(1,true)", "(2,false)"}' as status_record[]));
I don't know Hibernate, so I can't tell if that will work.
I have the following select-query creation:
final DSLContext create = DSL.using(..., SQLDialect.POSTGRES);
create
.select(DSL.field("identifier"), DSL.field("name"),
create.selectCount()
.from(DSL.table("person"))
.where(DSL.field("identifier").eq(DSL.field("personOuter.identifier")))
.asField("count"))
.from(DSL.table("person").as("personOuter"))
jOOQ generates the following query:
select
identifier,
name,
(select count(*)
from person
where identifier = personOuter.identifier) as "count"
from person as "personOuter"
The query should be:
select
identifier,
name,
(select count(*)
from person
where identifier = personOuter.identifier) as "count"
from person as personOuter
The latter query works perfectly in PostgreSQL. The table alias should not be surrounded by quotes.
Is this a bug?
(Note that the query is pretty dumb. I am playing around with jOOQ to evaluate.)
The following "hack" works:
create
.select(DSL.field("identifier"), DSL.field("name"),
create.selectCount()
.from(DSL.table("person"))
.where(DSL.field("identifier").eq(DSL.field("personOuter.identifier")))
.asField("count"))
.from("person as personOuter")
A note on using the code generator
I'm assuming you have a good reason to avoid using the code generator (e.g. you work on a dynamic schema), because working with generated code prevents having to worry about such details. Plus, you get access to many advanced features, like implicit joins, embeddable types, etc.
What's a string in the jOOQ API?
By default, jOOQ will wrap all your identifiers in quotes in order to be able to handle case-sensitivity correctly.
The confusing part is why this isn't done for DSL.field(String), but only for Field.as(String). The reason for this is that jOOQ re-uses the String type for both:
Plain SQL as in DSL.field(String), where the input String doesn't really represent an identifier, but an arbitrary SQL expression
Identifiers as in DSL.name(String), where the input String represents a name / identifier. There is also DSL.fieldByName(String) to create Field types composed of (schema) / table / column identifiers.
In order to remove the quotes from all generated identifiers, you can also change the Settings.renderNameStyle to RenderNameStyle.AS_IS.
More information about Settings can be found here. And also in this blog post about "What’s a “String” in the jOOQ API?"
I have two tables that I want to join using hibernate. The join column, as represented in my model is a String (its a varchar(10) in my database). When I run the HQL query, what I see is the following error, "conversion failed when converting the varchar value 'AS00' to data type int. "AS500" is the first value of join column in the first row.
I do not know why hibernate is doing this. My join column is not an int. I have checked both models corresponding to my tables and they are both defined as Strings. Is there some kind of restriction on the data types that can be used for join columns?
Please post both your model and the hql query.
If I had to take a guess (and that's all any of us can do without specifics), I would say that your hql query does not use .setParameter and it does not have single quotes around the string value in your query... so it is trying to implicitly convert the value to int.
Example that would cause this error:
Query query = session.createQuery("from Person where name = bob");