单个租户的 Hibernate 数据导出/导入
我过去曾发布过一个关于使用非常具体的技术实施多租户解决方案的相当普遍的问题。我现在有一个更具体的问题,希望得到一些意见。
对于我们的多租户应用程序,我们需要将单个租户的数据从一个数据库导出到另一个数据库。例如,我们的数据库可能包含 10 个租户的数据,但这 10 个租户之一想要拥有自己的应用程序实例(无论出于何种原因,例如,他们需要非常具体的自定义)。
所以我基本上希望能够编写一个“工具”来仅获取租户 X 的数据并将其移至其自己的数据库中。
我们使用 MySQL 数据库以及最新版本的 Hibernate 核心和注释。
我们实现多租户的方式基本上是向我们所有的表/所有实体扩展的基类添加一个租户 id。
最初我以为我可以使用这样的东西:
mysqldump -uusername -ppassword database_name -w "tenant_id=1" > dump.sql
但不幸的是所有@JoinTables都没有tenant_id,所以这失败了。
我的问题是:是否有人对如何导出单个租户的数据然后将其导入到新数据库(我想我可以使用标准 Hibernate SchemaExport 创建)有任何好主意?
我有一个想法,但为了正确实现它,我需要 Hibernate 创建没有任何约束的数据库模式,然后我可以将数据输入,然后我需要在导入后的数据库上施加约束。我不知道如何让 Hibernate 首先不添加约束,然后稍后再添加,所以如果有人有想法,请告诉我。
谢谢
I have posted quite a general question about implementing a multi-tenant solution using very specific technologies in the past. I now have a much more specific question that I was hoping to get some input on.
For our multi-tenant application we have a requirement to export a single tenant's data from one database into another database. For example, our DB might contain 10 tenants' data but one of these 10 tenants wants to have his own instance of the application (for whatever reasons, e.g. they require very specific customizations).
So I basically want to be able to write a "tool" to take only the data for tenant X and move that into it's own database.
We are using a MySQL database and also the latest version of Hibernate core and annotations.
The way we have implemented multi-tenancy is basically to add a tenant id to all our tables / the base class from which all our entities extend.
Originally I thought that I could just use something like this:
mysqldump -uusername -ppassword database_name -w "tenant_id=1" > dump.sql
but unfortunately all @JoinTables do not have a tenant_id so this fails.
My question is: does anyone have any good ideas about how to export a single tenant's data and then import this into a new database (which I guess I can create using the standard Hibernate SchemaExport)?
I had one idea but for me to implement it properly I would need Hibernate to create my db schema with no constraints on it and then I could pump the data in and I would then need to put the constraints on the db post import. I have no idea how to get Hibernate to not add constraints at first and then do it later though so if anyone has an idea please let me know.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论