-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: org.hibernate.MappingException: Unknown entity
PostPosted: Mon Sep 12, 2011 11:23 am 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I'm using spring 3, Hibernate 3.6 and I have the following Exception which occurs when addEntity(...) is exuted

Quote:
sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity("Stock");


Code:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.MappingException: Unknown entity: Stock
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

org.hibernate.MappingException: Unknown entity: Stock
   org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
   org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:335)
   org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:376)
   org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:355)
   org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:171)
   org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:87)
   org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67)
   org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:166)
   org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:160)
   org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
   org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
   com.stockdomain.domain.StockDAOImpl.getRecord(StockDAOImpl.java:31)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   java.lang.reflect.Method.invoke(Method.java:597)
   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   $Proxy13.getRecord(Unknown Source)
   com.stockdomain.bo.StockBO.getStock(StockBO.java:187)
   com.stockdomain.bo.StockBO.getStockHibernate(StockBO.java:113)
   com.stockdomain.controller.StockController.submitForm(StockController.java:96)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   java.lang.reflect.Method.invoke(Method.java:597)
   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Spring Context XML :

Code:
<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-3.0.xsd">         
         
   <context:component-scan base-package="com.stockdomain">
       <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>         

    <!-- dataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName"><value>org.postgresql.Driver</value></property>
      <property name="url"><value>jdbc:postgresql://localhost/db_finance</value></property>
      <property name="username"><value>xxxx</value></property>
      <property name="password"><value>xxxx</value></property>
    </bean> 
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
           <list>
                <value>com.stockdomain.domain.Stock</value>
           </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
         </props>
      </property>
   </bean>
   
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
               <ref bean="sessionFactory" />
        </property>
    </bean>
   
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>            
   
</beans>


The DAO

Code:
package com.stockdomain.domain;

import java.util.HashMap;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.stockdomain.util.DbUtilities;

@Transactional
@Repository
public class StockDAOImpl implements StockDAO {

   @Autowired
   SessionFactory sessionFactory;

   public void saveStock(Stock stock) {
      // getHibernateTemplate().saveOrUpdate(stock);
   }

   @SuppressWarnings("unchecked")
   public List<Stock> getRecord() {
      String sql = "SELECT * FROM stock ";
      Query query = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity("Stock");
      List<Stock> list = query.list();
      return list;
   }
}


The Entity class

Code:
package com.stockdomain.domain;

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

import org.hibernate.annotations.Entity;



@Entity
@Table(name = "stock")
public class Stock {

   @Id
   @GeneratedValue
   private int id;
   @Column
   private String code;
   @Column
   private String name;
   @Column
   private String isin;
   @Column
   private String exchangeCode;
   @Column
   private String countryCode;
   @Column
   private String subSectorCode;
   @Column
   private String active;
   @Column
   private String liquid;

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

   public int getId() {
      return id;
   }

   public String getCode() {
      return code;
   }

   public void setCode(String code) {
      this.code = code;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getIsin() {
      return isin;
   }

   public void setIsin(String isin) {
      this.isin = isin;
   }

   public String getExchangeCode() {
      return exchangeCode;
   }

   public void setExchangeCode(String exchangeCode) {
      this.exchangeCode = exchangeCode;
   }

   public String getCountryCode() {
      return countryCode;
   }

   public void setCountryCode(String countryCode) {
      this.countryCode = countryCode;
   }

   public String getSubSectorCode() {
      return subSectorCode;
   }

   public void setSubSectorCode(String subSectorCode) {
      this.subSectorCode = subSectorCode;
   }

   public String getActive() {
      return active;
   }

   public void setActive(String active) {
      this.active = active;
   }

   public String getLiquid() {
      return liquid;
   }

   public void setLiquid(String liquid) {
      this.liquid = liquid;
   }

}


Top
 Profile  
 
 Post subject: Re: org.hibernate.MappingException: Unknown entity
PostPosted: Wed Sep 21, 2011 4:44 am 
Newbie

Joined: Wed Sep 21, 2011 4:39 am
Posts: 1
Hi fv967,
Have you found an answer to this issue?
We are encountering the same error from Hibernate - we have one part of the application where it can happily load and save an object but another place in the application where it cannot. Im suspecting that it is down to a hibernate Session issue.

thanks,
Stuart.


Top
 Profile  
 
 Post subject: Re: org.hibernate.MappingException: Unknown entity
PostPosted: Wed Sep 21, 2011 6:58 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Can you try using addEntity(Stock.class)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.