Эта проблема была описана здесь много раз, но я не видел эффективных советов, как ее устранить.
Вот пара идентичных проблем без ответов:
- org.hibernate.MappingException: неизвестный объект
- Ошибка гибернации - org.hibernate.MappingException: неизвестный объект:
Я еще раз опишу проблему.
Простое учебное приложение.
hibernate-core-5.3.1.Final
спящий-аннотаций-3.5.6-Final
hibernate-jpa-2.0-api-1.0.0.Final
...
Основной класс
package main;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Main {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
builder.applySettings(conf.getProperties());
ServiceRegistry registry = builder.build();
SessionFactory sessionFactory = conf.buildSessionFactory(registry);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Employee employee = new Employee();
employee.setFirstName("John");
employee.setLastName("Doe");
employee.setSalary(1000000);
session.save(employee);
transaction.commit();
sessionFactory.close();
}
}
класс сотрудников
package main;
import javax.persistence.*;
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "SALARY")
private int salary;
public Employee() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
hibernate.cfg.xml file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/world</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.verifyServerCertificate">false</property>
<property name="hibernate.connection.useSSL">true</property>
<property name="hibernate.connection.requireSSL">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.format_sql">true</property>
<mapping class="main.Employee"/>
</session-factory>
</hibernate-configuration>
Исключение
...
INFO: HHH10001001: Connection properties: {user=root, password=****, verifyServerCertificate=false, useSSL=true, requireSSL=false}
Jun 26, 2018 10:54:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jun 26, 2018 10:54:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Jun 26, 2018 10:54:38 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Jun 26, 2018 10:54:39 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@5026735c'
Exception in thread "main" org.hibernate.MappingException: Unknown entity: main.Employee
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:684)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1692)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:122)
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:709)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:696)
at main.Main.main(Main.java:26)
@Entity
присутствует
Отображение class="main.Employee"
присутствует.
Прошу помочь установить причину проблемы.
Использование configuration.addAnnotatedClass(Employee.class)
и создание файла hbm не предлагайте, пожалуйста.
Эти варианты я пробовал, они работают.
Меня особенно интересует случай, описанный выше.