无法使用auto-increment键作为PK在mysql表中插入记录

时间:2011-04-15 15:38:31

标签: mysql hibernate

我无法插入记录。我正在使用hibernate和mysql。

文件如下......

Route.hbm.xml

    <id name="routeId" type="java.lang.Integer" unsaved-value="null">
        <column name="ROUTE_ID" not-null="true" />
        <generator class="increment" />
    </id>
    <property name="routeName" type="java.lang.String" column="ROUTE_NAME" />
    <property name="from" type="java.lang.String" column="FROM" />
    <property name="to" type="java.lang.String" column="TO" />
    <property name="distance" type="java.lang.Integer" column="DISTANCE" />
    <property name="time" type="java.lang.Integer" column="TIME" />

RouteDTO.java

private static final long serialVersionUID = 1L;
private Integer routeId;
private String routeName;
private String from;
private String to;
private Integer distance;
private Integer time;


public Integer getRouteId() {
    return routeId;
}
public void setRouteId(Integer routeId) {
    this.routeId = routeId;
}
.
.
.
.

表格结构

CREATE TABLE `route` (
  `ROUTE_ID` int(5) NOT NULL AUTO_INCREMENT,
  `ROUTE_NAME` varchar(20) NOT NULL,
  `FROM` varchar(20) NOT NULL,
  `TO` varchar(20) NOT NULL,
  `DISTANCE` int(11) DEFAULT NULL,
  `TIME` int(11) DEFAULT NULL,
  PRIMARY KEY (`ROUTE_ID`)
) 

当我尝试按RoutDAO.java

保存此记录时

代码是

getHibernateTemplate().save(routeDto);

它给我的错误如下....

Hibernate: select routedto0_.ROUTE_ID as ROUTE1_, routedto0_.ROUTE_NAME as ROUTE2_0_, routedto0_.FROM as FROM0_, routedto0_.TO as TO0_, routedto0_.DISTANCE as DISTANCE0_, routedto0_.TIME as TIME0_ from ROUTE routedto0_
Hibernate: insert into ROUTE (ROUTE_NAME, FROM, TO, DISTANCE, TIME, ROUTE_ID) values (?, ?, ?, ?, ?, ?)
Apr 15, 2011 9:04:03 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1064, SQLState: 42000
Apr 15, 2011 9:04:03 PM 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 'FROM, TO, DISTANCE, TIME, ROUTE_ID) values ('A099', 'Hadapser', 'Pune Station', ' at line 1
Apr 15, 2011 9:04:03 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
    at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
    at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:726)
......

Caused by: java.sql.BatchUpdateException: 
  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, TO, DISTANCE, TIME, ROUTE_ID) values 
  ('A099', 'Hadapser', 'Pune Station', ' at line 1
    at 

com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
    at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
    ... 58 more

请帮帮我......

2 个答案:

答案 0 :(得分:1)

这是因为名为FROM的列。这是一个SQL关键字。因此,Hibernate发布的SQL无效。我有点惊讶MySQL让你先创建表。

您应为列添加一个与FROM不同的名称。

答案 1 :(得分:1)

我认为问题是您使用MySQL reserved words作为列名。