如何动态创建Java POJO类?

发布于 2024-11-30 04:45:24 字数 228 浏览 0 评论 0原文

我在这个网站上看到过一篇关于动态 POJO 生成的帖子。我现在也有类似的需求。

我在数据库中有一些表。我希望每个表都有一个 POJO 类,其中包含字段以及相应的 getter 和 setter。这些类将动态创建。创建这些类后,我应该使用其他类中的这些 setter 和 getter 来获取和设置数据并返回 Java 对象。

我见过 BCEL、CGLIB 和其他一些开源工具,但找不到合适的例子。你能帮助我吗?

I have seen a post in this website regarding the dynamic POJO generation. I have the similar requirement now.

I have some tables in the database. I want to have a POJO class for each table with fields and corresponding getters and setters. These classes are to be created dynamically. Once these classes are created I should use those setters and getters in other class to get and set the data and return the Java object.

I have seen BCEL, CGLIB and some other open source tools for this, but couldnt find the proper example. Can you help me?

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

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

发布评论

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

评论(1

眼波传意 2024-12-07 04:45:24

您是否查看过专门为此目的而创建的 ORM(对象关系映射)框架? Hibernate 或 Java EE 6 标准 JPA。听起来你正开始走上一条重新发明一些既相当复杂又非常耗时的东西的道路——这从来都不是一个好主意。

更新:回应评论

好吧,我只能说你们正在把自己建设成一个受伤的世界。考虑一下:

  1. 如果您依赖于完全动态的类,您会从其他类中引用这些类和对象吗?通过反思?您将如何跟踪所有创建的类、它们的名称以及它们的 setter 和 getter 的名称?
  2. 当您必须重新启动系统时会发生什么?您将如何重新创建所有这些动态类?
  3. 对于完全动态的数据库,您将如何对其进行性能优化?例如,像索引一样。

我只能强烈建议你重新思考你的架构。动态数据模型一开始就很混乱,而且几乎不可能维护、优化和调试。我见过基于它的系统,但它并不漂亮。 IBM Lotus WCM 是数据模型恐怖的一个典型例子。正确设计和规范化的关系模型在 99,99999999% 的情况下会更好。

将其与动态运行时 ORM 类结合起来将完全不可能维护(和理解)。

Have you looked at any of the ORM (Object Relational Mapping) frameworks out there that have been created for just this purpose? Hibernate or the Java EE 6 standard JPA, for instance. It sounds like you are starting down on a path of re-inventing something that is both pretty complex and very time consuming - never a good idea.

UPDATE: in response to comment

Well, I can only say that you guys are building yourselves into a world of hurt. Consider:

  1. If you rely on completely dynamic classes, you are you going to reference those classes and objects from other classes? Through reflection? And how are you going to keep track of all the created classes, their names and the names of their setters and getters?
  2. What happens when you have to restart your system? How are you going to re-create all those dynamic classes?
  3. With a completely dynamic database, how are you going to be able to do any performance optimization on it? Like indexing, for instance.

I can only strongly advise you to re-think your architecture. Dynamic datamodels are a mess to begin with, and next to impossible to maintain, optimize and debug. I've seen systems based on it and it's not pretty. IBM Lotus WCM is a prime example of data model horror. A properly designed and normalized relational model will be better in 99,99999999% of the cases.

Combining this with a harness of dynamic, run time ORM classes will be utterly impossible to maintain (and understand).

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