Issue
I encountered a problem with hibernate.MappingException: Unknown entity. I have annotations and database, but for some reason does not recognize entity. I have tomacat 10.0.23. Here is the code:
public static void save () {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
Company company = new Company();
company.setId(1L);
company.setCompanyName("madela");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction transaction = session.beginTransaction();
session.save(company);
transaction.commit();
session.close();
}
Entity:
import jakarta.persistence.*;
@Entity
@Table(name = "company", schema = "public", catalog = "madela_crm")
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "company_name")
private String companyName;
public Company() {
}
public Company(Long id, String companyName) {
this.id = id;
this.companyName = companyName;
}
public String getCompanyName() {
return companyName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
and servlet where i trust this
@WebServlet(name="HelloWorld", value = "/hello")
public class HelloWorld extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
AbstractTest.saveTest();
}
hibernate.cfg.xml :
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/madela_crm</property>
<property name="connection.username">crm</property>
<property name="connection.password">crm</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL10Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.example.entity.Company"/>
</session-factory>
</hibernate-configuration>
and this give to me :
org.hibernate.MappingException: Unknown entity: com.example.entity.Company org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:710) org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1648) org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114) org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:194) org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:179) org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:75) org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107) org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:676) org.hibernate.internal.SessionImpl.save(SessionImpl.java:669) org.hibernate.internal.SessionImpl.save(SessionImpl.java:664) com.example.service.impl.AbstractTest.saveTest(AbstractTest.java:43) com.example.servlet.HelloWorld.doPost(HelloWorld.java:34) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:694) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:777) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
public class Company implements Serializable
it didn't help
Solution
The problem was because there were a lot of jakarta dependencies, after removing them I updated hibernate to 6.1.2.Final and changed the dialect to org.hibernate.dialect.PostgreSQLDialect
Answered By - MishaBucha
Answer Checked By - Mildred Charles (JavaFixing Admin)