【发布时间】:2014-01-01 19:06:51
【问题描述】:
我在 Hibernate 中尝试了简单的程序并捕获了一堆异常。
我不知道到底出了什么问题。
我有三个课程 - Book、Reader 和 Using。最后是绑定前两个,依赖一对多。
这是我的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的sn-p:
<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>
数据库中的所有表都已创建但为空。 一切看起来都很好。有什么建议吗?
- 如何解决这个问题?
【问题讨论】:
-
看来 USING 是您数据库中的保留字。顺便说一句,最好将您的属性命名为“book”和“reader”而不是“idbook”和“idreader”
-
将您的表名从 USING 更改为其他名称。
-
这是MySQL Reserved Words的链接。