org.hibernate.exception.SQLGrammarException:无法执行语句 [英] org.hibernate.exception.SQLGrammarException: could not execute statement
问题描述
我找不出什么错误。
我有三个类 - Book,Reader和Using。最后一个是绑定前两个依赖关系的一对多。
这是我的 main()
:
public class Appl {
表。
public static void main(String [] args){
Book book = new Book( );
book.setTitle(book01155);
//
读者阅读器= new Reader();
reader.setName(reader2);
//
使用using = new Using();
using.setIdBook(book);
using.setIdReader(reader);
//
列表< Book> elements = new ArrayList< Book>();
//
会话会话= 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();
(Book b:elements){
System.out.println(book:id =+ b.getIdBook()+Title =
+ b.getTitle());
}
System.out.println(\\\
The END.\\\
);
$ / code $ / pre
以下是异常消息:
错误:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行中使用'USING(IDBOOK,IDREADER)值(2,2)'附近的正确语法
线程mainorg.hibernate.exception中的异常。 SQLGrammarException:在org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java在org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert无法执行的语句
(SQLExceptionTypeDelegate.java:82)
: 49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
hiberante.cfg.xml :
<冬眠-结构>
< 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>秘密< / property>
<! - property name =hbm2ddl.auto> create< / property - >
< property name =hbm2ddl.auto>更新< / 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 是 mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-0.htmlrel =noreferrer>保留字。
因此,只需在
使用
实体上使用@ javax.persistence.Table
注释重命名表。
类似于
@Entity
@Table(name =TB_USING)
public类使用{
...
}
我假设你有一张桌子对于
USING
,但您提到它是一对多关系,因此您可以省略该表,并使用<$ c中的一个外键对其进行建模$ c> Reader
顺便提一下,hibernate不会强制您为多对多连接表创建一个新实体除了外键之外,它没有任何其他属性)。但我认为有一个关系实体是一个很好的习惯,因为大多数情况下,将来会为关系定义一些属性。
I tried easy program at Hibernate and caught bunch of exception.
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()
: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
:<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.
So just rename the table by using @javax.persistence.Table
annotation on your Using
entity.
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.
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). 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.
这篇关于org.hibernate.exception.SQLGrammarException:无法执行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!