0
votes

I am using Spring 3 and Hibernate 4 in my application. While running I am getting below exception.

Note : I searched on net i made it sure that import of my Entity annotation is correct and component-scan is also well in pace in xml file.

01:51:15,184 ERROR [STDERR] org.hibernate.MappingException: Unknown entity: com.voya.sid.entity.ApplicationEntity 01:51:15,184 ERROR [STDERR] at 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:15,184 ERROR [STDERR] at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206) 01:51: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.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683) 01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675) 01:51:15,184 ERROR [STDERR] at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)


BELOW ARE MY APPLICATION 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...
  }

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>

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

Please help.

4
provide the more detail about your hibernate.cfg.xml file. - Keval Trivedi
@Keval : Added in question - VJS
There is no mapping of your ApplicationEntity class in xml file. - Keval Trivedi
Is the table name valid and present in DB?? You can set hbm2ddl.auto=create if you want to create the schema - Pratik Shelar
<value>ApplicationEntity(with package name)</value> add this code in your root-context.xml file - Keval Trivedi

4 Answers

0
votes

It may helps you :

Add the below code in your root-context.xml file.

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

Also try adding the below in root-context.xml file.

<context:annotation-config />
0
votes

You need to do something like this to achieve a context scan for annotations to be picked up.

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

Add the following to your 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>