Hibernate generating wrong sql syntax - java

Hibernate generating wrong sql(MySQL) query with syntax error.
given this HQL query :
"update GpClientContacter set id = :newKey where id = :Key"
Hibernate executes this sql query:
Hibernate: update GP_CLIENT_CONTACTER set CODE_CLIENT, NOM_RS=(?, ?) where (CODE_CLIENT, NOM_RS)=(?, ?)
Here is exception message:
ERROR: 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 ' NOM_RS=(15, 'test1') where (CODE_CLIENT, NOM_RS)=(15, 'test5')' at line 1
.
.
org.hibernate.exception.SQLGrammarException: could not execute statement
id is an EmbeddedId that has the codeClient and nomRs fields. :key and :newKey are instances of the EmbeddedId Type

Related

Unknown column sql error while using jooq

Query:
this.dslContext.select(
ROLE.asterisk(),
multiset(
select(PERMISSION.asterisk())
.from(ROLE_PERMISSION)
.innerJoin(PERMISSION)
.on(ROLE_PERMISSION.PERMISSION_ID.eq(PERMISSION.ID))
.where(ROLE_PERMISSION.ROLE_ID.eq(ROLE.ID))
).as("permissions")
).from(ROLE)
.where(ROLE.ID.eq(id))
.fetchOneInto(Role.class)
Error:
jOOQ; bad SQL grammar [set #t = ##group_concat_max_len; set ##group_concat_max_len = 4294967295; select `users`.`role`.*, (select coalesce(json_merge_preserve('[]', concat('[', group_concat(json_array(`v0`, `v1`) separator ','), ']')), json_array()) from (select `users`.`permission`.`id` as `v0`, `users`.`permission`.`name` as `v1` from `users`.`role_permission` join `users`.`permission` on `users`.`role_permission`.`permission_id` = `users`.`permission`.`id` where `users`.`role_permission`.`role_id` = `users`.`role`.`id`) as `t`) as `permissions` from `users`.`role` where `users`.`role`.`id` = ?; set ##group_concat_max_len = #t;]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'users.role.id' in 'where clause'.
Database: MYSQL, Database Name: 'users', JOOQ Version: 3.16.6
Correlating derived tables isn't supported in MySQL 5.7. Support has been added only in MySQL 8.0.14:
https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html
jOOQ currently can't work around this limitation, see:
https://github.com/jOOQ/jOOQ/issues/12045
The solution is either:
Upgrade your MySQL version
Use a MULTISET_AGG based approach instead

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 'order

What is causing this exception, I have used the similar approach in a UserController and that worked fine. Is the mapping of SQL table with Entity Classes wrong ?
I can't store data and I can't retrieve anything from it either. It keeps giving me the same error, Initially I though my Latitude and Longitude might be wrong but then I tried inserting a simple user_id and is being rejected too.
Controller
#Autowired
OrderMapper orderMapper;
#ResponseBody
#RequestMapping(value = "/getOrder", method = RequestMethod.POST)
public Order PlaceOrder()
{
Order order = new Order();
order.setUserId(1);
orderMapper.insert(order);
return order;
}
OrderMapper
<insert id="insert" parameterType="com.mena.api.entity.Order">
insert into order (order_id, user_id, start_latitude,
start_logitude, end_latitude, end_logitude,
total_distance, type, cost,
create_time, start_address, end_address,
user_id2)
values (#{orderId,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{startLatitude,jdbcType=DECIMAL},
#{startLogitude,jdbcType=DECIMAL}, #{endLatitude,jdbcType=DECIMAL}, #{endLogitude,jdbcType=DECIMAL},
#{totalDistance,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{cost,jdbcType=VARCHAR},
#{createTime,jdbcType=TIMESTAMP}, #{startAddress,jdbcType=VARCHAR}, #{endAddress,jdbcType=VARCHAR},
#{userId2,jdbcType=INTEGER})</insert>
And SQL Table Info
Error Log
</pre><p><b>Root Cause</b></p><pre>org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'order
where order_id = 1' at line 6
### The error may exist in file [\khadim\khadimApi\khadimApi\target\khadminapi\WEB-INF\classes\mybatis\OrderMapper.xml]
### The error may involve com.mena.api.mapper.OrderMapper.selectByPrimaryKey-Inline
### The error occurred while setting parameters
### SQL: select 'true' as QUERYID, order_id, user_id, start_latitude, start_logitude, end_latitude, end_logitude, total_distance, type,
cost, create_time, start_address, end_address, user_id2 from order where order_id = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'order
where order_id = 1' at line 6
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'order
where order_id = 1' at line 6
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235)
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:75)
org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
com.sun.proxy.$Proxy35.selectOne(Unknown Source)
org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:167)
org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
com.sun.proxy.$Proxy36.selectByPrimaryKey(Unknown Source)
com.mena.api.controller.OrderController.PlaceOrder(OrderController.java:34)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
order is a reserved word in MySql, this is why your select ... from order probably fails with that exception. If order really is the table name in your database, you could escape it as follows:
select ... from `order` ...
Or better yet would be to use a non-reserved table name, like 'orders'.

How to write Date range query in H2 using jooq

I'm using mybatis library to execute native sql queries.
I'm using jooq to generate dynamic sql queries for different dialects.
Syntax:
condition = condition.and(DSL.field("START_TIME_").between("2014-10-12")
.and("2014-10-12"));
Error:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ACT_HI_PROCINST WHERE (DELETE_REASON_ IS NULL AND START_TIME_ BETWEEN CAST(:[*]1 AS VARCHAR) AND CAST(:2 AS VARCHAR))
LIMIT ? OFFSET ? "; expected "NOT, EXISTS, INTERSECTS, SELECT, FROM"; SQL statement:
select *
from ACT_HI_PROCINST
where (DELETE_REASON_ is null and START_TIME_ between cast(:1 as varchar) and cast(:2 as varchar))

SQL prepared statement throwing an error about syntax

So I am using SQL with phpMyAdmin. Now I want to make an update to my database with a prepared statement but doind it gives me the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'autor='Lol' WHERE id=44' at line 1
And this is how my statement looks like:
command = connection.prepareStatement("UPDATE books SET name=? author=? WHERE id=?");
command.setString(1, name.getText());
command.setString(2, author.getText());
command.setInt(3, IDx);
command.execute();
Wat is wrong with the statement I have made? It should be working In my opinion.
You need to separate the updated fields in a comma like:
command = connection.prepareStatement("UPDATE books SET name=?, author=? WHERE id=?");
You forgot a comma before author
UPDATE books
SET name = ?, author = ?
WHERE id = ?

MyBatis java and MySql local variables

I'm new to java world. And I have a problem with simple query:
<insert id="create" parameterType="models.entities.CategoryEntity">
set #catId := (select categoryId from Categories limit 1);
insert into Categories(CategoryId, Title, LeftValue, RightValue)
values(#catId, 'Test in', 1,2);
....
</insert>
it simply fails when i trying to run it with mybatis:
PersistenceException occured : ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2 ### The error may involve Category.create-Inline ### The error occurred while setting parameters ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'insert into Categories(CategoryId, Title, LeftValue, RightValue) values(' at line 2
If I remove this line:
set #catId := (select categoryId from Categories limit 1);
then everything is ok. What am i doing wrong? Is it problem with jdbc or mybatis? How to use mysql #variables with mybatis? Does someone have examples of using MySql local variables with mybatis?
I found out how to get it worked. Just set datasource property allowMultiQueries=true
jdbc:mysql://localhost:3306/DBS?allowMultiQueries=true

Categories

Resources