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

catch23

我在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上的所有表均已创建,但为空。一切都还好。有什么建议?

  • 如何解决这个麻烦?
阿米尔·帕萨扎德(Amir Pashazadeh)

在MySQL中,USING保留字

因此,只需通过@javax.persistence.TableUsing实体使用批注来重命名表即可就像是

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法执行JDBC批量更新

来自分类Dev

无法执行保存操作:org.hibernate.exception.GenericJDBCException:无法执行语句

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法准备语句

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法准备语句-Spring ROO

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法准备语句-Spring ROO

来自分类Dev

嵌套的异常是org.hibernate.exception.GenericJDBCException:无法执行语句

来自分类Dev

SQLGrammarException:无法执行语句(PostgreSQL + Hibernate)

来自分类Dev

org.hibernate.exception.DataException:无法执行查询

来自分类Dev

org.hibernate.exception.GenericJDBCException:无法执行查询

来自分类Dev

org.hibernate.exception.GenericJDBCException:无法在休眠中执行语句

来自分类Dev

javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法使用heroku PostgreSQL执行查询

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法插入我正在

来自分类Dev

错误 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet

来自分类Dev

无法执行JDBC批量更新:线程“主要” org.hibernate.exception.ConstraintViolationException中的异常:

来自分类Dev

嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet Hibernate + SpringMVC

来自分类Dev

org.hibernate.exception.ConstraintViolationException:无法插入

来自分类Dev

请求处理失败;嵌套的异常是org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

来自分类Dev

请求处理失败;嵌套的异常是org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

来自分类Dev

org.hibernate.exception.SQLGrammarException:表'XXX'不存在

来自分类Dev

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

来自分类Dev

org.hibernate.exception.SQLGrammarException:无效的列名称

来自分类Dev

org.hibernate.exception.SQLGrammarException:无法获取下一个序列值

来自分类Dev

休眠注释错误(org.hibernate.exception.SQLGrammarException:无法插入:[com.hib.ex.entity.Node])

来自分类Dev

HTTP状态500-org.hibernate.exception.SQLGrammarException:无法打开连接

来自分类Dev

嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet,Spring4,Hibernate4

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    org.hibernate.exception.SQLGrammarException:无法执行JDBC批量更新

  6. 6

    无法执行保存操作:org.hibernate.exception.GenericJDBCException:无法执行语句

  7. 7

    org.hibernate.exception.SQLGrammarException:无法准备语句

  8. 8

    org.hibernate.exception.SQLGrammarException:无法准备语句-Spring ROO

  9. 9

    org.hibernate.exception.SQLGrammarException:无法准备语句-Spring ROO

  10. 10

    嵌套的异常是org.hibernate.exception.GenericJDBCException:无法执行语句

  11. 11

    SQLGrammarException:无法执行语句(PostgreSQL + Hibernate)

  12. 12

    org.hibernate.exception.DataException:无法执行查询

  13. 13

    org.hibernate.exception.GenericJDBCException:无法执行查询

  14. 14

    org.hibernate.exception.GenericJDBCException:无法在休眠中执行语句

  15. 15

    javax.persistence.PersistenceException:org.hibernate.exception.SQLGrammarException:无法使用heroku PostgreSQL执行查询

  16. 16

    org.hibernate.exception.SQLGrammarException:无法插入我正在

  17. 17

    错误 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet

  18. 18

    无法执行JDBC批量更新:线程“主要” org.hibernate.exception.ConstraintViolationException中的异常:

  19. 19

    嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet Hibernate + SpringMVC

  20. 20

    org.hibernate.exception.ConstraintViolationException:无法插入

  21. 21

    请求处理失败;嵌套的异常是org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

  22. 22

    请求处理失败;嵌套的异常是org.hibernate.exception.ConstraintViolationException:无法执行JDBC批处理更新

  23. 23

    org.hibernate.exception.SQLGrammarException:表'XXX'不存在

  24. 24

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

  25. 25

    org.hibernate.exception.SQLGrammarException:无效的列名称

  26. 26

    org.hibernate.exception.SQLGrammarException:无法获取下一个序列值

  27. 27

    休眠注释错误(org.hibernate.exception.SQLGrammarException:无法插入:[com.hib.ex.entity.Node])

  28. 28

    HTTP状态500-org.hibernate.exception.SQLGrammarException:无法打开连接

  29. 29

    嵌套的异常是org.hibernate.exception.SQLGrammarException:无法提取ResultSet,Spring4,Hibernate4

热门标签

归档