org.hibernate.hql.ast.QuerySyntaxException: table not mapped

burning picture burning · May 20, 2014 · Viewed 31.5k times · Source

I know many people have asked this question but I just started Java so I couldn't figure out still.

So here is my problem: I am writing RESTful webservices using Javarestlet. Here is the snippet of my DAO file.

try {

            session.beginTransaction();

            String query = "select number from   blockedcli";
            @SuppressWarnings("rawtypes")
            List list = session.createQuery(query).list(); //.setString("sId", businessId)


            logger.error("*******************list*****************************************");
            logger.error(list);
            logger.error("*******************listend*****************************************");


            @SuppressWarnings("rawtypes")
            Iterator iterator = list.iterator();

            while (iterator.hasNext()) {

                blockedcli = (BlockedCli) iterator.next();
            }
            session.getTransaction().commit();
        } 

Correspondingly my entity class looks like .

@Entity
@Table(name = "blockedcli")
public class BlockedCli implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="idBlockedCLI", nullable = false, unique=true)
    private Integer idBlockedCLI;

    @Column(name = "number",nullable = false, length=45)
    private String number;

    @Column(name = "type", nullable = false)
    private Integer type;
    .

I have placed a BlackListedN.hbm.xml file in my config directory with following text .

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="tecd.persistence.entity.BlockedCli" table="blockedcli">    
    <id name="idBlockedCLI" type="long" unsaved-value="null">
        <column name="idBlockedCLI" not-null="true"/>
        <generator class="identity"/>
    </id>
        <property name="number">
            <column name="number" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

As I want to display only number .

And Here is the DB table .

+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+
| idBlockedCLI | number       | status | type | createdDT           | updatedDT           | BusinessDirectory_idBusinessDirectory |
+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+
|            1 | 919845611234 |      1 |    1 | 2014-03-24 13:31:20 | 2014-03-24 13:31:20 | 1                                     |
+--------------+--------------+--------+------+---------------------+---------------------+---------------------------------------+

But when I ran this Everytime it says

org.hibernate.hql.ast.QuerySyntaxException: blockedcli is not mapped [select number from   blockedcli]

Please help me out to rectify this issue .

This is my first Java program so I am not sure what else is required to elaborate this however do let me know if anything else required.

Answer

Aleksander Blomsk&#248;ld picture Aleksander Blomskøld · May 20, 2014

When using HQL, you need to refer to correctly capitalized entity names/properties, not the table names/column names. Simply change your query to select number from BlockedCli, and it should work.