I have a column in the database which has been defined as follows in the entity class:
#Column(name="lastlogindate", columnDefinition ="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
private Timestamp lastlogindate;
The create table command that is being generated is as follows:
create table usermaster (
lastlogindate `TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`)
while what works is
create table usermaster (
lastlogindate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
Hibernate reports the following error while creating tables:
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 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Unable to understand why extra ticks are being added and how to avoid it.
The version details are as follows:
HIBERNATE VERSION: 4.3.8.Final
MySQL version:5.6.26
MySQL Dialect information:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.autocommit ">false</property>
<property name="hibernate.globally_quoted_identifiers">true</property>
Please remove the following config line:
<property name="hibernate.globally_quoted_identifiers">true</property>
This is likely not needed and almost surely causing the problem you experience.
UPDATE:
The issue was finally solved by changing the dialect to MySQL5 from generic MySQL:
org.hibernate.dialect.MySQL5Dialect
Related
I am switching a Java application using Hibernate from Oracle to Postgres and encountering a issue with Id GeneratedValues.
The Domain objects have Ids configured like this:
#Id #GeneratedValue(strategy = GenerationType.AUTO) #Column(name = "id")
private Long id;
Under Oracle there was a sequence called "HIBERNATE_SEQUENCE" that provided this. I have created this sequence in Postgres like this:
CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT BY 1 START WITH 50000 CACHE 20 NO CYCLE ;
However, when persisting an object I'm getting an error of:
org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "hibernate_sequence"
I have tried:
switching the "GenerationType" to "SEQUENCE"
creating the sequence in lower case (hibernate_sequence)
But I get the same error in both cases.
You have to configure Hibernate to speak the PostgreSQL dialect of SQL.
In Oracle you get the next value of a sequence with a pseudo-column (hibernate_sequence.nextval), while in PostgreSQL you use a function (nextval(hibernate_sequence)). Using the Oracle syntax with PostgreSQL will cause the error you quote.
The main problem I had was not setting the hibernate.dialect property correctly.
It was
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
and changing it to
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
fixed it and started using the correct sequence function as Laurenze Able pointed out
I'm using Oracle XE database and annotation driven mapping. What column type should I choose for BigInteger value mapping in Hibernate?
If you will see documentation it already mentioned
big_decimal, big_integer
Type mappings from java.math.BigDecimal and java.math.BigInteger to
NUMERIC (or Oracle NUMBER).
here is an example which i got from Hibernate community
Hibernate 3.1.1 with Oracle 10g and XE. The script of my Table sample is this:
CREATE sample (
sampa NUMBER,
sampb NUMBER(5),
sampc NUMBER(10),
sampd NUMBER(15,2),
sampe NUMBER(19,7)
);
and corresponding
<property name="sampa" column="sampa" type="java.math.BigInteger"/>
<property name="sampb" column="sampb" type="java.math.BigInteger"/>
<property name="sampc" column="sampc" type="java.math.BigInteger"/>
<property name="sampd" column="sampd" type="java.math.BigDecimal" precision="15" scale="2"/>
<property name="sampe" column="sampe" type="java.math.BigDecimal" precision="19" scale="7"/>
I have this user_model.java below:
public class User_Model implements Serializable {
#Id #GeneratedValue
private Long id;
#ColumnTransformer(
read="AES_DECRYPT(username, 'Hf7p4u6e') USING utf8))",
write="AES_ENCRYPT(?, 'Hf7p4u6e')")
The above code is working when I remove USING utf8) but when I put it again I get this 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 '.USING user_mo0_.UTF-8).
I don't want to remove the utf8 because I used it for korean language.
I also put a utf8 setting in my hibernate.xml
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb?UseUnicode=true&characterEncoding=utf8</property>
<property name="hibernate.connection.CharSet">utf8</property>
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
But still, it's not work at all.How can I make my query work? any help
Two things:
Maybe you ned to use 'USING utf8' with CONVERT function (https://dev.mysql.com/doc/refman/5.0/en/charset-convert.html)
CONVERT(_latin1 AES_DECRYPT(username, 'Hf7p4u6e') USING utf8)
utf8 has nothing to do with your already created tables if they are not in UTF-8. This only forces newly created tables to use UTF-8. Existing tables remain as they are.
I am getting following exception at query.list() line:
org.hibernate.MappingException: No Dialect mapping for JDBC type: -4
at org.hibernate.dialect.TypeNames.get(TypeNames.java:56)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:81)
at org.hibernate.dialect.Dialect.getHibernateTypeName(Dialect.java:369)
at org.hibernate.loader.custom.CustomLoader$Metadata.getHibernateType(CustomLoader.java:559)
at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.performDiscovery(CustomLoader.java:485)
at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:501)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
following is my configuration file:
<property name="hibernate.connection.driver_resource">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">mysql</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">mydatabase</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
When I am trying to run application into Eclipse IDE then this exception is not coming but when I create jar of application and run then only I am getting it.
thanks in advance...
Sometimes database returns results of custom SQL queries in strange types that cannot be mapped to Hibernate types (especially when you use expressions under select).
You need to find an offending query and add an explicit cast to it.
For example
Object o = session.createSQLQuery("select 2*2").uniqueResult();
may cause such a problem. You may fix it as follows:
Object o = session.createSQLQuery("select cast(2*2 as int)").uniqueResult();
Got the solution:
Just change the Query, I am fetching whole record instead of select specific.
e.g. from table and then get respective field (here script) value from table Object instead of using select script from table, It is working fine now.
I have one sqlserver 2008 r2 datatable, it has one column autoId int identity(1,1), but it's not the primary key, another column varchar(20) is the one.
question is : how do i config the hbm file?
bellow is my config file,but it got errors when i try to save one instance.
"Cannot insert explicit value for identity column in table 'acct_info' when IDENTITY_INSERT is set to OFF."
<property name="autoId" type="int">
<column name="auto_id" not-null="true" unique="true" />
</property>
There can be two reasons , either you don't have sufficient privileges in DB for IDENTITY INSERT or there is mismatch in the mechanism by which you are trying to set an identifier in hibernate and DB layer.
You can have a look at your id generation strategy in hibernate definition file
In DB you can change to Set IDENTITY_INSERT to "ON"
Pick a different generator class