how to add boolean fields in java model class through hibernate annotations.
I am doing this and its shows me 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 'Like bit' at line 2
My code:
#Column(name="Like")
private boolean like;
#Column(name="Dislike")
private boolean dislike;
#Column(name="Flag")
private boolean flag;
In fact your problem is that you used Like as a column name in your entity, so Hibernate will try to map this column to the name Like while it's a reserved keyword in SQL, that's why you got the exception:
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 'Like bit' at line 2
What you can do here is either to use a different name of the column or escape the name.
For further details and escaping solutions you can check:
Hibernate Tips: How to escape table and column names article.
Creating field with reserved word name with JPA
Related
I recently updated a project's jooq version from 3.13.5 to 3.14.15. I've rerun jooq-codegen. I'm using MySQL 5.7.
When running one of my tests - it performs a DAOImpl.exists. That call generates the following exception:
org.jooq.exception.DataAccessException: SQL [select `count`(*) from `users` where `users`.`id` = ?]; 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 '*) from `users` where `users`.`id` = 1094' at line 1
at org.jooq_3.14.15.MYSQL.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2903)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:757)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:389)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:453)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:422)
at org.jooq.impl.AbstractResultQuery.fetchLazyNonAutoClosing(AbstractResultQuery.java:436)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:613)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:565)
at org.jooq.impl.SelectImpl.fetchOne(SelectImpl.java:3034)
at org.jooq.impl.DAOImpl.existsById(DAOImpl.java:300)
at org.jooq.impl.DAOImpl.exists(DAOImpl.java:288)
The problem is that count has backticks. It should be select count(*) from `users` where `users`.`id` = ?
Has anyone run into this before and know how to fix it? TIA
You probably specified RenderQuotedNames.ALWAYS and ran into this problem? https://github.com/jOOQ/jOOQ/issues/9931
The setting ALWAYS is misleading. It really means literally always, though people probably read it as ALWAYSISH_I_E_ONLY_WHEN_IT_MAKES_SENSE. Probably a naming design error, but what you want is EXPLICIT_DEFAULT_QUOTED, see the documentation:
https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-name-style/
RenderQuotedNames
ALWAYS: This will quote all identifiers.
EXPLICIT_DEFAULT_QUOTED: This will quote all identifiers, which are not explicitly unquoted using DSL.unquotedName().
EXPLICIT_DEFAULT_UNQUOTED: This will not quote any identifiers, unless they are explicitly quoted using DSL.quotedName().
NEVER: This will not quote any identifiers.
COUNT in DSL.count()is an explicitly unquoted name, and ALWAYS will override that.
I am trying to run my java program, in which I have to transfer data from a DB to another one.
Well, the program works but one of the column field is named "public", and when I'm trying to transfer that data using jdbc, it crashes with the following statement:
"incorrect syntax near from keyword 'public'".
I've tried to load the column field as [public], (public), `public` works in SQL syntax but not in the program, since it has to check equivalence for "public", so "'public' is returning false.
Some code sample when i load the datas :
String publicDb = iArboFromExtranet.get(i).getString("public");
Some code sample when i transfer it :
private static final String PUBLIC = "public";
IArbo.set(PUBLIC, Strings.nullToEmpty(publicVar))
By the way, i cant rename the fields since i'm working for IT services industry. Its not our database
Since you're using a framework to generate your SQL, and you use a reserved word as a column name, you have a (tough?) choice to make:
Update the implementation of IArbo to quote reserved words (or all names).
Don't use IArbo.
Don't use a reserved word as a column name.
It looks like you're building SQL insert and/or update queries. Quote everything, so if the database field is MyField and the table is MyTable your query should have "MyTable" and "MyField" for this particular field. In Java you can build the string by escaping the double quote with a backslash:
queryString = "insert into \"MyTable\" (\"MyField\"...) values ('whatever'...)";
When I try to execute:
SELECT matricula FROM lss_alumnos WHERE lss_alumnos.division = 'PREP';
I get this message:
SQLException: 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 '.division = 'PREP'' at line 1
And I am also using MySQL Workbench, and there I got the right result.
Try removing the qualified name lss_alumnos before the column name, also, remove the leading ;.
SELECT `matricula` FROM `lss_alumnos` WHERE `division` = 'PREP'
(It's also a good practice to surround the names with the ` symbol).
I believe the comments are closer to the truth than the only other answer. To be sure, this query will work:
SELECT la.`matricula` FROM `lss_alumnos` la WHERE la.`division`='PREP'
Edit: I checked. None of these words are a MySQL reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html. It must be the semicolon
We have a project includes Eclipselink and MySQL.
For example if we use simple query:
SELECT a from ExampleTable a
it is transformed to something like this:
SELECT `id`, `code`, `high`, `key`, `name`, `regionalCode` FROM `ExampleTable`
and if I am using JPA Console I am getting something like that:
SELECT id, code, high, key, name, regionalCode FROM ExampleTable
and error message
[42000][1064] 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 'key, name, regionalCode FROM ExampleTable' at line 1
The problem is "key" column but I have no chance to rename it.
I tried to find options to fix that but have no results.
I think if there will be no answer - the good way is to create bug report.
How we get enclosed names of columns?
We are using EntityManager
EntityManager em;
...
TypedQuery = em.createQuery("SELECT et FROM ExampleTable et");
And if we run it we see (in debug mode) that all names in generated query are enclosed with ` symbol.
key is reserved keyword of MySQL database (see MySQL: Reserved Words).
so you should escape reserved keyword in your mapping.
You can do it in the following way:
#Table
#Entity
public class ExampleTable implement Serializable
{
// ...
#Column(name = "'key'") // or #Column(name = "\"key\"")
private String key;
//...
}
I am trying to make a select from a table, which works fine with every other table in my database, but when I try the following I recieve an error:
db.makeQuery("SELECT * FROM References");
Which calls:
public ResultSet makeQuery(String query) throws Exception
{
preparedStatement = connect.prepareStatement(query);
resultSet = preparedStatement.executeQuery(query);
return resultSet;
}
It then throws 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 'References' at line 1
I seems very strange to me, since this statement works:
db.makeQuery("select * from Products");
References is a keyword in SQL, so you better avoid it for table names. (See for instance this documentation.)
As suggested by Nishant, you can use reserved words in queries if you escape them with backticks.
Related question:
Using MySQL keywords in a query?
use
db.makeQuery("SELECT * FROM `References`");
if you can, better, avoid having names that are MySQL keywords. As suggested by aioobe
You might be misspelling the name of your table. MySQL gives this error when it can't find that table you're referring to.
Use SHOW TABLES; to see the names of the tables in your database, and double-check the name.