简体   繁体   English

使用Liquibase和Hibernate的放置/插入约束

[英]Drop/Insert constraint using Liquibase and Hibernate

I'm new to Liquibase + Hibernate. 我是Liquibase + Hibernate的新手。

I have a table named Users, with columns {Id, FirstName, LastName, YearOfBirth, .....}. 我有一个名为Users的表,其列为{Id,FirstName,LastName,YearOfBirth,.....}。 I set with Hibernate annotation a unique constraint for the couple {Id, LastName}. 我使用Hibernate注释为这对{Id,LastName}设置了唯一约束。

Now, I decided that I want to update the constraint, to include the FirstName column, meaning I want my unique constraint to be {Id, FirstName, LastName}. 现在,我决定要更新约束,以包括FirstName列,这意味着我希望我的唯一约束为{Id,FirstName,LastName}。 Can this be done through Liquibase, on my existing db, through a changeset? 是否可以通过更改集通过现有数据库上的Liquibase完成?

I thought about dropping the old constraint through a changeset, and creating the new constraint with another. 我考虑过通过变更集删除旧约束,并与另一个创建新约束。 The problem is, I don't know the name of the old constraint, neither the names of the indexes following it. 问题是,我不知道旧约束的名称,也不知道其后的索引的名称。 And since Liquibase/Hibernate auto-generate names for the constraints/indexes, I don't want to search through a query to fetch the specific constraint name. 而且由于Liquibase / Hibernate自动为约束/索引生成名称,所以我不想搜索查询以获取特定的约束名称。

I'm searching for a solution that is more generic, so I can tackle same future issues. 我正在寻找更通用的解决方案,以便将来能够解决同样的问题。

Any suggestions? 有什么建议么?

I'm using Java 8, Hibernate 5.2.10, Liquibase 3.5.2 我正在使用Java 8,Hibernate 5.2.10,Liquibase 3.5.2

Unfortunately, the index name is a required field when dropping it. 不幸的是,索引名称在删除时是必填字段。 Here is the relevant documentation from liquibase . 这是liquibase的相关文档。

In this case, you will need to drop the index using SQL commands. 在这种情况下,您将需要使用SQL命令删除索引。 Depending on what the underlying database is, you will need to programmatically figure out the name of the index assigned to the table that you would like to drop, based on the column names of the index. 根据基础数据库是什么,您将需要根据索引的列名以编程方式找出分配给要删除的表的索引名。 For example, if you are using Oracle database you could run the SQL explained here and capture these commands in a sql file. 例如,如果您使用的是Oracle数据库,则可以运行此处说明的SQL 并将这些命令捕获到sql文件中。 Liquibase supports including SQL files as changesets, so you will need to use the sqlFile tag in your changeSet as explained here so that the sql statements that you captured earlier is run. Liquibase支持将SQL文件作为变更集包含在内,因此,您将需要按此处所述在changeSet中使用sqlFile标记,以便运行您先前捕获的sql语句。 Once the index is dropped, you could use the liquibase changeSet again to add a new index/constraint as needed. 删除索引后,您可以再次使用liquibase changeSet根据需要添加新的索引/约束。

Some notes for the future: 未来的一些注意事项:

You can set a unique constraint name by this way: 您可以通过以下方式设置唯一的约束名称:

@Table(uniqueConstraints = 
    @UniqueConstraint(columnNames = { "unique_field" }, name = "UK_users_unique_field")

I think, you shouldn't create a schema by the Hibernate, if you want to use Liquibase. 我认为,如果要使用Liquibase,则不应由Hibernate创建架构。 You need to create a schema by Liquibase with your SQL scripts. 您需要使用SQL脚本通过Liquibase创建架构。 You can generate a SQL script by Hibernate of course. 您当然可以通过Hibernate生成SQL脚本。

Use 采用

hibernate.hbm2ddl.auto=validate 

to validate a database schema only, without creating anything. 仅验证数据库模式,而不创建任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM