JPA: Weird error when I try to persist an object

穿精又带淫゛_ 提交于 2019-12-01 20:13:32

GROUP is indeed a reserved keyword, you'll have to escape it. In JPA 2.0, there is a standardized way to specify delimited identifiers. From the JPA 2.0 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 something like this should work:

@Entity
@Table(name="\"GROUP\"")
public class Group {
    ...
}
kernel255

Found the same problem with an Entity with an attribute named external, which is a SQL reserved word. Resolved with the suggested solution:

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