gpt4 book ai didi

hibernate - 将 JPA 实体命名为 "Group"是否非法?

转载 作者:行者123 更新时间:2023-12-04 09:38:01 27 4
gpt4 key购买 nike

我正在开发一个 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,但我真的很想知道这里发生了什么。我现在应该有什么限制,比如“不要调用实体组”?但如果是这样,为什么我的错误如此不清楚?

最佳答案

如果您告诉 JPA 提供程序对它们进行转义,则可以对数据库对象名称使用保留关键字。这已在 JPA 2.0 中标准化,如规范的以下部分所述:

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;"/>


所以 JPA 2.0 的方式是指定 Table像这样:
@Entity
@Table(name="\"Group\"")
public class Group {
@Id @GeneratedValue
private Long id;

@NotNull
private String name;

//Getters and Setters
}

Hibernate 绝对支持这一点(请参阅 HHH-4553 ),这里没有泄漏的抽象。

关于hibernate - 将 JPA 实体命名为 "Group"是否非法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3148249/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com