Java 使用改进的命名策略创建表时出错
我的pojo看起来像这样:Java 使用改进的命名策略创建表时出错,java,spring,hibernate,postgresql,spring-mvc,Java,Spring,Hibernate,Postgresql,Spring Mvc,我的pojo看起来像这样: @Entity @Table(name = "USERS") public class User { 当我保留名称时,一切正常,hibernate sql log: create table users (id int8 not null, username varchar(255), primary key (id)) 删除注释时,我得到: Hibernate: create table user (id int8 not null, username var
@Entity
@Table(name = "USERS")
public class User {
当我保留名称时,一切正常,hibernate sql log:
create table users (id int8 not null, username varchar(255), primary key (id))
删除注释时,我得到:
Hibernate: create table user (id int8 not null, username varchar(255), primary key (id))
19:24:43 [localhost-startStop-23] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: create table user (id int8 not null, username varchar(255), primary key (id))
19:24:43 [localhost-startStop-23] ERROR o.h.tool.hbm2ddl.SchemaExport - ERROR: syntax error at or near "user" Position: 14
我正试图使用它,因为它是在我的xml中定义的:
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
有什么建议,为什么namingstrategy不起作用吗?PostgreSQL关键字列表: 标记为保留的是那些不允许作为列或列的标记 表名 和您的表名: 用户保留 事实上是保留的 所以不能在PostgreSQL中创建名为user的表。这就是为什么使用注释时一切都正常,因为注释指定了用户名。没有这样的关键字。默认表名用户是保留关键字。在这种情况下,您必须明确指定名称,以便向其添加必要的转义:
@Entity
@Table(name = "`USER`")
public class User { ... }
用户不是保留关键字吗?使用该设置,表名是什么?是带引号的用户吗?@Jaanus:不带引号的用户。引号告诉Hibernate在查询中应用必要的转义。另一位发帖人说,用户根本不允许使用表名,你们确定它有效吗。或者CAPS版本会起作用?这个案子不重要。文档说明:一般来说,如果命令中包含任何列出的关键字作为标识符,那么您应该尝试引用该标识符,以查看问题是否消失。若在查询中引用user,那个么user是一个有效的表名。所以其他答案基本上是错误的,我可以在PostgreSQL中创建名为user的表。