【问题标题】:org.hibernate.MappingException: Unknown entity: from xxxorg.hibernate.MappingException:未知实体:来自 xxx
【发布时间】:2015-08-05 21:38:27
【问题描述】:

我正在尝试解决我发布的问题 here

但是,现在我遇到了另一个问题。 :(

org.hibernate.MappingException: Unknown entity: from EmpTest
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1095)
    at org.hibernate.internal.SessionImpl.getOuterJoinLoadable(SessionImpl.java:1754)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1659)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:380)
    at com.model.EmpModelImp.getAll(EmpModelImp.java:84)
    at com.model.EmpServiceImpl.getAll(EmpServiceImpl.java:47)
    at com.controller.EmpControl.getAll(EmpControl.java:34)

EmpTest.java

package com.entity;
// Generated May 23, 2015 10:43:49 PM by Hibernate Tools 4.3.1


import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * EmpTest generated by hbm2java
 */
@Entity
@Table(name="EMP_TEST"
    ,schema="SCOTT"
)
public class EmpTest  implements java.io.Serializable {


     private BigDecimal id;
     private String ename;
     private Date dob;
     private BigDecimal sal;

    public EmpTest() {
    }

    public EmpTest(BigDecimal id, String ename, Date dob, BigDecimal sal) {
       this.id = id;
       this.ename = ename;
       this.dob = dob;
       this.sal = sal;
    }

     @Id 


    @Column(name="ID", unique=true, nullable=false, precision=22, scale=0)
    public BigDecimal getId() {
        return this.id;
    }

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


    @Column(name="ENAME", nullable=false, length=50)
    public String getEname() {
        return this.ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="DOB", nullable=false, length=7)
    public Date getDob() {
        return this.dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }


    @Column(name="SAL", nullable=false, precision=5)
    public BigDecimal getSal() {
        return this.sal;
    }

    public void setSal(BigDecimal sal) {
        this.sal = sal;
    }




}

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.url}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" /-->

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

</beans>

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    <context:component-scan base-package="com.controller"/>
    <mvc:annotation-driven />


    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

</beans>

hibernate.cfg.xml

<?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>
    <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="hibernate.connection.username">scott</property>
    <property name="hibernate.connection.password">tiger</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <mapping resource="com/entity/EmpTest.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

HibernateUtil.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.util;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;

/**
 * Hibernate Utility class with a convenient method to get Session Factory
 * object.
 */
public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

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

【问题讨论】:

  • 嗯,EmpTest 不是一个公认的实体。它是用 javax.persistence.Entity 注释的吗?它是否列在persistence.xml 文件中?发布必要的信息。向我们展示您认为不应有此例外的原因。
  • 我不关心你之前的帖子。如果这是一个不同的问题,那么所有必要的信息都应该在这个问题中。如果是同一个问题,那么这个问题应该被删除。
  • &lt;!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --&gt;。您还没有在 spring 配置中声明任何与休眠相关的东西。我们不知道您是如何创建 SessionFactory 的。
  • @JBNizet 我对休眠很陌生。这些文件是由 NetBeans 创建的。
  • 好的。然后退后一步,忘记你的 IDE,阅读 Spring 参考文档,了解如何将 Hibernate 或 JPA 集成到 Spring 应用程序中。

标签: hibernate mapping entity


【解决方案1】:

我在模型中更改了以下行,这对我有用。

emp=session.createCriteria("from EmpTest").list();

emp=session.createCriteria(EmpTest.class).list();

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2011-11-28
    • 2014-04-16
    • 2012-12-04
    • 2014-08-05
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多