Search code examples
javahibernatejpajpql

use column name instead of entity property in JPQL


I have JPQL/Hibernate for DA layer. I have a table as following:

CREATE TABLE Log_Tbl
(
id number,
name varchar2(30),
);

and I have a entity for above table as following:

@Entity
@Table(name = "Log_Tbl")
public class ELog
{
  @Column(name = "id")
  private long entity_id;

  @Column(name = "name") 
  private String entity_name;
}

and map Log entity to Log_Tbl table by jpa; I have two JPQL queries as following:

  • select ELog from ELog where entity_name = 'Job'
  • select ELog from ELog where name = 'Job'

Both queries returned the correct result. My question is:

Why does the second query return the correct result although I used the column name instead of the entity_name property?


Solution

  • To quote a post to a similar (if not duplicate) question

    When you use something that isn't known by Hibernate in the WHERE clause of an HQL query (e.g. a function that is not registered in the SQL dialect), Hibernate acts smartly and passes it directly to the database.

    So, it does work but it is vendor specific and probably wouldn't work if you switched your JPA implementation.