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上的所有表均已创建,但为空。一切都还好。有什么建议么?
- 如何解决这个麻烦?
-
在MySQL中, USING 是保留字。
因此,只需使用实体
@javax.persistence.Table
上的注释来重命名表即可Using
。就像是@Entity @Table(name = "TB_USING") public class Using { ... }
我假设您有一个用于的表
USING
,但是您提到它是一对多关系,因此您可以省略该表,并仅使用Reader
表中的单个外键对其进行建模。顺便说一下,hibernate不会强迫您为多对多联接表(除了外键之外没有其他属性)创建新实体。但是我认为,为该关系创建实体是一种好习惯,因为在大多数情况下,将来会为该关系定义一些属性。
-
org.hibernate.exception.SQLGrammarException:无法插入[com.sample.Person]
2021-02-01 关注 0 浏览81 1答案
-
org.hibernate.exception.GenericJDBCException:无法执行查询
2021-02-01 关注 0 浏览107 1答案
-
获取org.hibernate.exception.JDBCConnectionException:即使通过JNDI也无法执行查询
2021-02-01 关注 0 浏览86 1答案
-
SQLGrammarException:无法执行查询
2021-02-01 关注 0 浏览126 1答案
-
为什么Hibernate抛出org.hibernate.exception.LockAcquisitionException?
2021-02-01 关注 0 浏览107 1答案
-
没有打印语句就无法执行代码
2021-01-30 关注 0 浏览88 1答案
-
无法执行目标org.codehaus.mojo:tomcat-maven-plugin :: run
2021-02-01 关注 0 浏览145 1答案
-
无法执行目标org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy(default-cli)
2021-01-30 关注 0 浏览105 1答案
-
Maven无法执行目标org.apache.maven.plugins:maven-clean-plugin:2.5:clean无法删除access_log
2021-02-01 关注 0 浏览104 1答案
-
Hibernate和@JoinFormula:org.hibernate.mapping.Formula无法转换为org.hibernate.mapping.Column
2021-02-01 关注 0 浏览101 1答案