【问题标题】:org.hibernate.exception.SQLGrammarException: could not execute statementorg.hibernate.exception.SQLGrammarException:无法执行语句
【发布时间】:2014-01-01 19:06:51
【问题描述】:

我在 Hibernate 中尝试了简单的程序并捕获了一堆异常。

我不知道到底出了什么问题。

我有三个课程 - Book、Reader 和 Using。最后是绑定前两个,依赖一对多。

这是我的main()

public class Appl {
    public static void main(String[] args) {
        Book book = new Book();
        book.setTitle("book01155");
        //
        Reader reader = new Reader();
        reader.setName("reader2");
        //
        Using using = new Using();
        using.setIdBook(book);
        using.setIdReader(reader);
        //
        List<Book> elements = new ArrayList<Book>();
        //
        Session session = null;     
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(book);
            session.save(reader);
            session.save(using);
            elements = session.createCriteria(Book.class).list();
            session.getTransaction().commit();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        for (Book b : elements) {
            System.out.println("book: id=" + b.getIdBook() + " Title="
                    + b.getTitle());
        }
        System.out.println("\nThe END.\n");
    }
}

这里是异常信息:

ERROR: 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 'USING (IDBOOK, IDREADER) values (2, 2)' at line 1
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

hiberante.cfg.xml的sn-p:

<hibernate-configuration>
    <session-factory>
        <property name="eclipse.connection.profile">097Hibernate</property>

        <property name="connection.url">jdbc:mysql://localhost/_097_Library</property>
        <property name="connection.username">root</property>
        <property name="connection.password">secret</property>

        <!-- property name="hbm2ddl.auto">create</property -->
        <property name="hbm2ddl.auto">update</property>

        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <mapping class="com.softserve.edu.Book" />
        <mapping class="com.softserve.edu.Reader" />
        <mapping class="com.softserve.edu.Using" />
    </session-factory>
</hibernate-configuration>

数据库中的所有表都已创建但为空。 一切看起来都很好。有什么建议吗?

  • 如何解决这个问题?

【问题讨论】:

  • 看来 USING 是您数据库中的保留字。顺便说一句,最好将您的属性命名为“book”和“reader”而不是“idbook”和“idreader”
  • 将您的表名从 USING 更改为其他名称。
  • 这是MySQL Reserved Words的链接。

标签: java hibernate exception


【解决方案1】:

在 MySQL 中 USINGreserved word

因此,只需在 Using 实体上使用 @javax.persistence.Table 注释来重命名表。 类似的东西

@Entity
@Table(name = "TB_USING")
public class Using {
    ...
}

我假设您有一个USING 的表,但您提到它是一对多关系,因此您可以省略该表,并仅使用Reader 表中的单个外键对其进行建模。

顺便说一句,hibernate 不会强迫您为多对多连接表(除了外键之外没有任何属性)创建新实体。但我认为为该关系创建一个实体是一种很好的做法,因为大多数情况下,将来会为该关系定义一些属性。

【讨论】:

    【解决方案2】:

    正如 Amir 指出的那样,不仅在 MySQL 中,在 Hibernate 中也有保留字。

    见:

    Reserved Words, Hibernate / JPA

    【讨论】:

      【解决方案3】:

      问题可能是由于 MySQL 版本冲突造成的 对于 MySQL 5.x 使用 org.hibernate.dialect.MySQL5Dialect 然后对于任何其他版本使用 org.hibernate.dialect.MySQLDialect

      修改了您的 hibernate.cfg.xml 或 .properties 文件

      org.hibernate.dialect.MySQL5Dialect

      【讨论】:

      • 查看接受的答案。你会找到原因和解决方案
      猜你喜欢
      • 2013-06-28
      • 1970-01-01
      • 2022-11-09
      • 2016-10-15
      • 2017-04-30
      • 2021-08-10
      • 1970-01-01
      • 2023-03-03
      • 2011-10-17
      相关资源
      最近更新 更多