Java org.hibernate.exception.SQLGrammarException: 无法执行语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20576236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:52:39  来源:igfitidea点击:

org.hibernate.exception.SQLGrammarException: could not execute statement

javahibernateexception

提问by nazar_art

I tried easy program at Hibernate and caught bunch of exception.

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

I couldn't figure out what exactly is wrong.

我无法弄清楚到底出了什么问题。

I have three classes - Book, Reader and Using. The last is binding first two with dependency one to many.

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

Here is my main():

这是我的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");
    }
}

Here is exception message:

这是异常消息:

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)

snippet of hiberante.cfg.xml:

片段hiberante.cfg.xml

<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>

All tables at DB are created but is empty. All looks ok. Any suggestions?

DB 中的所有表都已创建但为空。一切看起来都不错。有什么建议?

  • How to solve this trouble?
  • 如何解决这个烦恼?

采纳答案by Amir Pashazadeh

In MySQL USINGis reserved word.

在 MySQL 中USING保留字

So just rename the table by using @javax.persistence.Tableannotation on your Usingentity. Something like

因此,只需@javax.persistence.Table在您的Using实体上使用注释来重命名表。就像是

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

I assumed you have a table for USING, but you mentioned that it is a one-to-many relationship, so you can omit the table, and model it using just a single foreign key in Readertable.

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

By the way hibernate does not force you to create a new entity for many-to-many join tables (which don't have any more attribute but the foreign keys). But I believe it is a good practice to have an entity for that relationship, cause most of the times some attributes will be defined for the relation in future.

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

回答by Damir Olejar

As Amir pointed out, there are reserved words, not just in MySQL but in Hibernate too.

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

See:

看:

Reserved Words, Hibernate / JPA

保留字,休眠/JPA

回答by chinyavadav

The problem can be a result of conflicting MySQL versions for MySQL 5.x use org.hibernate.dialect.MySQL5Dialect then for anyother version use org.hibernate.dialect.MySQLDialect

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

Revised your hibernate.cfg.xml or .properties file

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

org.hibernate.dialect.MySQL5Dialect

org.hibernate.dialect.MySQL5Dialect