Java Hibernate映射异常:未知实体

Java Hibernate映射异常:未知实体,java,hibernate,Java,Hibernate,我在运行hibernate应用程序时遇到此错误: Exception in thread "main" org.hibernate.MappingException: Unknown entity: sajjad.htlo.book.Books at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1095) at org.hibernate.internal

我在运行hibernate应用程序时遇到此错误:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: sajjad.htlo.book.Books
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1095)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1439)
...
项目结构:

我在hibernate.cfg中声明了它的映射类:

下面是BookDao.java:


我的代码怎么了?

根据堆栈跟踪,我猜您导入了错误的实体类。请使用JPA实体类


使用import javax.persistence.Entity;不是导入org.hibernate.annotations.Entity。

否,我在pojo书籍中使用javax.persistence.Entity。包含实体的包名是什么?并完全发布您的hibernate.cfg.xml。
...
<mapping class="sajjad.htlo.book.Books" />
...
@Entity
@Table(name = "Books")
public class Books implements Serializable {

    @Id
    @GeneratedValue
    private Integer id;
    private String title;
    private String author;
    private int isbn;
    private double cost;

    public Books() {
    }

    public Books(Integer id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
    }

    public Books(String title, String author) {
        this.title = title;
        this.author = author;
    }

// getters/setters
}
public class BookDao implements BookDaoInterface<Books, String> {

    private Session currentSession;
    private Transaction currentTransaction;

    public BookDao() {
    }

    public Session openCurrentSession() {
        currentSession = getSessionFactory().openSession();
        return currentSession;
    }

    public void closeCurrentSession() {
        getCurrentSession().close();
    }

    public Session openCurrentSessionwithTransaction() {
        currentSession = getSessionFactory().openSession();
        currentTransaction = currentSession.beginTransaction();
        return currentSession;
    }

    public void closeCurrentSessionwithTransaction() throws Exception {
        getCurrentTransaction().commit();
        getCurrentSession().close();
    }

    public static SessionFactory getSessionFactory() {
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties());
        SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
        return sessionFactory;
    }

    @Override
    public void persist(Books entity) {
        getCurrentSession().save(entity);
    }

    @Override
    public void update(Books entity) {
        getCurrentSession().update(entity);
    }

    @Override
    public Books findById(String id) {
        Books book = (Books) getCurrentSession().get(Books.class, id);
        return book;
    }

    @Override
    public void delete(Books entity) {
        getCurrentSession().delete(entity);
    }

    @Override
    public List<Books> findAll() {
        List<Books> books = (List<Books>) getCurrentSession().createQuery("from Books").list();
        return books;
    }

    @Override
    public void deleteAll() {
        List<Books> allBooks = findAll();
        for (Books eachBook : allBooks) {
            delete(eachBook);
        }
    }

    public Session getCurrentSession() {
        return currentSession;
    }

    public void setCurrentSession(Session currentSession) {
        this.currentSession = currentSession;
    }

    public Transaction getCurrentTransaction() {
        return currentTransaction;
    }

    public void setCurrentTransaction(Transaction currentTransaction) {
        this.currentTransaction = currentTransaction;
    }
}
public class BookService {

    private static BookDao bookDao;

    public BookService() {
        bookDao = new BookDao();
    }

    public void persist(Books entity) throws Exception {
        bookDao.openCurrentSessionwithTransaction();
        bookDao.persist(entity);
        bookDao.closeCurrentSessionwithTransaction();
    }

    public void update(Books entity) throws Exception {
        bookDao.openCurrentSessionwithTransaction();
        bookDao.update(entity);
        bookDao.closeCurrentSessionwithTransaction();
    }

    public Books findBID(String id) {
        bookDao.openCurrentSession();
        Books foundBook = bookDao.findById(id);
        bookDao.closeCurrentSession();
        return foundBook;
    }

    public void delete(String id) throws Exception {
        bookDao.openCurrentSessionwithTransaction();
        Books book = bookDao.findById(id);
        bookDao.delete(book);
        bookDao.closeCurrentSessionwithTransaction();
    }

    public List<Books> findAll() {
        bookDao.openCurrentSession();
        List<Books> books = bookDao.findAll();
        bookDao.closeCurrentSession();
        return books;
    }

    public void deleteAll() throws Exception {
        bookDao.openCurrentSessionwithTransaction();
        bookDao.deleteAll();
        bookDao.closeCurrentSessionwithTransaction();
    }

    public BookDao bookDao() {
        return bookDao;
    }
}