org.hibernate.AnnotationException:无法创建唯一键约束 [英] org.hibernate.AnnotationException: Unable to create unique key constraint

查看:1021
本文介绍了org.hibernate.AnnotationException:无法创建唯一键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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实现了java.io.Serializable {

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

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

.....

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

 通过:org.hibernate.AnnotationException:无法创建唯一键约束(name1)在表主题1:数据库列'name1'找不到。确保您使用正确的列名称这取决于正在使用的命名策略(它可能与实体中的属性名称不同,特别是对于关系类型而言)
在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会追加单引号来处理保留字。但是从PhysicalToLogical进行映射时,它无法将name1映射到name1.Hence,因此我得到了上面的错误信息。



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

解决方案

当hibernate.globally_quoted_identifiers被设置时,Hibernate期待确切的列名。
使用Hibernate查看 JPA 3.6.8.Final,PostgresSQL 9.1,SQLGrammarException - 配置问题?奇怪的SQL语句了解更多信息。
所以根据这个,需要引用pojo类中的列名和表名。


I am using hibernate 4.3.5.Final version.

To handle reserve words at Database,I used a property in hibernate hibernate.globally_quoted_identifiers = true.

And My pojo class having a unique column and it looks like

@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;

 .....

Then when my SessionFactoryBean is loading it is failing with below error

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)

In the debugging process,I find issue is because of the property I add (hibernate.globally_quoted_identifiers). When this property is added,Hibernate will append single quotes to handle reserved words.But while mapping from PhysicalToLogical,it is failed to map 'name1' with name1.Hence I got above error.

Can any one suggest how to handle above two cases(reserve words + UniqueConstraint) at a time.

解决方案

When hibernate.globally_quoted_identifiers is set, Hibernate expects exact column name. Check JPA with Hibernate 3.6.8.Final, PostgresSQL 9.1, SQLGrammarException - configuration issue? Weird SQL statement for more information. So according to this, your column name and Table names in the pojo class need to be quoted.

这篇关于org.hibernate.AnnotationException:无法创建唯一键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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