Java Hibernate:object.org.Hibernate.MappingException

Java Hibernate:object.org.Hibernate.MappingException,java,hibernate,Java,Hibernate,这是我面临的异常,它发生在我调用session.save时 object.org.hibernate.MappingException: Unknown entity: com.java.learn.pojo.Employee 这是我的java代码 try { Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder =

这是我面临的异常,它发生在我调用session.save时

object.org.hibernate.MappingException: Unknown entity:
  com.java.learn.pojo.Employee
这是我的java代码

try
{
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder builder =
        new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    SessionFactory factory = configuration.buildSessionFactory(builder.build());
    Session session = factory.openSession();
    Employee e = new Employee(1, "", "", 3);
    e.setFirstName("sparsh");
    session.save(e);
    System.out.println("in dao");
}
catch (Throwable ex)
{
    System.err.println("Failed to create sessionFactory object." + ex);
    factory.close();
    throw new ExceptionInInitializerError(ex);
}
这是我的
hibrnate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="hibernate.connection.driver_class">
            com.mysql.jdbc.Driver
        </property>

        <!-- Assume students is the database name -->
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/test
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            root1
        </property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <mapping class="com.java.learn.pojo.Employee"/>
    </session-factory>
</hibernate-configuration>

请提供帮助您可以使用
new Configuration().configure()
hbm.xml
文件加载实体映射

但是,由于您正在为
员工
实体类使用注释,因此您只需使用
注释配置
而不是
配置
,如下所示:

因此,替换下面的行:

Configuration configuration = new Configuration().configure();
 Configuration configuration = new AnnotationConfiguration().configure();

Configuration configuration = new Configuration().configure();
 Configuration configuration = new AnnotationConfiguration().configure();

问题:

这里的问题很明显,您没有正确映射与错误堆栈跟踪相关的
员工
实体:

object.org.hibernate.MappingException:未知实体:com.java.learn.pojo.Employee

问题是,
hibernate.cfg.xml
配置文件中的映射实体无法访问,因为您将文件放置在
web inf/classes
下,因此当
hibernate
尝试查找实体时,它将在
web inf/classes
下查找,但找不到

解决方案:


要解决此问题,只需将
hibernate.cfg.xml
文件放在
Java/src
文件夹下,而不是
web inf/classes
即可访问实体。

在web inf/classes中,您将
hibernate.cfg.xml
放在哪里了?我认为这会对您有所帮助。@sparsh610您应该将其放在您的资源中文件夹。或者,如果它是任何其他文件夹,那么您应该在配置中提供该路径。但是annoationconfiguration将被弃用。。。另外,您正在尝试在配置实例中插入sessionfactory。您可以查看这里:另外,我建议您首先尝试
AnnotationConfiguration
,检查它是否有效,然后可以深入了解可能存在的问题。。。