2014-07-11 177 views
0

我在我的應用程序中使用Spring 3和Hibernate 4。在運行時,我得到了異常。異常:org.hibernate.MappingException:未知實體

注:我在網上搜索,我確定導入我的實體註釋是正確的,並且組件掃描在xml文件中也很好。

01:51:15184 ERROR [STDERR] org.hibernate.MappingException:未知實體:com.voya.sid.entity.ApplicationEntity 01:51:15184 ERROR [STDERR]在org.hibernate.internal。 SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146) 01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1358) 01:51:15,184 ERROR [STDERR] at org。 hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116) 01:51:15184 ERROR [STDERR]在org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206) 01:5 1:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 01:51:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(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 ERROR [STDERR] at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683) 01:51:15,184 ERROR [STDERR] .internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675) 01:51:15,184 ERROR [STD ERR]在org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)


下面是我應用CLASEES:

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... 
    } 

根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> 

請幫助。

+0

提供有關hibernate.cfg.xml文件的更多細節。 –

+0

@Keval:新增問題 – VJS

+0

在XML文件中沒有您的ApplicationEntity類的映射。 –

回答

0

它可以幫助你:

添加下面的代碼在你的根context.xml的文件。

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

也可以嘗試在根的context.xml文件中添加以下。

<context:annotation-config /> 
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" 
/> 
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> 
相關問題