简体   繁体   English

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

[英]org.hibernate.exception.SQLGrammarException: could not execute statement

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.最后一个是绑定前两个,依赖一对多。

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?有什么建议么?

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

In MySQL USING is reserved word .在 MySQL 中USING保留字

So just rename the table by using @javax.persistence.Table annotation on your Using entity.因此,只需在Using实体上使用@javax.persistence.Table注释重命名表。 Something like类似的东西

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

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

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

In my case, colum name in oracle and in class was different.就我而言,oracle 和 class 中的列名称不同。 After i made them same, it solved.在我使它们相同之后,它解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.exception.SQLGrammarException:无法执行语句 - org.hibernate.exception.SQLGrammarException: could not execute statement org.springframework.web.util.NestedServletException:嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行语句 - org.springframework.web.util.NestedServletException: nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement 嵌套的异常是org.hibernate.exception.SQLGrammarException:无法执行查询 - nested exception is org.hibernate.exception.SQLGrammarException: could not execute query 线程“main”中的异常 javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: 无法执行语句 - Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement 如何解决 org.hibernate.exception.SQLGrammarException: 无法执行查询 - How to solve org.hibernate.exception.SQLGrammarException: could not execute query RuntimeException:org.hibernate.exception.SQLGrammarException:无法执行查询 - RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query org.hibernate.exception.SQLGrammarException:无法执行JDBC批更新 - org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update org.hibernate.exception.SQLGrammarException:无法执行 JDBC 批量更新 - org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update org.hibernate.exception.SQLGrammarException:无法再次执行查询 - org.hibernate.exception.SQLGrammarException: could not execute query again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM