java 未知实体类型异常:无法找到persister是什么?

nfg76nw0  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(104)

Can u help me? Idk what Im doing wrong...
//Employee.class

package org.example.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic
    @Column(name = "name", nullable = true, length = 10)
    private String name;
    @Basic
    @Column(name = "surname", nullable = true, length = 15)
    private String surname;
    @Basic
    @Column(name = "office_id", nullable = true)
    private Integer officeId;

}

hibernate.cfg.xml

<?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="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">vfr4vgy7</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.show_sql">true</property>

    <mapping class="org.example.entity.Employee"/>

      <!-- DB schema will be updated if needed -->
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

main

package org.example;

import org.example.entity.Employee;
import org.hibernate.Session;

public class Main {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Employee employee = new Employee();
        employee.setName("New");
        employee.setSurname("Hero");
        employee.setOfficeId(1);
        employee.setId(12);
        session.beginTransaction();
        session.save(employee);
        session.getTransaction().commit();
        session.close();
        HibernateUtil.close();

    }
}

HibernateUtil.class

package org.example;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * Created by Ариорх on 27.05.2017.
 */
public class HibernateUtil {
    private static SessionFactory sessionFactory = null;

    static {
        Configuration cfg = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                .applySettings(cfg.getProperties());
        sessionFactory = cfg.buildSessionFactory(builder.build());
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void close(){
        getSessionFactory().close();
    }
}

So, when I start the project Im catching (Exception in thread "main" org.hibernate.UnknownEntityTypeException: Unable to locate persister: org.example.entity.Employee at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.getEntityDescriptor(MappingMetamodelImpl.java:416) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1492) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:197) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:182) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:28) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:78) at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:649) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:641) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:636) at org.example.Main.main(Main.java:16).
Using PostgresSQL. IntellIJ Ultimate version. Maybe problem in HibernateUtil.class?

iqjalb3h

iqjalb3h1#

要从hibernate.cfg.xml加载配置,必须在StandardServiceRegistryBuilder上调用configure()

final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .configure() // configures settings from hibernate.cfg.xml
            .build();
    try {
        sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
    }
    catch (Exception e) {
        // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
        // so destroy it manually.
        StandardServiceRegistryBuilder.destroy( registry );
    }

相关问题