开发者

Not able to insert a record in mysql table with auto-increment key as PK

i am not able to insert a record. i am using hibernate and mysql.

files are as follows...

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" colum开发者_JS百科n="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;
}
.
.
.
.

Table Structure is

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`)
) 

when i am trying to save this record by RoutDAO.java

code is

getHibernateTemplate().save(routeDto);

it gives me error as follows....

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

Please help me out...


This is because of the column calledFROM. This is a SQL keyword. The SQL being issued by Hibernate is therefore invalid. I'm mildly surprised MySQL let you create the table in the first place.

You should pick a different name for your column than FROM.


I think the problem is you are using MySQL reserved words for column names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜