请教高手:spring +struts2+hibernate 问题

shenchuan010 2010-06-09 08:01:32
我是一个初学者,在写一个在线购物系统的生成订单时抛出异常,遇到了如下问题,查了好长时间也没解决,望哪位高手帮忙解决一下,谢谢啦
问题如下:description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not fetch initial value; bad SQL grammar [select max(id) from order]; nested exception is com.mysql.jdbc.exceptions.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 'order' at line 1

root cause

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not fetch initial value; bad SQL grammar [select max(id) from order]; nested exception is com.mysql.jdbc.exceptions.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 'order' at line 1
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:224)
org.springframework.orm.hibernate3.HibernateAccessor.convertJdbcAccessException

root cause

com.mysql.jdbc.exceptions.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 'order' at line 1
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:89)
org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:45)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:85)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId
。。。
部分代码如下:
数据库映射文件Order.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.onlineshop.bean.Order" table="order">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="orderId" type="java.lang.Integer">
<column name="orderId" not-null="true" />
</property>
<many-to-one name="cominfo" class="com.onlineshop.bean.ComInfo" fetch="select">
<column name="comid" not-null="true" />
</many-to-one>
<many-to-one name="userinfo" class="com.onlineshop.bean.User" fetch="select">
<column name="userid" not-null="true" />
</many-to-one>
<property name="number" type="java.lang.Integer">
<column name="number" not-null="true" />
</property>
<property name="address" type="java.lang.String">
<column name="address" length="128" not-null="true" />
</property>
<property name="postcode" type="java.lang.String">
<column name="postcode" length="10" not-null="true" />
</property>
<property name="orderdate" type="java.util.Date">
<column name="orderdate" length="19" not-null="true" />
</property>
<property name="status" type="java.lang.String">
<column name="status" not-null="true" />
</property>
</class>
</hibernate-mapping>

OrderDaoImpl 中生成订单的代码:
public class OrderDaoImpl extends HibernateDaoSupport implements OrderDao{

public void save(Order order){
getHibernateTemplate().saveOrUpdate(order);

}
。。。。。
}
...全文
248 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
enjoyinwind 2011-05-16
  • 打赏
  • 举报
回复
感谢lz,一语惊醒梦中人
qq9209999 2010-06-10
  • 打赏
  • 举报
回复
[select max(id) from order]; nested exception is com.mysql.jdbc.exceptions.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 'order' at line 1

你仔细检查你的数据库语句
报的错是数据库语句错误
也就是select max(id) from order这里错了
izard999 2010-06-10
  • 打赏
  • 举报
回复
这种小错误以后不要犯啦.. 记住就行了
shenchuan010 2010-06-10
  • 打赏
  • 举报
回复
问题解决了,谢谢大家了,就是order表名的错误,我是新手,第一次在上面发帖提问就得到各位大牛帮助,真的很感谢~
  • 打赏
  • 举报
回复
[Quote=引用楼主 shenchuan010 的回复:]
javax.servlet.ServletException: org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not fetch initial value; bad SQL grammar [select max(id) from order];
[/Quote]
明显提示是sql语法问题。注意sql中的关键字不要当类名
yantaiwsj1 2010-06-10
  • 打赏
  • 举报
回复
跟着学习了
aaaaaafd 2010-06-10
  • 打赏
  • 举报
回复
sql语句写错了,你仔细检查SQL语句!在你的sql语句排序的那个语句写错了!
order by ,,,,,,
javaisthis 2010-06-10
  • 打赏
  • 举报
回复
这种错误很明显了、、、、我也不多说了,顶一下
Java技术栈 2010-06-10
  • 打赏
  • 举报
回复
select max(id) from order

明显这句有错了

order是表名 改为 Order就行了 session.createQuery("select max(id) from Order")

这样也行 session.createSqlQuery("select max(id) from order");
vvhlj 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zh370232864 的回复:]
order 是sql中的关键字,而你的from后面没有接表名还是怎么的了?就直接接了个关键字,所以
[/Quote]

透彻,就是这样解决
feng8023 2010-06-10
  • 打赏
  • 举报
回复
我同意七楼的看法!
焙焙龙 2010-06-10
  • 打赏
  • 举报
回复
很明显,LZ使用了Hibernate的自动建表功能
而且涉及到订单,而订单又恰好是E文Order,更凑巧的是,在MYSQL里,order是关键字
然而这个兄弟在配置文件里这样写了
<class name="com.onlineshop.bean.Order" table="order">
导致Hibernate按照规则生成的SQL却是非法的!
改个表明就行了
建议LZ养成这样的习惯
表名以 “t_” 开头
视图名以“v_” 开头
索引以 “idx_”开头
序列以 “seq_”开头
逆风向前进 2010-06-10
  • 打赏
  • 举报
回复
order 是sql中的关键字,而你的from后面没有接表名还是怎么的了?就直接接了个关键字,所以
  • 打赏
  • 举报
回复
bad SQL grammar [select max(id) from order];
应该是你的SQL有错误
茶叶 2010-06-10
  • 打赏
  • 举报
回复
从上面可以看出是sql语法有故障
主键标识increment,整数都会有select max(id) from order
可以换成native不会产生select max(id) from order 这样的语句,看下什么情况。
qiheia 2010-06-10
  • 打赏
  • 举报
回复
你的sql 语句有错
select max(id) from order
order 是sql中的关键字,而你的from后面没有接表名还是怎么的了?就直接接了个关键字,所以

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧