报错如下:
org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
手贱,建了个表叫Check,里面有个列名叫leave ,后来查mysql的关键字发现是两个活生生的关键字啊。 这个关键字查询不会报错,调用update就报错了!
解决方法Hibernate配置取别名:
<?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="org.model.Check" table="tcheck" catalog="hotel" dynamic-insert="true" dynamic-update="true">再把数据库里面相对应表的名字和列名改了就好了,还有,提醒:如果你这里Hibernate配置错了,会报404错误<id name="checkid" type="java.lang.Integer"> <column name="checkid" /> <generator class="identity" /> </id> <property name="sid" type="java.lang.Integer"> <column name="sid" /> </property> <property name="checkin" type="java.util.Date"> <column name="checkin" length="10" /> </property> <property name="checkout" type="java.util.Date"> <column name="checkout" length="10" /> </property> <property name="checked" type="java.lang.Integer"> <column name="checked" /> </property> <property name="late" type="java.lang.Integer"> <column name="late" /> </property> <property name="leave" type="java.lang.Integer"> <column name="fuckleave" /> //这里取别名 </property> <property name="day" type="java.util.Date"> <column name="day" length="10" /> </property> <property name="about" type="java.lang.String"> <column name="about" /> </property> <many-to-one name="staff" column="staff_sid" class="org.model.Staff" cascade="none" fetch="select" lazy="false" not-found="ignore"> </many-to-one> </class> </hibernate-mapping>
文章详细介绍了在使用Hibernate进行数据库操作时遇到列名冲突导致的InvalidDataAccessResourceUsageException和SQLGrammarException错误,并提供了解决方案。通过修改Hibernate配置文件中的列名别名和数据库表结构调整,成功解决了问题。文章还提醒开发者注意避免关键字作为列名,以防止潜在的错误。
4471

被折叠的 条评论
为什么被折叠?



