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

  • Why the object is not mapped?

    By using Spring+Hibernate, I just want to get all the "product" objects from "products" table in database, but I got "products is not mapped" error when executing "select id, description, price from products" query.

    My "Product" object class is like following:
    Code:
    package springapp.domain;
    
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "products")
    public class Product implements Serializable {
    
       @Id
       @Column(name = "id")
       private int id;
        
       @Column(name = "description")
        private String description;
    	
        @Column(name = "price")
        private Double price;
        
        public void setId(int i) {
            id = i;
        }
    
        public int getId() {
            return id;
        }
    
        public String getDescription() {
            return description;
        }
        
        public void setDescription(String description) {
            this.description = description;
        }
        
        public Double getPrice() {
            return price;
        }
        
        public void setPrice(Double price) {
            this.price = price;
        }
        
        public String toString() {
            StringBuffer buffer = new StringBuffer();
            buffer.append("Description: " + description + ";");
            buffer.append("Price: " + price);
            return buffer.toString();
        }
    }
    My productDaoImplementation class is :
    Code:
    package springapp.repository;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
    import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    import springapp.domain.Product;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.*;
    
    import util.HibernateUtil;
    
    public class JdbcProductDao extends SimpleJdbcDaoSupport implements ProductDao {
    
        /** Logger for this class and subclasses */
        protected final Log logger = LogFactory.getLog(getClass());
    
        private SessionFactory sessionFactory;
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        
        public List<Product> getProductList() {
            logger.info("Getting products!");
       
             /*Here, I get all the product objects from "products" table*/
            List<Product> products =this.sessionFactory.getCurrentSession()
            					.createQuery("select id, description, price from products")
            					.list();
            return products;
        }
    
        public void saveProduct(Product prod) {
            logger.info("Saving product: " + prod.getDescription());
    
            /*----------------Using hibernate---------------*/ 
            
            /** Getting the Session Factory and session */
          
            Session sess = this.sessionFactory.getCurrentSession();
            /** Starting the Transaction */
            Transaction tx = sess.beginTransaction();
    
            
            String prodUpdate = "update products set description = :description, price = :price where id = :id";
            int i=sess.createQuery(prodUpdate)
            		.setString("description", prod.getDescription())
            		.setDouble("price", prod.getPrice())
            		.setInteger("id", prod.getId())
            		.executeUpdate()
            		
            		;
            /** Commiting the changes */
            tx.commit();
            System.out.println("Record Inserted");  
    
        }
     
    }
    Bean definition in applicationContext.xml is:
    Code:
    <bean id="productDao" class="springapp.repository.JdbcProductDao">
            <property name="dataSource" ref="dataSource"/>
            <property name="sessionFactory" ref="sessionFactory"/>
            
        </bean>
    The error msg is :
    Code:
    INFO springapp.repository.JdbcProductDao - Getting products!
    ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/springapp].[springapp] - Servlet.service() for servlet springapp threw exception
    org.hibernate.hql.ast.QuerySyntaxException: products is not mapped [select id, description, price from products]
    	at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
    	at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    	at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
    	at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:277)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
    	at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
    So, why my products is not mapped??
    Last edited by mellon; Feb 26th, 2009, 03:17 PM.

  • #2
    You do not use sql statements in createQuery method of session, instead you use HQL.

    You can read more on HQL using this link.

    Vincent

    Comment


    • #3
      Originally posted by vincent_ray21 View Post
      You do not use sql statements in createQuery method of session, instead you use HQL.

      You can read more on HQL using this link.

      Vincent
      Hi Vincent, I read the link and modify the query statement in my JdbcProductDao class in following ways:
      change to:
      Code:
      createQuery("from products")
      or to:
      Code:
      createQuery("select pro.id, pro.description, pro.price from products as pro")
      or to;
      Code:
      createQuery("select pro.id, pro.description, pro.price from Product as pro")
      But all of the above try still cause the same problem: products is not mapped...
      (As you see in my Product.java class, my object class is "Product", table name is "products")
      Last edited by mellon; Feb 27th, 2009, 02:42 PM.

      Comment


      • #4
        Originally posted by vnvnvn2000
        Sweet! thanks much for the answer, much appreciated..

        - -, I still got the same problem...anything you can share?

        Comment


        • #5
          You can try this:

          Code:
          createQuery("from springapp.domain.Product");
          with condition:

          Code:
          createQuery("from springapp.domain.Product as product where product.id=:productId");
          Vincent

          Comment


          • #6
            Have you mapped the class in your SessionFactory bean?
            e.g.
            Code:
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            	<property name="dataSource" ref="dataSource" />
            		<property name="annotatedClasses">
            		<list>
            			<value>springapp.domain.Product</value>
            		</list>
            	</property>
            	<property name="hibernateProperties">
            		<props>
            			<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            			<prop key="hibernate.hbm2ddl.auto">create</prop>
            			<prop key="hibernate.show_sql">true</prop>
            		</props>
            	</property>
            </bean>

            Comment

            Working...
            X