Java Hibernate:object.org.Hibernate.MappingException
这是我面临的异常,它发生在我调用session.save时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 =
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
,检查它是否有效,然后可以深入了解可能存在的问题。。。