2015-02-26 5 views
0

Я получил эту ошибку при запуске моего спящего режима приложения:Hibernate MappingException: Неизвестный объект

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

Что случилось с моим кодом?

ответ

0

Согласно вашей трассировке стека, я предполагаю, что вы импортировали неправильный Entity класс. Используйте класс сущности JPA.

Использовать import javax.persistence.Entity; вместо import org.hibernate.annotations.Entity.

+0

Нет, я использую 'javax.persistence.Entity' в книгах pojo. –

+0

Каково имя пакета, в котором содержится ваше лицо? И полностью разместите свой hibernate.cfg.xml. – RE350

+0

Проблема решена, спасибо. –

Смежные вопросы