Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reserved JPQL keywords with JPA

I have an entity class called "Group" and NetBeans warns me "The entity table name is a reserved Java Persistence QL keyword".

A similar case would be the use of reserved SQL keywords.

Will this name be escaped? Would the use of a different table name solve the problem @Table(name="otherName"). Or should I rename the class?

like image 946
deamon Avatar asked Jan 28 '10 10:01

deamon


People also ask

Does JPA use JPQL?

The Java Persistence Query Language (JPQL) is the query language defined by JPA. JPQL is similar to SQL, but operates on objects, attributes and relationships instead of tables and columns.

Which methods should be used for pagination with JPQL?

For all JPA query objects (except for native SQL queries), you would use pagination through the setMaxResults(int) and setFirstResult(int) methods.

Does hibernate use JPQL?

It behaves as a runtime interface between a Java application and Hibernates. It uses Java Persistence Query Language (JPQL) is an object-oriented query language to perform database operations. It uses Hibernate Query Language (HQL) is an object-oriented query language to perform database operations.

What is the use of @query in JPA?

In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file.


1 Answers

Will this name be escaped?

There is nothing in the JPA spec that says so, if your provider does, this is provider specific.

Would the use of a different table name solve the problem @Table(name="otherName")

Obviously, it would (as long as you don't use another reserved keyword of course). But if you are using a JPA 2.0 provider, there is a standard way to get a db object name escaped, with double quotes:

@Table(name="\"Group\"")

In JPA 1.0, there is nothing standard, it depends on your JPA provider. For example, Hibernate uses backticks:

@Table(name="`Group`")

Or should I rename the class?

No. The table name of an entity defaults to the entity name but you can control it using the @Table annotation as we saw. There is thus no need to change the class name of your entity.

like image 197
Pascal Thivent Avatar answered Oct 09 '22 21:10

Pascal Thivent