Hibernate Annotations - 不区分大小写的UniqueConstraint [英] Hibernate Annotations - Case Insensitive UniqueConstraint
问题描述
@Entity
@Table(uniqueConstraints = {@ UniqueConstraint (columnNames = {name})})
public class Component extends Model {
...
}
是否有可能使UniqueConstraint区分大小写?我们正在使用PostgreSQL。
使用PostgreSQL,您确实可以这样做来实现您的要求:
CREATE UNIQUE INDEX My_Index on Component(lower(name));
但是,据我所知,没有办法使用注释来实现这一点。
如果您想依赖Hibernate的 hbm2ddl
工具来生成模式并且仍然创建该索引,那么我可以考虑的唯一选择是利用 import.sql
功能。从鹿特丹JBug和Hibernate的import.sql 博客文章:
import.sql:轻松地在单元测试中导入数据
Hibernate有一个整洁的小功能
,它是大量记录不足和
未知。您可以在数据库模式
生成后立即在SessionFactory
创建
期间执行SQL脚本
,以将数据导入新鲜
数据库。您只需要在您的类路径
root中添加一个名为import.sql
的文件
,并设置create
或
create-drop
作为您的
hibernate.hbm2ddl.auto
属性。
我将它用于
中的Hibernate Search现在我开始执行
查询章节。它使用
我的单元测试的一组新数据初始化我的
数据库。在不同的例子中,JBoss Seam也使用它
。
import.sql
是一个非常简单的功能
,但在时间上非常有用。记住
,SQL可能依赖
数据库(可移植性!)。
$ $ p $ $ $ $ $ $#$ .sql文件
从产品
中删除插入到产品(PROD_ID,ASIN,TITLE,PRICE,IMAGE_URL,DESCRIPTION)values('1','630522577X','My Fair Lady',19.98,'630522577X .jpg','我的公平等等等等');
插入到产品(PROD_ID,ASIN,TITLE,PRICE,IMAGE_URL,DESCRIPTION)值('2','B00003CXCD','罗马假日',12.98,'B00003CXCD.jpg','我们可以争辩说,等等等等);
有关此
功能的更多信息,请查看 Eyal的博客(更新死链接位置),他
写了一个很好的关于它的小条目。
请记住,如果您想添加额外的
数据库对象(索引,表格和
等),您还可以使用
辅助数据库对象
功能。
I have an entity annotated with the following:
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"name"})})
public class Component extends Model {
...
}
Is it possible to make the UniqueConstraint case insensitive? We are using PostgreSQL.
With PostgreSQL, you would indeed do something like this to implement your requirement:
CREATE UNIQUE INDEX My_Index on Component (lower(name));
But there is to my knowledge no way to achieve this using annotations.
The only option I can think of if you want to rely on Hibernate's hbm2ddl
tool to generate the schema and still have that index created would be to leverage the import.sql
feature. From the Rotterdam JBug and Hibernate's import.sql blog post:
import.sql: easily import data in your unit tests
Hibernate has a neat little feature that is heavily under-documented and unknown. You can execute an SQL script during the
SessionFactory
creation right after the database schema generation to import data in a fresh database. You just need to add a file namedimport.sql
in your classpath root and set eithercreate
orcreate-drop
as yourhibernate.hbm2ddl.auto
property.I use it for Hibernate Search in Action now that I have started the query chapter. It initializes my database with a fresh set of data for my unit tests. JBoss Seam also uses it a lot in the various examples.
import.sql
is a very simple feature but is quite useful at time. Remember that the SQL might be dependent on your database (ah portability!).#import.sql file delete from PRODUCTS insert into PRODUCTS (PROD_ID, ASIN, TITLE, PRICE, IMAGE_URL, DESCRIPTION) values ('1', '630522577X', 'My Fair Lady', 19.98, '630522577X.jpg', 'My Fair blah blah...'); insert into PRODUCTS (PROD_ID, ASIN, TITLE, PRICE, IMAGE_URL, DESCRIPTION) values ('2', 'B00003CXCD', 'Roman Holiday ', 12.98, 'B00003CXCD.jpg', 'We could argue that blah blah');
For more information about this feature, check Eyal's blog (Updated Location for dead link), he wrote a nice little entry about it. Remember if you want to add additional database objects (indexes, tables and so on), you can also use the auxiliary database objects feature.
这篇关于Hibernate Annotations - 不区分大小写的UniqueConstraint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!