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

新手上路,请多包涵

我在 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 中的所有表都已创建但为空。一切看起来都很好。有什么建议么?

  • 如何解决这个麻烦?

原文由 catch23 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 684
2 个回答

在 MySQL 中 USING保留字

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

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

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

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

原文由 Amir Pashazadeh 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

org.hibernate.dialect.MySQL5方言

原文由 chinyavadav 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题