Java EE 中的动态类型表/模型?

发布于 2024-08-29 07:18:12 字数 308 浏览 6 评论 0原文

通常在Java EE中,当我们创建Model时,我们在编译之前通过XML或注释来定义字段和字段类型。有没有办法在运行时更改这些?或者更好的是,是否可以在运行时根据用户的输入创建一个新模型?这样列的数量和字段的类型是动态的(在运行时确定)?

非常感谢您的帮助。谢谢。

我觉得有必要澄清一下自己。

  1. 是的,当谈论模型时,我指的是数据库建模。

  2. 对于用例,我想为用户提供一种定义和创建自己的表的方法。不需要无限的灵活性。然而,必须有一定程度的自由:例如,用户可以定义描述其产品所需的字段。

Usually with Java EE when we create Model, we define the fields and types of fields through XML or annotation before compilation time. Is there a way to change those in runtime? Or better, is it possible to create a new Model based on the user's input during the runtime? Such that the number of columns and types of fields are dynamic (determined at runtime)?

Help is much appreciated. Thank you.

I felt the need to clarify myself.

  1. Yes, I meant database modeling, when talking about Model.

  2. As for the use cases, I want to provide a means for users to define and create their own tables. Infinite flexibility is not required. However some degree of freedom has to be there: e.g. the users can define what fields are needed to describe their product.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼泪都笑了 2024-09-05 07:18:12

您听起来好像希望能够根据运行时的用户输入来更改对象和架构。对我来说,这听起来像是一场灾难。我从来没有见过它被完成。

我见过将外键关系合并到名称/值对的通用表中的通用模式,但这些往往会成为无限灵活的抽象,在性能方面既不能轻易理解,也不能摆脱自己的方式。

我敢打赌,您的用户确实不想要无限的灵活性。我警告你不要走这个方向。最好弄清楚你的真实用例。

当然,一切皆有可能。我的直接经验告诉我,如果你能成功的话,你的用户会讨厌它,这是一个坏主意。祝你好运。

You sound like you want to be able to change both objects and schema according to user input at runtime. This sounds like a chaotic recipe for disaster to me. I've never seen it done.

I have seen general schemas that incorporate foreign key relationships to generic tables of name/value pairs, but these tend to become infinitely flexible abstractions that can neither be easily understood nor get out of their own way when it comes to performance.

I'm betting that your users really don't want infinite flexibility. I'd caution you against taking this direction. Better to get your real use cases straight.

Anything is possible, of course. My direct experience tells me that it's a bad idea that your users will hate if you can pull it off. Best of luck.

隔纱相望 2024-09-05 07:18:12

我在一个拥有此类设施的系统上工作。为了保持效率,我们将为客户模式动态生成/更改表。我们还需要嵌入一个元模型(模型的模型)来动态处理实体中的信息。

选项 1:使用自定义表,您可以获得充分的灵活性,但它也会显着增加复杂性,尤其是现有数据的更新/迁移。以下是您需要考虑的事项列表:

  • 如果列的类型发生变化怎么办?
  • 如果添加一列怎么办?有默认值吗?
  • 如果删除一列怎么办?我可以放弃现有的信息吗?
  • 如何管理列的重命名?
  • 如何使事物跨数据库可移植?
  • 如何使其在数据库级别(例如索引)高效?
  • 如何管理人为错误(例如用户删除一列然后改变主意)?
  • 当客户站点安装新版本系统时,如何管理迁移(脚本、部署等)?
  • 使用 ORM 时如何实现这一点?

选项2:一个轻量级的替代方案是在不同类型的业务表中添加一些“备用”列(例如:“USER_DATE_1”、“USER_DATE_2”等)我见过一些次。它会让您的 DBA 尖叫,并且实际上并不被认为是一个好的实践,但至少可以促进一些事情,例如(迁移脚本、ORM 集成)。

选项 3:另一个选项是将所有内容存储在具有结构属性/数据的表中。但这对于数据库性能来说确实是一场灾难。任何不完全琐碎的事情都需要许多连接。 DBA 会尖叫得更厉害。

选项 4:它是选项 2 和 3 的混合。核心表是固定的,但可以使用具有属性/数据的表以某种方式扩展它们。

总之:在走这条路之前要三思而后行。这是可以做到的,但对应用程序的设计和维护有重大影响。

I worked on a system where we had such facilities. To stay efficient, we would generate/alter the table dynamically for the customer schema. We also needed to embed a meta-model (the model of the model) to process information in the entities dynamically.

Option 1: With custom tables, you have full flexibility, but it also increases the complexity significantly, notably the update/migration of existing data. Here is a list of things you will need to consider:

  • What if the type of a column change?
  • What if a column is added? Is there a default value?
  • What if a column is removed? Can I discard the existing information?
  • How to manage renaming of a column?
  • How to make things portable across databases?
  • How to make it efficient at database-level (e.g. indexes) ?
  • How to manage a human error (e.g. user removes a column then changes its mind)?
  • How to manage migration (script, deployment, etc.) when new version of the system is installed at customer site?
  • How to have this while using an ORM?

Option 2: A lightweight alternative is to add a few "spare" columns in the business tables of different types (e.g.: "USER_DATE_1", "USER_DATE_2", etc.) I've seen that a few times. It will makes your DBA scream and is not really considered a good practice, but at least can facilitates a few things, e.g. (migration scripts, ORM integration).

Option 3: Another option is to store everything in a table with a structure property/data. But then it's really a disaster for database performance. Anything that is not completely trivial will require many joins. And the DBA will scream even more.

Option 4: It is a mix of options 2 and 3. Core tables are fixed, but a table with property/data can be used to somehow extend them.

In summary: think twice before you go this way. It can be done, but has a significant impact on the design and maintenance of the application.

一抹苦笑 2024-09-05 07:18:12

使用元建模技术,这在某种程度上是可能的:

  • 数据库级别的表/列/类型的表
  • Java 级别的键/值结构

但这有明显的限制(缺乏强类型对象),恕我直言,它会很快变得非常复杂(不是甚至确定如何处理关系)。我不会使用这种方法来完全定义域对象,而只是扩展现有的域对象(产品、文章等)。

如果我没记错的话,这就是一些电子商务解决方案(例如 BroadVision)正在做的事情。

This is somehow possible using meta-modeling techniques:

  • tables for table / column / types at the database level
  • key/value structures at the Java level

But this has obvious limitations (lack of strong typed objects) and can IMHO get quickly very complicated (not even sure how to deal with relations). I wouldn't use this approach to define domain objects entirely, but only to extend existing ones (products, articles, etc).

If I remember well, this is what some e-commerce solutions (e.g. BroadVision) were doing.

丢了幸福的猪 2024-09-05 07:18:12

我想我自己已经找到了一个很好的答案。那些新的 no-sql(hbase、cassandra)数据库似乎正是我正在寻找的。谢谢大家的回答。

I think I have found a good answer myself. Those new no-sql (hbase, cassandra) database seems to be exactly what I was looking for. Thanks everyone for your answeres.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文