Announcement Announcement Module
Collapse
No announcement yet.
table is not mapped exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • table is not mapped exception

    Hi,
    I got a quite curious problem. I wrote a DAOTest to check if my DAO implementation works fine.
    Code:
    Code:
    /**
    *	23.05.2005
    *	11:52:05
    * 	johannes.hiemer
    *  	SystemTypeDAOTest.java
    */
    package itecon.dao.hibernate.test;
    
    import itecon.bo.dao.ISystemtypeDAO;
    import itecon.bo.model.Systemtype;
    import junit.awtui.TestRunner;
    
    import org.apache.log4j.Logger;
    
    /**
     * @author johannes.hiemer
     *
     * ITecon Dell Call System 
     */
    public class SystemTypeDAOTest extends BaseDAOTestCase {
    
        private Systemtype stype;
        private ISystemtypeDAO stypeDAO;
        
        protected void setUp() throws Exception {
            log = Logger.getLogger(SystemErrorDAOTest.class.getName());
            stypeDAO = (ISystemtypeDAO) ctx.getBean("systemTypeDAO");
        }
        
        protected void tearDown() throws Exception {
            stypeDAO = null;
        }
        
        public static void main(String args []) {
            TestRunner.run(SystemTypeDAOTest.class);
        }
        
        public Systemtype createSystemType() {
            stype = new Systemtype();
            stype.setStprice("40,0");
            stype.setSttype("Server");
            return stype;
        }
        
        public void testAddAndRemoveSystemType() {
            createSystemType();
            log.info(stype);
    
            stypeDAO.saveSystemtype(stype);
    
            assertTrue("primary key assigned", stype.getStid() != null);
            log.info(stype);
            assertTrue(stype.getSttype() != null);
            
            stypeDAO.getSystemtype(stype.getStid());
            
            assertTrue("primary key not assigned",  stype.getStid() != null);
        }
    }
    When I run my DAOTest it works. Adding one line results in a crash, which origin is not understandable for me.
    Code:
            log.info(stypeDAO.getSystemtypes());
    When I add this line behind:
    stypeDAO.saveSystemtype(stype);

    I a get an exception: systemtype is not mapped.

    Stacktrace:
    Code:
    org.springframework.orm.hibernate3.HibernateQueryException: systemtype is not mapped. [from systemtype]; nested exception is org.hibernate.hql.ast.QuerySyntaxError: systemtype is not mapped. [from systemtype]
    org.hibernate.hql.ast.QuerySyntaxError: systemtype is not mapped. [from systemtype]
    	at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:196)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:130)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
    	at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:425)
    	at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:880)
    	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:830)
    	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
    	at org.springframework.orm.hibernate3.HibernateTemplate$29.doInHibernate(HibernateTemplate.java:749)
    	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:311)
    	at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:740)
    	at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:732)
    	at itecon.bo.dao.hibernate.SystemtypeDAOHibernate.getSystemtypes(SystemtypeDAOHibernate.java:42)
    	at itecon.dao.hibernate.test.SystemTypeDAOTest.testAddAndRemoveSystemType(SystemTypeDAOTest.java:50)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at junit.framework.TestCase.runTest(TestCase.java:154)
    	at junit.framework.TestCase.runBare(TestCase.java:127)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Caused by:  systemtype is not mapped.
    	at org.hibernate.hql.ast.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:85)
    	at org.hibernate.hql.ast.FromElementFactory.addFromElement(FromElementFactory.java:77)
    	at org.hibernate.hql.ast.FromClause.addFromElement(FromClause.java:67)
    	at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:217)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:2830)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2719)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:513)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:371)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:201)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:151)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:189)
    	... 27 more
    But what I am wondering is, that the insert in the line before (stypeDAO.saveSystemtype(stype) works, but the complete select from the database doesn't. I checked if the Database got the inserted values. What's wrong with my implementation/DAOTest?

    I hope you can help me.

    Thanks a lot

    Johannes

  • #2
    Can you post your mappings.

    Comment


    • #3
      Hi,
      here it is:
      Code:
      <?xml version="1.0"?>
      <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http&#58;//hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
      <!-- 
              Auto-generated mapping file from
              the hibernate.org cfg2hbm engine
      -->
        <class name="itecon.bo.model.Systemtype" table="SYSTEMTYPE" schema="DELL">
          <id name="Stid" type="java.lang.Long">
            <column name="STID" scale="22" precision="0" not-null="true" unique="true" sql-type="NUMBER" />
            <generator class="native" />
          </id>
          <property name="Sttype" type="java.lang.String">
            <column name="STTYPE" scale="100" precision="0" not-null="true" sql-type="VARCHAR2" />
          </property>
          <property name="Stprice" type="java.lang.String">
            <column name="STPRICE" scale="100" precision="0" not-null="true" sql-type="VARCHAR2" />
          </property>
        </class>
      </hibernate-mapping>
      Thanks a lot for your help.

      Bye Johannes

      Comment


      • #4
        Hi,
        to provide some help for people who got stuck in the same
        situation, remember that "from Table" is case sensitive.

        That was my fault.

        Bye Johannes

        Comment


        • #5
          table is not mapped exception

          Hi all,

          I was faced with the same problem too. I have a table named PRODUCTS and class Product. I got the feeling it is because of the limitation of Hibernate in translating table name to the right class. It does not even able to translate the table alias to the class. For example: the piece of the SQL statement "from PRODUCTS as Product" to the Product class.

          If you are using HibernateTemplate from org.springframework.orm.hibernate3, then I guess you have no choice to sync the table and class names.

          The solutions that I found are as follows:
          Solution A: use the Session.createCriteria()

          Code:
          List products = sess.createCriteria(Product.class)
          .add( Restrictions.in( "name", new String[] { "Lamp", "Table", "Chair" } ) )
          .list();
          I think this solution is good when you need to query data only from a single table.

          Solution B: use the Session.createSQLQuery()

          Code:
          List products = getSession().createSQLQuery("SELECT * FROM Products").addEntity(Product.class).list();
          Cheers,
          JR

          Comment


          • #6
            I think you miss the points of hibernate... Hibernate uses OBJECTS so if you have a class Product mapped to a table PRODUCTS your query is

            Code:
            from Product
            In HQL you work with objects not with tables!

            So in your case your select hql for all the stuff would read

            Code:
            from Systemtype
            instead of

            Code:
            from systemtype

            Comment

            Working...
            X