【问题标题】:Hibernate: Can't insert data into tableHibernate:无法将数据插入表中
【发布时间】:2015-04-20 11:00:46
【问题描述】:

我无法将数据插入到我的所有表中,选择可以正常工作。使用的用户有插入数据的权限,我也试过手动插入数据。

hibernate.cfg、hibernate.reveng、hbm.xml等所有配置文件均由NetBeans生成。

Event.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 20, 2015 10:05:17 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="at.ac.univie.salome.domain.Event" table="event" catalog="salomeapp" optimistic-lock="version">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="title" type="string">
            <column name="title" not-null="true" />
        </property>
        <property name="desc" type="string">
            <column name="desc" length="1024" />
        </property>
        <set name="partitions" table="partition" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="event_id" not-null="true" />
            </key>
            <one-to-many class="at.ac.univie.salome.domain.Partition" />
        </set>
    </class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/salomeapp?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">******</property>
    <property name="hibernate.connection.password">******</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="at/ac/univie/salome/domain/Partition.hbm.xml"/>
    <mapping resource="at/ac/univie/salome/domain/Chunk.hbm.xml"/>
    <mapping resource="at/ac/univie/salome/domain/Event.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

例外:

Caused by: 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) values ('Test', 'test')' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
    ... 102 more

我正在使用NetbeansGlassfish 4Hibernate 4.3MySQL 5.6.23

【问题讨论】:

  • 看起来您使用保留字 desc 作为列名,除非它用反引号 ` ` 包裹,否则会产生此语法错误,最好将列名更改为 description 或反引号它在查询中。
  • 插入记录的JAVA代码在哪里?
  • @AbhikChakraborty,backtic it in the query 是什么意思
  • 将 desc 列名重命名为其他任何内容..
  • 检查这个答案,查询与使用保留字的地方相同stackoverflow.com/questions/27502081/…

标签: java mysql hibernate


【解决方案1】:

我认为您正在尝试在实体中使用保留的列名。 尝试更改您的列名。

看看:http://developer.mimer.se/validator/sql-reserved-words.tml 看看保留了哪些字

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多