Is it illegitimate to name an JPA entity "Group"?
You can use reserved keywords for database objects names if you tell the JPA provider to escape them. This has been standardized in JPA 2.0 as described in the following section of the specification:
2.13 Naming of Database Objects
(...)
To specify delimited identifiers, one of the following approaches must be used:
It is possible to specify that all database identifiers in use for a persistence unit be treated as delimited identifiers by specifying the
<delimited-identifiers/>
element within thepersistence-unit-defaults
element of the object/relational xml mapping file. If the<delimited-identifiers/>
element is specified, it cannot be overridden.It is possible to specify on a per-name basis that a name for a database object is to be interpreted as a delimited identifier as follows:
- Using annotations, a name is specified as a delimited identifier by enclosing the name within double quotes, whereby the inner quotes are escaped, e.g.,
@Table(name="\"customer\"")
.- When using XML, a name is specified as a delimited identifier by use of double quotes, e.g.,
<table name=""customer""/>
So the JPA 2.0 way would be to specify the Table
like this:
@Entity
@Table(name="\"Group\"")
public class Group {
@Id @GeneratedValue
private Long id;
@NotNull
private String name;
//Getters and Setters
}
This is definitely supported by Hibernate (see HHH-4553), no leaky abstraction here.
Group is a reserved word in your database MySQL see here
package model
//Imports...
@Entity
@Table(name = "group_table")
public class Group {
@Id @GeneratedValue
private Long id;
@NotNull
private String name;
//Getters and Setters
}
edit See @Pascals answer for JPA 2.0 way if you want to use words reserved in your db instance.
With some JPA implementations you can use classes with names like that, and the JPA implementation does the sensible thing and "quotes" the name in any SQL (since it is a reserved word as pointed out by Paul Whelan), so it is accepted. DataNucleus certainly allows this no problem.