我正在使用Spring mvc Web应用程序,需要在其中使用Hibernate创建城镇。以下是我的城镇模型。
@Entity @Table(name="town") public class TownModel { @Id @Column(name="townid") @GeneratedValue private Integer townId; @Column(name="name") private String townName; @Column(name="desc") private String townDesc; @ManyToOne(optional = true) @JoinColumn(name="districtid") private DistrictModel districtModel; }
我还有一个名为区的实体。镇是区的一部分,一个区可以有多个镇。以下是我的区域模型:
@Entity @Table(name="district") public class DistrictModel { @Id @Column(name="districtid") @GeneratedValue private Integer districtId; @Column(name="name") private String name; @Column(name="desc") private String description; @OneToMany(mappedBy = "districtModel", fetch = FetchType.EAGER) @Fetch(value = FetchMode.SUBSELECT) private List<TownModel> townModelList; }
我正在使用以下Hibernate代码保存城镇:
Session session = sessionFactory.getCurrentSession(); session.save(townModel);
但是显示错误:
21:22:08,104 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--127.0.0.1-9090-1) SQL Error: 1064, SQLState: 42000 21:22:08,112 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http--127.0.0.1-9090-1) 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 'desc, name) values (2, 'Test town desc.', 'Test town')' at line 1 com.mysql.jdbc.exceptions.jdbc4.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 'desc, name) values (2, 'Test town desc.', 'Test town')' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_45] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_45] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_45] at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_45] at com.mysql.jdbc.Util.handleNewInstance(Util.java:377) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.Util.getInstance(Util.java:360) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077) ~[com.mysql.jdbc_5.1.5.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062) ~[com.mysql.jdbc_5.1.5.jar:na] at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493) ~[na:na] at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186) ~[hibernate-core-4.2.16.Final.jar:4.2.16.Final] ... 64 common frames omitted
错误消息指出:
您的SQL语法有误;检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在第1行的“ desc,名称”值(2,“ Test town desc。”,“ Test town”)附近使用
问题是在生成的查询中,由于使用了desc它,因为它是MySQL中的保留字。
desc
可能的解决方案:
description
name
@Column(name="`desc`")
IMO虽然选项3是一种快速而肮脏的解决方案,但我发现选项1是将来使用数据库的最佳解决方案。