如何动态创建Java POJO类?
我在这个网站上看到过一篇关于动态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否查看过专门为此目的而创建的 ORM(对象关系映射)框架? Hibernate 或 Java EE 6 标准 JPA。听起来你正开始走上一条重新发明一些既相当复杂又非常耗时的东西的道路——这从来都不是一个好主意。
更新:回应评论
好吧,我只能说你们正在把自己建设成一个受伤的世界。考虑一下:
我只能强烈建议你重新思考你的架构。动态数据模型一开始就很混乱,而且几乎不可能维护、优化和调试。我见过基于它的系统,但它并不漂亮。 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:
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).