如何解决线程中的异常" main" org.hibernate.MappingException:未知实体:

时间:2018-02-04 22:38:23

标签: hibernate

我在线程" main"中得到一个例外。 org.hibernate.MappingException:未知实体:net.codejava.hibernate.Book。

代码

public class MainApp {
public static void main(String[] args) {

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

    Session session = sessionFactory.openSession();
    session.beginTransaction();

    // Check database version
    String sql = "select version()";

    String result = (String) session.createNativeQuery(sql).getSingleResult();
    System.out.println(result);

    session.getTransaction().commit();
    session.close();

    Book book = new Book();
    book.setTitle("Effective Java");
    book.setAuthor("Joshua Bloch");
    book.setPrice(32.59f);

    Session session1 = sessionFactory.openSession();
    session1.beginTransaction();

    session1.save(book);

    session1.getTransaction().commit();
    session1.close();


   // HibernateUtil.shutdown();
  }
}

1 个答案:

答案 0 :(得分:0)

解决方案1:

this.cdr.detectchanges

解决方案2:

sessionFactory.setPackagesToScan(
  new String[] { "net.codejava.hibernate" });