Answer the question
In order to leave comments, you need to log in
What could cause a MappingException to be thrown after upgrading Hibernate from 4.3.11.Final to 5.1.0.Final?
What are the significant changes in 5.1.0.Final that might cause hibernate.xfg.xml mappings to not work? With version 4.3.11.Final everything works fine.
When I try to save an object to the database, I get an exception:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: test.hibernate.Book
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
at test.hibernate.Main.main(Main.java:26)
// Configure Hibernate and build session factory
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// Save a book object
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Book book = new Book();
book.setName("qwerty");
session.save(book);
transaction.commit();
session.close();
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test_db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="test.hibernate.Book" />
</session-factory>
</hibernate-configuration>
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue
private int id;
@Column(nullable = false, length = 100, unique = true)
private String name;
// Getters & setters...
}
Answer the question
In order to leave comments, you need to log in
To initialize the SessionFactory you need this:
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question