Is it illegitimate to name an JPA entity “Group”?

安稳与你 提交于 2019-12-05 05:29:05

Group is a reserved word in your database MySQL see here

Edit:

package model
//Imports...
@Entity
@Table(name = "group_table")
public class Group {
  @Id @GeneratedValue
  private Long id;

  @NotNull
  private String name;

  //Getters and Setters
}

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 the persistence-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="&quot;customer&quot;"/>

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!