Home > other >  How to fix the "Unknown entity" error in Hibernate 5?
How to fix the "Unknown entity" error in Hibernate 5?

Time:09-21

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC



        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">123456</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQL9Dialect</property>
    </session-factory>
</hibernate-configuration>

SessionFactory

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil
{
    private HibernateUtil()
        {}
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory  = new Configuration().configure().buildSessionFactory();


        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed."   ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

HumanBeing.java

@Entity
@Table(appliesTo = "HumanBeing")
public class HumanBeing
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id; 
    @Column(name = "name")
    private String name; 
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn (name="coordinates_id")
    private Coordinates coordinates; 
    @Column(name = "creationDate")
    private java.time.LocalDate creationDate;
    @Column(name = "realHero")
    private boolean realHero;
    @Column(name = "hasToothpick")
    private boolean hasToothpick;
    @Column(name = "impactSpeed")
    private Float impactSpeed; 
    @Column(name = "weaponType")
    @Enumerated(EnumType.STRING)
    private WeaponType weaponType; 
    @Column(name = "mood")
    @Enumerated(EnumType.STRING)
    private Mood mood; 
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn (name="car_id")
    private Car car; 

    ...
}

code

Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx1 = session.beginTransaction();
        session.save(new HumanBeing());
        tx1.commit();
        session.close();

And Error:org.hibernate.MappingException: Unknown entity: HumanBeing

P.S: It looks like your post is mostly code; please add some more details.

It looks like your post is mostly code; please add some more details.

It looks like your post is mostly code; please add some more details.

CodePudding user response:

Error caused by your Entity is not mapped with hibernate.cfg.xml

Add this in your hibernate.cfg.xml file in <session-factory>:

<mapping class="packagename.HumanBeing"/>
  • Related