Ing*_*her 7 orm hibernate jpa java-ee-6 jpa-2.0
我正在开发一个JEE6应用程序,使用JPA 2.0和Hibernate 3.5.2-Final作为Provider(和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
}
Run Code Online (Sandbox Code Playgroud)
当然我也把它添加到了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|#]
Run Code Online (Sandbox Code Playgroud)
现在,当我将实体重命名为"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|#]
Run Code Online (Sandbox Code Playgroud)
任何人都知道问题是什么?好的,我可以将Group重命名为MyGroup,但我真的想知道这里发生了什么.我现在应该有什么限制,比如"不要叫实体集团"吗?但如果是这样的话,为什么错误我会变得如此不清楚?
Pas*_*ent 14
如果告诉JPA提供程序转义它们,则可以对数据库对象名称使用保留关键字.这已在JPA 2.0中标准化,如规范的以下部分所述:
2.13数据库对象的命名
(......)
要指定分隔标识符,必须使用以下方法之一:
通过指定 对象/关系xml映射文件的
<delimited-identifiers/>元素内的persistence-unit-defaults元素,可以指定将用于持久性单元的所有数据库标识符视为分隔标识符.如果<delimited-identifiers/>指定了 元素,则无法覆盖它.可以在每个名称的基础上指定数据库对象的名称将被解释为分隔标识符,如下所示:
- 使用注释,通过将名称括在双引号内来将名称指定为分隔标识符,从而对内引号进行转义,例如
@Table(name="\"customer\"").- 使用XML时,通过使用双引号将名称指定为分隔标识符,例如,
<table name=""customer""/>
所以JPA 2.0的方式是指定Table如下:
@Entity
@Table(name="\"Group\"")
public class Group {
@Id @GeneratedValue
private Long id;
@NotNull
private String name;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
Hibernate肯定支持这一点(参见HHH-4553),这里没有泄漏抽象.
组是数据库 MySQL 中的保留字,请参见此处
package model
//Imports...
@Entity
@Table(name = "group_table")
public class Group {
@Id @GeneratedValue
private Long id;
@NotNull
private String name;
//Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
编辑 如果您想使用数据库实例中保留的单词,请参阅@Pascals对JPA 2.0方式的回答。
| 归档时间: |
|
| 查看次数: |
2573 次 |
| 最近记录: |