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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With