简体   繁体   English

带有动态方案的 Jpa

[英]Jpa with dynamic scheme

I'm working on a software that uses JSF(2.1)/JPA(Hibernate)/Spring/Jboss7.1.我正在开发一个使用 JSF(2.1)/JPA(Hibernate)/Spring/Jboss7.1 的软件。 The user will have many clients, and their data wont be used together.用户会有很多客户端,他们的数据不会被一起使用。

So, I have these tables:所以,我有这些表:

Company
ID | Name
1  | Foo
2  | Bar

Products
ID | Company_ID | Name
1  | 1          | Doll 01
2  | 1          | Doll 02
3  | 2          | Candy

When working on company 1, the user doesn't need the data from Company 2.在公司 1 上工作时,用户不需要来自公司 2 的数据。

Today, all I have to do is use a where clause.今天,我所要做的就是使用 where 子句。

However the DBA thinks we might have problems when the data get really high, and suggests we should use One scheme for each company.然而,DBA 认为当数据变得非常高时我们可能会遇到问题,并建议我们应该为每个公司使用一个方案。

So, the above structure would be something like this:所以,上面的结构将是这样的:

GeneralScheme.Company
ID | Name
1  | Foo
2  | Bar

Company1.Products           Company2.Products
ID | Name                   ID | Name
1  | Doll 01                3  | Candy
2  | Doll 02  

He says, this way would be easier to make specifics backups, and performance could be better as well.他说,这种方式更容易进行具体的备份,而且性能也会更好。 But, is it possible to work this way on JPA?但是,是否可以在 JPA 上以这种方式工作? Changing the scheme at runtime?在运行时更改方案? And creating new schemes as well since the user might add others companies?由于用户可能会添加其他公司,因此还要创建新方案?

Thanks.谢谢。

Using multiple schemas as a "horizontal partitioning" mechanism sounds like the "poor man's sharding".使用多个模式作为“水平分区”机制听起来像是“穷人的分片”。 I would go with a clean design with only one table: PRODUCT.我会采用只有一张桌子的简洁设计:产品。 Since you use PostgreSQL, in case it grows large you can simplypartition it.由于您使用 PostgreSQL,如果它变大,您可以简单地对其进行分区

If table partitioning is too basic you can go and shard even more than one table .如果表分区太基本,您甚至可以对多个表进行分片 Check out how Braintree did it.看看布伦特里是如何做到的。

Hibernate supports multiple schemas, but mappings are holding the schema info, so loading new mapping at runtime is probably possible, yet tricky. Hibernate 支持多个模式,但映射保存模式信息,因此在运行时加载新映射可能是可能的,但很棘手。

The best designs are simple and elegant, and if you already start thinking of hacks to support them, you are not there yet.最好的设计是简单而优雅的,如果您已经开始考虑使用 hack 来支持它们,那么您还没有到位。

Hibernate supports operating against multiple databases/schemas/shards in a multitenacy fashion. Hibernate 支持以multitenacy方式对多个数据库/模式/分片进行操作。

You could, in theory, use different a DataSource (each with a corresponding EntityManager and TransactionManager) for each Customer schema, but you wont be able to add new Customer schemas without restarting the application (or web server) since you will need to register new beans (new DataSource, etc...) in Spring each time a new Customer is added.从理论上讲,您可以为每个 Customer 模式使用不同的 DataSource(每个都有相应的 EntityManager 和 TransactionManager),但是您将无法在不重新启动应用程序(或 Web 服务器)的情况下添加新的 Customer 模式,因为您需要注册新的每次添加新客户时,Spring 中的 bean(新数据源等)。

Also you will need to implement discrimination logic somewhere in you service/dao layer so that each customer is persisted using the correct EntityManager instance.此外,您还需要在服务/dao 层的某处实现区分逻辑,以便每个客户都使用正确的 EntityManager 实例进行持久化。

In summary, it is not a good idea, specially since there are other possible database optimizations (depending on you database server).总之,这不是一个好主意,特别是因为还有其他可能的数据库优化(取决于您的数据库服务器)。

Obviously, you could do this in plain JDBC .显然,您可以在普通JDBC执行此操作。

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

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