naz*_*art 5 java hibernate exception
我在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不会强制你为多对多连接表创建一个新实体(除了外键之外没有任何其他属性).但我认为为这种关系建立一个实体是一个好习惯,因为大多数时候将来会为这种关系定义一些属性.
| 归档时间: | 
 | 
| 查看次数: | 61179 次 | 
| 最近记录: |