指定JPA实体“Group”是否是非法的? [英] Is it illegitimate to name an JPA entity "Group"?

查看:107
本文介绍了指定JPA实体“Group”是否是非法的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 包模型
//进口...
@Entity
public class Group {
@Id @GeneratedValue
private Long id;

@NotNull
私人字符串名称;

// Getters and Setters
}

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



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

  [#| 2010 -06-30T11:54:29.862 + 0200 |信息| glassfish3.0.1 | org.hibernate.cfg.AnnotationBinder | _ThreadID = 11; _ThreadName = Thread-1; |从注释类绑定实体:model.Group |#] 
[#| 2010-06-30T11:54:29.862 + 0200 | INFO | glassfish3.0.1 | org.hibernate.cfg.annotations.EntityBinder | _ThreadID = 11; _ThreadName = Thread-1; |绑定实体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; |不成功:创建表组(id bigint不为空auto_increment,名称varchar(255)不为null,主键(id))|#]
[#| 2010-06-30T11:54:33.773 + 0200 | SEVERE | glassfish3.0.1 | org.hibernate.tool.hbm2ddl.SchemaExport | _ThreadID = 11; _ThreadName = Thread-1; |您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行|#]
附近使用'Group(id bigint not null auto_increment,name varchar(255)not null,primary ke' 2010-06-30T11:54:54.883 + 0200 |信息| glassfish3.0.1 | org.hibernate.cfg.AnnotationBinder | _ThreadID = 25; _ThreadName = Thread-1; |从注释类绑定实体:model.Group |#]
[#| 2010-06-30T11:54:54.884 + 0200 | INFO | glassfish3.0.1 | org.hibernate.cfg.annotations.EntityBinder | _ThreadID = 25; _ThreadName = Thread-1; |绑定实体模型.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; |您的SQL语法有错误;请查看手册对应于您的MySQL服务器版本,在第1行|#]
附近使用正确的语法以使用'Group(id bigint not null auto_increment,name varchar(255)not null,primary ke'

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

  [#| 2010-06-30T11:58: 51.456 + 0200 |信息| glassfish3.0.1 | org.hibernate.cfg.AnnotationBinder | _ThreadID = 11; _ThreadName = Thread-1; |从注释类绑定实体:model.MyGroup |#] 
[#| 2010- 06-30T11:58:51.456 + 0200 |信息| glassfish3.0.1 | org.hibernate.cfg.annotations.EntityBinder | _ThreadID = 11; _ThreadName = Thread-1; |绑定实体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; |来自注释类:model的绑定实体。 MyGroup |#]
[#| 2010-06-30T11:59:21.569 + 0200 | INFO | glassfish3.0.1 | org.hibernate.cfg.annotations.EntityBinder | _ThreadID = 25; _ThreadName = Thread-1;绑定实体model.MyGroup在表MyGroup |#]

任何人都知道问题是什么?
好​​的,我可以将Group重命名为MyGroup,但我真的很想知道这里发生了什么。我现在有什么限制,比如不要调用实体组吗?但是,如果是这样,为什么错误我得到如此不清楚?解决方案

组是数据库中的保留字MySQL查看此处

编辑:

 包装模型
//进口...
@Entity
@Table(name =group_table)
public class Group {
@Id @GeneratedValue
private Long id;

@NotNull
私人字符串名称;

// Getters and Setters
}


I'm developing a JEE6-application, using JPA 2.0 and Hibernate 3.5.2-Final as the Provider (and MySQL 5.1.41). My Application Server is Glassfish V3.0.1. I already have a working CRUD-app with some entities and relationships.

Now i added an (really simple) entity with the name "Group". The entity class looks like this:

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

  @NotNull
  private String name;

  //Getters and Setters
}

Of course I also added it to the persistence.xml, like <class>model.Group</class>. My persistence.xml drops and recreates all tables on deploy.

So when i deploy my application, the tables for all my entities are generated, except for the table group. In the hibernate logs, i discovered the following error (which doesn't prevent the application to be deployed)

[#|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|#]

Now when i rename the entity to something like "MyGroup" (the properties stay the same), alter the persistence.xml accordingly, and redeploy my app, the table "MyGroup" is successfully created! I found the following lines in the logs showing that MyGroup is correctly created:

[#|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|#]

Anyone got an idea what the problem is? Okay, i could just rename Group to MyGroup, but i really want to know what's going on here. Is there any restriction i should now of, like "don't call an entity Group"? But if it is so, why is the error i get so unclear?

解决方案

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
}

这篇关于指定JPA实体“Group”是否是非法的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆