小编典典

将JPA实体命名为“组”是不合法的吗?

hibernate

我正在开发一个JEE6-应用程序,使用JPA 2.0和Hibernate 3.5.2-Final作为提供程序(和MySQL
5.1.41)。我的应用程序服务器是Glassfish V3.0.1。我已经有一个带有某些实体和关系的CRUD应用程序。

现在,我添加了一个(非常简单的)实体,名称为“ Group”。实体类如下所示:

package model
//Imports...
@Entity
public class Group {
  @Id @GeneratedValue
  private Long id;

  @NotNull
  private String name;

  //Getters and Setters
}

当然,我也将其添加到persistence.xml中,例如<class>model.Group</class>。我的persistence.xml删除并在部署时重新创建所有表。

因此,当我部署我的应用程序时,将生成除表组以外的所有实体的表。在hibernate日志中,我发现了以下错误(这不会阻止应用程序的部署)

[#|2010-06-30T11:54:29.862+0200|INFO|glassfish3.0.1|org.hibernate.cfg.AnnotationBinder|_ThreadID=11;_ThreadName=Thread-1;|Binding entity from annotated class: model.Group|#]
[#|2010-06-30T11:54:29.862+0200|INFO|glassfish3.0.1|org.hibernate.cfg.annotations.EntityBinder|_ThreadID=11;_ThreadName=Thread-1;|Bind entity model.Group on table Group|#]
[#|2010-06-30T11:54:33.773+0200|SEVERE|glassfish3.0.1|org.hibernate.tool.hbm2ddl.SchemaExport|_ThreadID=11;_ThreadName=Thread-1;|Unsuccessful: create table Group (id bigint not null auto_increment, name varchar(255) not null, primary key (id))|#]
[#|2010-06-30T11:54:33.773+0200|SEVERE|glassfish3.0.1|org.hibernate.tool.hbm2ddl.SchemaExport|_ThreadID=11;_ThreadName=Thread-1;|You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group (id bigint not null auto_increment, name varchar(255) not null, primary ke' at line 1|#]
[#|2010-06-30T11:54:54.883+0200|INFO|glassfish3.0.1|org.hibernate.cfg.AnnotationBinder|_ThreadID=25;_ThreadName=Thread-1;|Binding entity from annotated class: model.Group|#]
[#|2010-06-30T11:54:54.884+0200|INFO|glassfish3.0.1|org.hibernate.cfg.annotations.EntityBinder|_ThreadID=25;_ThreadName=Thread-1;|Bind entity model.Group on table Group|#]
[#|2010-06-30T11:54:58.402+0200|SEVERE|glassfish3.0.1|org.hibernate.tool.hbm2ddl.SchemaExport|_ThreadID=25;_ThreadName=Thread-1;|Unsuccessful: create table Group (id bigint not null auto_increment, name varchar(255) not null, primary key (id))|#]
[#|2010-06-30T11:54:58.403+0200|SEVERE|glassfish3.0.1|org.hibernate.tool.hbm2ddl.SchemaExport|_ThreadID=25;_ThreadName=Thread-1;|You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Group (id bigint not null auto_increment, name varchar(255) not null, primary ke' at line 1|#]

现在,当我将实体重命名为“ MyGroup”(属性保持不变)时,相应地更改persistence.xml,然后重新部署我的应用程序,表“
MyGroup”已成功创建!我在日志中发现以下行,表明正确创建了MyGroup:

[#|2010-06-30T11:58:51.456+0200|INFO|glassfish3.0.1|org.hibernate.cfg.AnnotationBinder|_ThreadID=11;_ThreadName=Thread-1;|Binding entity from annotated class: model.MyGroup|#]
[#|2010-06-30T11:58:51.456+0200|INFO|glassfish3.0.1|org.hibernate.cfg.annotations.EntityBinder|_ThreadID=11;_ThreadName=Thread-1;|Bind entity model.MyGroup on table MyGroup|#]
[#|2010-06-30T11:59:21.569+0200|INFO|glassfish3.0.1|org.hibernate.cfg.AnnotationBinder|_ThreadID=25;_ThreadName=Thread-1;|Binding entity from annotated class: model.MyGroup|#]
[#|2010-06-30T11:59:21.569+0200|INFO|glassfish3.0.1|org.hibernate.cfg.annotations.EntityBinder|_ThreadID=25;_ThreadName=Thread-1;|Bind entity model.MyGroup on table MyGroup|#]

任何人都知道问题出在哪里吗?好的,我可以将Group重命名为MyGroup,但是我真的很想知道这里发生了什么。我现在应该有什么限制,例如“不要叫实体组”?但是,如果是这样,为什么我仍然不清楚该错误?


阅读 297

收藏
2020-06-20

共1个答案

小编典典

组是数据库MySQL中的保留字,请参见此处

编辑:

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

  @NotNull
  private String name;

  //Getters and Setters
}
2020-06-20