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

发布于 2021-02-01 12:24:36

我在Hibernate尝试了简单的程序,并发现了一堆异常。

我不知道到底是什么问题。

我有三个课程-书籍,阅读器和使用。最后一个是将前两个绑定为一对多。

这是我的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

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

DB上的所有表均已创建,但为空。一切都还好。有什么建议么?

  • 如何解决这个麻烦?
关注者
0
被浏览
97
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

    在MySQL中, USING保留字

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

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

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

    顺便说一下,hibernate不会强迫您为多对多联接表(除了外键之外没有其他属性)创建新实体。但是我认为,为该关系创建实体是一种好习惯,因为在大多数情况下,将来会为该关系定义一些属性。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看