我查看堆栈溢出以查找会导致此问题的不同原因和排列,但我不认为这种情况已被提出。
我试图在Maven上使用带有spring MVC的hibernate,并且由于某种原因,hibernate.cfg.xml的映射没有正确映射(我检查了我的路径,我认为它是正确的)。我收到以下错误:
org.hibernate.MappingException: Unknown entity: com.firstspringmvcproject.introcontroller.Entry
使用此映射:
<mapping class="com.firstspringmvcproject.introcontroller.Entry" />
在我的hibernate.cfg.xml文件中。
这是我的文件结构:
对于那些感兴趣的人 - 一些特定的文件:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">NAMEHERE</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="connection_pool_size">1</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.firstspringmvcproject.introcontroller.Entry" />
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FirstSpringMVCProject</display-name>
<servlet>
<!-- name of the servlet will add a -servlet postfix
unless otherwise specified. -->
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class> <!-- is the class for which to find the servlet -->
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
package com.firstspringmvcproject.introcontroller;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ENTRY_DATE")
public class Entry {
@Id @GeneratedValue
int entryInfo;
Date date;
public int getEntryInfo() {
return entryInfo;
}
public void setEntryInfo(int entryInfo) {
this.entryInfo = entryInfo;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
答案 0 :(得分:2)
请检查hibernate jar是否正确添加为pom.xml中的依赖项 您可以在Entry类中使用Hibernate注释(org.hibernate.annotations.Entity)而不是JPA注释(javax.persistence.Entity)来检查这一点。 如果未正确导入Jar,则无法成功构建项目。
答案 1 :(得分:0)
如果你有一个Entry.hbm.xml,那么尝试添加hbm.xml文件的路径:
<mapping class="com.firstspringmvcproject.introcontroller.Entry" />
<mapping resource="com/firstspringmvcproject/introcontroller/Entry.hbm.xml"/>