How to fix the Unknown entity error in Hibernate 5?
Error caused by Entity
is not mapped with hibernate.cfg.xml
Add this in your hibernate.cfg.xml
file in <session-factory>
:
<mapping class="packagename.HumanBeing"/>
org.hibernate.MappingException: Unknown entity when trying to create a new record
You need to change your code as example you referred is for Hibernate 4.3
and you are using Hibernate 5.x
. Refer here for explanation.
Change your code as per hibernate 5.x docs
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
Hibernate 5 annotation org.hibernate.MappingException: Unknown entity
You are mixing 2 approaches to configure your session factory:
- the deprecated way using
Configuration
class - the new way using
MetadataSources
andServiceRegistryBuilder
Try to only use the new way with something like this :
@Bean
public SessionFactory createSessionFactory() throws ClassNotFoundException {
Properties properties = new Properties();
properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto","update");
StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
standardServiceRegistryBuilder.applySettings(properties);
standardServiceRegistryBuilder.applySetting(Environment.DATASOURCE,dataSource());
MetadataSources metadataSources = new MetadataSources(standardServiceRegistryBuilder.build());
metadataSources.addAnnotatedClass(Customer.class);
return metadataSources.getMetadataBuilder().build().buildSessionFactory();
}
Hibernate 5: org.hibernate.MappingException: Unknown entity
UPDATED: In Hibernate 5.0.x, configuration with standard service registry is deprecated
Instead you should bootstrap it with Metadata:
In your HibernateUtil
class, you should add
private static SessionFactory buildSessionFactory() {
try {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure( "hibernate.cfg.xml" )
.build();
Metadata metadata = new MetadataSources( standardRegistry )
.getMetadataBuilder()
.build();
return metadata.getSessionFactoryBuilder().build();
}
And as a side note, change the ID declaration of your Message Class to
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long id;
So hibernate can properly use mySql's auto increment, otherwise it will throw an exception.
org.hibernate.MappingException: Unknown entity Employee
The problem with this
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory(serviceRegistry);
You try to do the configuration twice. This is not neccessary
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
You can't use this way to configure Hibernate 5. Just do this for the configuration
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
How to configure Hibernate 5
Getting org.hibernate.MappingException: Unknown entity:
Your session factory configuration is incorrect for Hibernate 5. If you use Hibernate 5, you can create a session factory by this way
sessionFactory = new Configuration().configure().buildSessionFactory();
In the file User.hbm.xml
you shouldn't use namespace
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
Use
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"
instead
Exception in thread main org.hibernate.MappingException: Unknown entity: bitronix.examples.hibernate.entities.User
The problem, obviously, with configuration code. You create Configuration
twice.
Just do
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
I know, that it is deprecated in Hibernate 4. But for Hibernate 5 it is a good way. This Hibernate 4 approach will not work with Hibernate 5.
Hibernate 5 :- org.hibernate.MappingException: Unknown entity
Related Topics
Change Background Color of Single Specific Menu Items of Navigationview
Android/Java -- Post Simple Text to Facebook Wall
Ionic Build Android Error When Downloading Gradle
How to Find Java Heap Size and Memory Used (Linux)
Moving from Jdk 1.7 to Jdk 1.8 on Ubuntu
Post to Jersey Rest Service Getting Error 415 Unsupported Media Type
Convert Existing Eclipse Project to Maven Project
Execute Another Jar in a Java Program
Good Way to Encapsulate Integer.Parseint()
How to Convert an Array to a Set in Java
How to Wait for a Number of Threads to Complete
Emulate Annotation Inheritance for Interfaces and Methods with Aspectj
Why Can't We Have Static Method in a (Non-Static) Inner Class (Pre-Java 16)
Java: How to Call Non Static Method from Main Method
Best Way to Compare Dates in Android
How Are Integers Internally Represented at a Bit Level in Java