【问题标题】:org.hibernate.AnnotationException: Unable to create unique key constraintorg.hibernate.AnnotationException:无法创建唯一键约束
【发布时间】:2014-08-07 12:51:05
【问题描述】:

我使用的是hibernate 4.3.5.Final版本。

为了在数据库中处理保留字,我在 hibernate 中使用了一个属性 hibernate.globally_quoted_identifiers = true。

我的 pojo 类有一个独特的列,它看起来像

@Entity
@Table(name="theme1"
,catalog="theme2"
, uniqueConstraints = @UniqueConstraint(columnNames={"name1"}) 
public class Theme1  implements java.io.Serializable {

 @Id @GeneratedValue(strategy=IDENTITY)
 private Integer id;

 @Column(name="name1", unique=true, nullable=false, length=32)
 private String name1;

 .....

然后,当我的 SessionFactoryBean 正在加载时,它会失败并出现以下错误

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint   (name1) on table theme1: database column 'name1' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1682)
at org.hibernate.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1614)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1450)

在调试过程中,我发现问题是由于我添加的属性(hibernate.globally_quoted_identifiers)。 添加此属性后,Hibernate 将附加单引号来处理保留字。但是从 PhysicalToLogical 映射时,无法将“name1”映射到 name1。因此出现上述错误。

任何人都可以建议如何一次处理上述两种情况(保留词+ UniqueConstraint)。

【问题讨论】:

    标签: java hibernate jpa unique-constraint


    【解决方案1】:

    当设置了 hibernate.globally_quoted_identifiers 时,Hibernate 需要准确的列名。 检查JPA with Hibernate 3.6.8.Final, PostgresSQL 9.1, SQLGrammarException - configuration issue? Weird SQL statement 了解更多信息。 所以根据这个,你的pojo类中的列名和表名需要加引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2011-05-28
      • 1970-01-01
      相关资源
      最近更新 更多