Hibernate version: 3.1.1
Hibernate-Annotations version: 3.1beta7
Configuration document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.dbcp.maxActive">8</property>
<property name="hibernate.dbcp.maxIdle">8</property>
<property name="hibernate.dbcp.maxWait">-1</property>
<property name="hibernate.dbcp.ps.maxActive">8</property>
<property name="hibernate.dbcp.ps.maxIdle">8</property>
<property name="hibernate.dbcp.ps.maxWait">-1</property>
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="show_sql">true</property>
<mapping class="org.evolizer.model.Data"/>
</session-factory>
</hibernate-configuration>
Class Annotations:
@Entity
public class Data {
private Long id;
private String key;
@Id(generate = GeneratorType.AUTO)
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getKey() { return this.key; }
public void setKey(String key) { this.key = key; }
}
Code between sessionFactory.openSession() and session.close():
Data data = new Data();
data.setKey("123");
Session session = HibernateUtil2.getSession();
session.saveOrUpdate(data);
Full stack trace of any exception that occurs:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [org.evolizer.model.Data]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:529)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:521)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:517)
at org.evolizer.model.DataStore.main(DataStore.java:22)
Caused by: java.sql.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 'key) values ('123')' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2978)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2902)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:930)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1968)
... 14 more
Name and version of the database you are using:
MySQL 4.1
The generated SQL (show_sql=true):
insert into Data (key) values (?)
Debug level Hibernate log excerpt:
Apr 7, 2006 10:34:57 AM org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: Unsuccessful: create table Data (id bigint not null auto_increment, key varchar(255), primary key (id))
Apr 7, 2006 10:34:57 AM org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: 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 'varchar(255), primary key (id))' at line 1
Apr 7, 2006 10:34:57 AM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: schema export complete
Hibernate: insert into Data (key) values (?)
Apr 7, 2006 10:34:57 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1064, SQLState: 42000
Apr 7, 2006 10:34:57 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: 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) values ('123')' at line 1
I tried to call the sql statements, which Hibernate prints out, with the mysql-query-browser. It seems that the column name "key" causes problems for MySQL. I suppose this is a reserved key word.
How can I change the configuration of Hibernate so that it produces automatically column names that are not equal to reserved key words of the database. (org.hibernate.dialect.MySQLDialect?)
|