Hibernate MappingException:未知实体

时间:2015-02-26 16:56:16

标签: 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.SessionImpl.getEntityPersister(SessionImpl.java:1439)
...

项目结构:

enter image description here

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

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

这是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
}

以下是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;
    }
}

这是我的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;
    }
}

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

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

使用import javax.persistence.Entity;代替import org.hibernate.annotations.Entity