为了处理数据库中的保留字,我在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) $ c $在调试过程中,我发现问题是因为我添加的属性(hibernate.globally_quoted_identifiers)。 当添加这个属性时,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:无法创建唯一键约束
发布评论