简体   繁体   中英

Hibernate MappingException : Unknown entity

I have got this error while running my hibernate app:

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)
...

Project Structure:

在此输入图像描述

I had declared it's mapping class in hibernate.cfg :

...
<mapping class="sajjad.htlo.book.Books" />
...

This is Books POJO:

@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
}

Here is BookDao.java :

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;
    }
}

And here is my BookService.java :

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;
    }
}

What is wrong with my code?

As per your stack trace,I guess you have imported wrong Entity class.Use JPA entity class.

Use import javax.persistence.Entity; instead of import org.hibernate.annotations.Entity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM