8

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?
3
  • It seems USING is a reserved word in your DB. By the way it is better to name your properties as 'book' and 'reader' not 'idbook' and 'idreader' Commented Dec 13, 2013 at 21:27
  • 1
    Change your table name from USING to something else. Commented Dec 13, 2013 at 21:30
  • This is link to MySQL Reserved Words. Commented Dec 13, 2013 at 21:31

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The problem can be a result of conflicting MySQL versions for MySQL 5.x use org.hibernate.dialect.MySQL5Dialect then for anyother version use org.hibernate.dialect.MySQLDialect

Revised your hibernate.cfg.xml or .properties file

org.hibernate.dialect.MySQL5Dialect

1 Comment

See accepted answer. You will find a reason and a solution
0

As Amir pointed out, there are reserved words, not just in MySQL but in Hibernate too.

See:

Reserved Words, Hibernate / JPA

Comments

0

In my case, colum name in oracle and in class was different. After i made them same, it solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.