Tuesday, September 11, 2012

XYZ is not mapped. [FROM XYZ ]

Posted by Sayan Guharoy

XYZ is not mapped. [FROM XYZ ] 

context
-------------------------
This Exception may occur while trying to use direct table names while writing a Hibernate HQL query.
Brand is a table in an Oracle user schema.I want to list all the rows in the table using HQL query.

String SQL_QUERY ="from BRANDS";
  List list = session.createQuery("FROM BRANDS").list();
     
     for(Iterator it=list.iterator();it.hasNext();){
  
       Brands brandlist=(Brands)it.next();
    System.out.println("ID: " + brandlist.getBrandid());
    System.out.println("First Name: " + brandlist.getBrandName());
    
    }
Tentative Exception that we may get
--------------------------------------------------------------------
BRAND is not mapped[FROM BRAND]

Workaround
----------------------------
1.Check you have the correct mapping in hibernate.cfg.xml or any other hibernate configuration file.

2.If you are using annotation you must map your persistance class to your hibernateconfig file
 
   <!— mapping files in this case is the pojo class -->
            

3.Try to use full class-path that the persistence layer (POJO class) maps with this table and apply it in createQuery instead of Table Name

 
 List list = session.createQuery("FROM com.hibernate.Brands").list();
    
  for(Iterator it=list.iterator();it.hasNext();){
  
       Brands brandlist=(Brands)it.next();

    System.out.println("ID: " + brandlist.getBrandid());
    System.out.println("First Name: " + brandlist.getBrandName());
 }
4.Use Javax Entity package for annotation in your pojo class and not hibernate Entity package

 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

0 comments: