异常:org.hibernate.MappingException:未知实体

时间:2014-07-11 06:12:48

标签: java spring hibernate hibernate-mapping

我在我的应用程序中使用Spring 3和Hibernate 4。在跑步的时候,我会遇到异常。

注意:我在网上搜索了我确保导入我的实体注释是正确的,并且组件扫描在xml文件中也很快。

  

01:51:15,184 ERROR [STDERR] org.hibernate.MappingException:未知实体:com.voya.sid.entity.ApplicationEntity   01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146)   01:51:15,184 org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1358)中的错误[STDERR]   01:51:15,184 ERROR [STDERR] at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)   01:51:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)   01:51:15,184 org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)的ERROR [STDERR]   01:51:15,184 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient的ERROR [STDERR](DefaultSaveOrUpdateEventListener.java:191)   01:51:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)   01:51:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)   01:51:15,184 org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)的错误[STDERR]   01:51:15,184 org.hibernate.internal.SessionImpl.save中的错误[STDERR](SessionImpl.java:675)   01:51:15,184 org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)的错误[STDERR]


以下是我的申请表:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="SID_APP_INSTANCE")
public class ApplicationEntity {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="APP_INSTANCE_KEY")
    // other code
 }

@Component
@Service("ApplicationService")
public class ApplicationServiceImpl implements ApplicationService{

public ApplicationDao getApplicationDao() {
    return applicationDao;
}
public void setApplicationDao(ApplicationDao applicationDao) {
    this.applicationDao = applicationDao;
}

@Autowired
private ApplicationDao applicationDao;

   // some methods..

  }

----------------------------------------------- ---------------------------------

@Component
@Repository("ApplicationDao")
 public class ApplicationDaoImpl implements ApplicationDao {

@Autowired
   private SessionFactory sessionFactory;


public SessionFactory getSessionFactory() {
    return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

public void addApplication(ApplicationEntity app) {
    try{
        System.out.println("Inside ApplicationDaoImpl..My Life : My Session :> "+ this.sessionFactory.getCurrentSession());
        this.sessionFactory.getCurrentSession().save(app);
    }catch (Exception e) {
        System.out.println("Exception in saving..");
        e.printStackTrace();
    }

}
  // other methods...
  }

root-context.xml:

    <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
 <tx:annotation-driven/>

   <context:component-scan base-package="com" />
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- <property name="dataSource" ref="dataSource" /> -->
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>

       </bean>

添加Hibernate.cfg.xml

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@host:50004:abc</property>
  <property name="hibernate.connection.username">user</property>
  <property name="hibernate.connection.password">pass</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">abc</property>
  <property name="show_sql">true</property>
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>
  <property name="hibernate.validator.apply_to_ddl">false</property> 
  <property name="hibernate.validator.autoregister_listeners">false</property>
</session-factory>
</hibernate-configuration>

请帮忙。

4 个答案:

答案 0 :(得分:0)

它可以帮助你:

root-context.xml 文件中添加以下代码。

<value>ApplicationEntity(with package name)</value> 

答案 1 :(得分:0)

另请尝试在 root-context.xml 文件中添加以下内容。

<context:annotation-config />

答案 2 :(得分:0)

你需要做这样的事情来实现上传注释的上下文扫描。

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="WEB-INF/classes/hibernate.cfg.xml"
p:packagesToScan="com.example.model"
/>

答案 3 :(得分:0)

将以下内容添加到xml中:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>your.package.name.here</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>