org.hibernate.MappingException:未知实体:bk.Book

时间:2016-01-22 12:49:13

标签: java hibernate

我正在尝试构建一个简单的Java EE应用程序。当我运行应用程序时,我收到以下错误:

  

org.hibernate.MappingException:   未知实体:bk.Book

在对stackoverflow和其他论坛进行彻底研究后,我无法弄清楚如何在我的日食中修复此错误。下面,我已经编写了所有必要的文件和配置。如果有人在这里可以启发我并解决这个问题,感激不尽。我正在使用hibernate 5.

Book

  package bk;
    public class Book {

    private int isbn;
    private String title;

    public Book(){

    }

    public int getIsbn() {
        return isbn;
    }

    public void setIsbn(int isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

            public void setTitle(String title) {
        this.title = title;
    }

     }       

Book.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name = "bk.Book" table = "Book">

        <id name = "isbn" type = "int" column = "book_isbn"/>

        <property name = "title" type = "string" column = "book_title"/>

    </class>

</hibernate-mapping>

hibernate.cfg.xml

  <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
    <!--  Database connection settings -->

    <property name = "hibernate.connection.driver.class">com.mysql.jdbc.Driver
    </property>

    <property name = "hibernate.connection.url">jdbc:mysql://localhost:3306/esd
    </property>

    <property name = "hibernate.connection.username">root
    </property>

    <property name = "hibernate.connection.password">root
    </property>

    <!--  Drop and re-create the database schema on startup -->

    <property name = "hibernate.hbm2ddl.auto">create</property>

    <!--  Echo all executed SQL to console -->

    <property name = "show_sql">true</property>

    </session-factory>
</hibernate-configuration>

BookManager

   package bk;
    import org.hibernate.*;
    import org.hibernate.cfg.Configuration;

    public class BookManager {

    private SessionFactory sessionFactory;

    public BookManager() throws Exception{


    //A sessionFactory is set up once for an application

    sessionFactory = new Configuration()
            .configure().buildSessionFactory(); // configures settings from hibernate.cfg.xml

}


public int addBook(int isbn, String title){
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Book b = new Book();
    b.setIsbn(isbn);
    b.setTitle(title);
    int BookID = (Integer)session.save(b);
    tx.commit();
session.close();
return BookID;
}

  }

1 个答案:

答案 0 :(得分:0)

尝试在hibernate.cfg.xml文件中添加:

  <hibernate-configuration>
   <session-factory> 
   ....
   <mapping class="bk.Book"/>     <!--  add this -->
   <mapping resource="bk/Book/Book.hbm.xml"/>   <!--  and this -->
   </session-factory> 
   </hibernate-configuration>