Using :-
Postgres : PostgreSQL 10.0
Hibernate : 4.0.1
I have the following example table created in Postgres
CREATE TABLE Automobile
(id SERIAL
,name VARCHAR(20)
,year INTEGER
);
I have created a domain Automobile class.
I have the following hibernate criteria query to return all the names for a particular year.
final Criteria criteria = session.createCriteria (Automobile.class);
criteria.add (Restrictions.eq ("year", 1 ));
List<Automobile> list = criteria.list();
However, the list() is generating the following exception.
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
So I changed query to pass in a String value for the "year" :-
criteria.add (Restrictions.eq ("year", new String("1")));
List<Automobile> list = criteria.list();
However, the list() is generating a different exception.
org.hibernate.exception.SQLGrammarException: ERROR: operator does not exist: integer = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 1145
Any thoughts on what maybe the cause and solution that would return me list of matches please ?
Thank you
Pete.
In the Hibernate mapping you have
<property name="year" type="java.lang.String" >
You didn't show you Java code. I suppose year is also String there.
But in the database year is an integer.
Solution: Change your mapping as follows:
Also change your Java class and define year as Integer.
Related
I am trying to update a JSONB field in one table with data from another table. For example,
update ms
set data = data || '{"COMMERCIAL": 3.4, "PCT" : medi_percent}'
from mix
where mix.id = mss.data_id
and data_id = 6000
and set_id = 20
This is giving me the following error -
Invalid input syntax for type json
DETAIL: Token "medi_percent" is invalid.
When I change medi_percent to a number, I don't get this error.
{"COMMERCIAL": 3.4, "PCT" : medi_percent} is not a valid JSON text. Notice there is no string interpolation happening here. You might be looking for
json_build_object('COMMERCIAL', 3.4, 'PCT', medi_percent)
instead where medi_percent is now an expression (that will presumably refer to your mix column).
Operator does not exist: character varying = uuid
Client id is UUId and should be why it is not working.
Where I am wrong, since I have tried almost everything I imagined.
SELECT * FROM "cobranca_assinatura"
INNER JOIN "cadastro_cliente" ON ("cobranca_assinatura"."cliente_id" = "cadastro_cliente"."id")
WHERE "cadastro_cliente"."nome" LIKE marcelo% ESCAPE '\'
[2019-03-21 14:40:34] [42883] ERROR: operator does not exist:
character varying = uuid [2019-03-21 14:40:34]
Dica: No operator
matches the given name and argument type(s). You might need to add
explicit type casts.
uuid is a specific datatype. To you it looks like text, but it's not. You cannot compare uuid using string functions (uuid like "abc%"), or compare it with text.
As Tamer suggests, you can cast it first, if you need to compare.
SELECT *
FROM (SELECT 'A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11'::uuid as my_uuid) foo
WHERE my_uuid::text like 'a%'
For example, above I create a uuid by casting a string to uuid type. (You'll fail if you attempt to cast just any old string to uuid because 'abc' cannot be a uuid).
Then with a uuid item called 'my_uuid', I cast to back to a string to use string comparison. (Note the 'A' becomes 'a'!)
In java -> spring boot for JDBC template. I got the same issue. This is type-mismatch error, it's expecting UUID datatype but supplying String.
So, I converted UUID from String using UUID.fromString() and using UUID type in prepared statement (SQL)
Example:
String testSelectQry = "SELECT * from facility_announcements where id=:announcementID";
SqlParameterSource params = new MapSqlParameterSource("announcementID", UUID.fromString("094b76da-4140-11eb-b139-0242ac11000f"));
namedParameterJdbcTemplate.query(testSelectQry, params, new FacilityAnnouncementMapper());
My table in DB contains varchar field which contains both string and integer values. Requirement is compare only integers with provided value.
This code
from c in Cars
where SqlFunctions.IsNumeric(c.Code)
&& Convert.ToInt32(c.Code) == 12
throws
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
Runing this query in LINQPad is working fine! Why?
Question: How to Convert string directly in db and compare with provided integer value.
Why not just:
from c in Cars
where c.Code == "12"
I am joining the two tables using the query below:
update campaign_items
set last_modified = evt.event_time
from (
select max(event_time) event_time
,result
from events
where request = '/campaignitem/add'
group by result
) evt
where evt.result = campaign_items.id
where the result column is of character varying type and the id is of integer type
But the data in the result column contains digits(i.e. 12345)
How would I run this query with converting the type of the result(character) into id
(integer)
Well you don't need to because postgresql will do implicit type conversion in this situation. For example, you can try
select ' 12 ' = 12
You will see that it returns true even though there is extra whitespace in the string version. Nevertheless, if you need explicit conversion.
where evt.result::int = campaign_items.id
According to your comment you have values like convRepeatDelay, these obviously cannot be converted to int. What you should then do is convert your int to char!!
where evt.result = campaign_items.id::char
There are several solutions. You can use the cast operator :: to cast a value from a given type into another type:
WHERE evt.result::int = campaign_items.id
You can also use the CAST function, which is more portable:
WHERE CAST(evt.result AS int) = campaign_items.id
Note that to improve performances, you can add an index on the casting operation (note the mandatory double parentheses), but then you have to use GROUP BY result::int instead of GROUP BY result to take advantage of the index:
CREATE INDEX i_events_result ON events_items ((result::int));
By the way the best option is maybe to change the result column type to int if you know that it will only contain integers ;-)
There is geometry type column in database like Postgis or h2gis(I am using it).
In the console provided by database, I can create a geometry value with select ST_GeomFromText('POINT(12.3 12)', 4326).
Or select a column with geometry type simply by select * from geom.
However I don't know how to insert a geometry value (a string actually) into a table or the opposite direction conversion.
There are also several miscellaneous question below.
Here is the table definition in slick:
class TableSimple(tag:Tag) extends Table[ (Double,String,String) ](tag,"tb_simple"){
def col_double = column[Double]("col_double",O.NotNull)
def col_str = column[String]("col_str",O.NotNull)
def geom = column[String]("geom",O.DBType("Geometry"))
def * = (col_double,col_str,geom)
}
1. About select
The most simple one:
sql" select col_double,col_str, geom from tb_simple ".as[(Double,String,String)]
won't work unless casting geom to string explicitly like:
sql" select col_double,col_str, cast( geom as varchar) from tb_simple ".as[(Double,String,String)]
The first sql throws the error java.lang.ClassNotFoundException: com.vividsolutions.jts.io.ParseException
Q1: How does slick know com.vividsolutions.jts.io.ParseException (it is lib used by h2gis)? Is it an error on the server side or client side(slick side)?
Q2: How to convert/treat column geom as string without writing too much code(e.g. create a new column type in slick)?
2. About insert
First of all the following sql works
StaticQuery.updateNA(""" insert into tb_simple values(11,'abcd',ST_GeomFromText('POINT(5.300000 1.100000)', 4326)) """).execute
I hope code like TableQuery[TableSimple] += (10.3,"hello","ST_GeomFromText('POINT(0.300000 1.100000)'") would work but it doesn't.
It shouldn't because slick translate it to
insert into tb_simple values(11,'abcd','ST_GeomFromText(''POINT(5.300000 1.100000)'', 4326)')
Notice the function ST_GeomFromText become a part of string, that's why it doesn't work.
Q3: Can I implant a string directly for a column instead of wrapped with '' in slick?
I hope I can insert a row as easy as TableQuery[TableSimple] += (10.3,"hello","ST_GeomFromText('POINT(0.300000 1.100000)'") or similar code.
Q4 What's the most convenient way in Slick to implement bidirectional conversion to and from String for a geometry or other self-defined column in the database?
Answering you main question: Slick-pg offers mapping of geometry types in the db to actual geometry types in your model.
It works for Postgis, but maybe it can also work with H2Gis.
You can find slick-pg at https://github.com/tminglei/slick-pg