Hibernate 5 :- Org.Hibernate.Mappingexception: Unknown Entity

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 and ServiceRegistryBuilder

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



Leave a reply



Submit