Ishan
Ishan

Reputation: 62

How to access database table not mapped as entity class

I want to access all records of my database table in spring + hibernate. I am using MySql as a database. The scenario is I cannot map this table to entity class, as this table is created run time (dynamically).

I tried to access by the Query "SELECT * FROM tableName" but I received the error:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM tableName].

And when I tried "from tableName" I am getting mapping related errors. If it is a silly question then please provide me some links from where I can get information regarding this.
Please help me out.

Upvotes: 1

Views: 149

Answers (1)

StanislavL
StanislavL

Reputation: 57421

It's not possible in HQL but hibernate allows to fire native SQL.

You can use

SQLQuery nativeQuery=session.createSQLQuery("SELECT * FROM tableName");
List results=nativeQuery.list();

And check the list. Or add Entity to map the results using nativeQuery.addEntity(YourEntityClassHere.class)

Upvotes: 2

Related Questions