简体   繁体   中英

Hibernate Mapping Exception : Unknown Entity

I am mapping a simple POJO to a database table in SQL server using xml file mapping. I am getting Hibernate Mapping Exception: Unknown Entity
These are my code files:
Entry point class:

public class Program {

    public static void main(String[] args) {
        System.out.println("Helo World");

        Session session = HibernateUtilities.getSessionFactory().openSession();
        session.beginTransaction();

        User user = new User();
        user.setId(1);
        user.setName("Ajay");
        user.setGoal(100);
        user.setTotal(0);

        session.save(user);

        session.getTransaction().commit();
        session.close();
        HibernateUtilities.getSessionFactory().close();
        System.out.println("session closed");
    }

}

this is HibernateUtilities class:

public class HibernateUtilities {
        private static SessionFactory sessionFactory;
        private static ServiceRegistry serviceRegistry;
        static {
            try {
                Configuration configuration = new Configuration().configure();
                serviceRegistry = new StandardServiceRegistryBuilder()
                        .applySettings(configuration.getProperties()).build();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (HibernateException e) {
                System.out.println(e);
            }
        }

        public static SessionFactory getSessionFactory(){
            return sessionFactory;
        }

}

this is POJO User:

public class User {
    private int id;
    private String name;
    private int total;
    private int goal;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getGoal() {
        return goal;
    }

    public void setGoal(int goal) {
        this.goal = goal;
    }

    public void setId(int id) {
        this.id = id;
    }
}

This is Mapping file:

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- Generated Sep 14, 2016 11:58:02 AM by Hibernate Tools 3.4.0.CR1 -->
    <hibernate-mapping package="com.example.hibernate">
     <class entity-name="User" name="com.example.hibernate.User" table="USERS">
      <id name="id" type="int">
       <column name="ID"/>
       <generator class="increment"/>
      </id>
      <property generated="never" lazy="false" name="name" type="java.lang.String">
       <column name="NAME"/>
      </property>
      <property generated="never" lazy="false" name="total" type="int">
       <column name="TOTAL"/>
      </property>
      <property generated="never" lazy="false" name="goal" type="int">
       <column name="GOAL"/>
      </property>
     </class>
    </hibernate-mapping>

this is configuration file:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory name="">
      <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
      <property name="hibernate.connection.password">ajay1994</property>
      <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=protein_tracker</property>
      <property name="hibernate.connection.username">test</property>
      <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
      <mapping class="com.example.hibernate.User" resource="com/example/hibernate/User.hbm.xml"/>
     </session-factory>
    </hibernate-configuration>

Stack Trace for Error:

Helo World
Sep 14, 2016 2:58:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.2.Final}
Sep 14, 2016 2:58:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 14, 2016 2:58:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 14, 2016 2:58:58 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Sep 14, 2016 2:58:58 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Sep 14, 2016 2:58:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Sep 14, 2016 2:58:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=protein_tracker]
Sep 14, 2016 2:58:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=test, password=****}
Sep 14, 2016 2:58:58 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Sep 14, 2016 2:58:58 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Sep 14, 2016 2:58:58 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.example.hibernate.User
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1604)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    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:673)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
    at com.example.hibernate.Program.main(Program.java:18)

open hibernate.cfg.xml file. press ctrl and hold it and click on mapping class " com.example.hibernate.User " check whether you are navigated to User class or not.

For anyone refereing this question today.Looks like he is using xml for mapping and in cfg.xml he used class and resource.If you are using hbm.xml for mapping cfg.xml should contain

  <mapping resource="com/example/hibernate/User.hbm.xml"/>

and if you are using annotation for mapping cfg.xml should contain the below mappping

  <mapping class="com.example.hibernate.User"/>

and you cannot use both in mapping.

For me it was the fact that at some point hibernate started using the annotations from jakarta.persistence.* as opposed to java.persistence.*

WOOHOO HOPE IT DOESN'T TAKE HOURS OF HEAD POUNDING TO FIGURE THAT OUT!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM