如何动态创建Java POJO类? [英] How to create Java POJO class dynamically?

查看:1519
本文介绍了如何动态创建Java POJO类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个网站上看到了一个关于动态POJO一代的帖子。我现在有类似的要求。

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

我在数据库中有一些表。我想有一个POJO类为每个表与字段和相应的getters和setters。这些类是动态创建的。创建这些类后,我应该使用其他类中的setter和getter来获取和设置数据并返回Java对象。

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.

我看过BCEL,CGLIB和一些其他开源工具为此,但couldnt找到正确的例子。你能帮助我吗?

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

推荐答案

你看过任何已经创建的ORM(对象关系映射)框架只是为了这个目的? Hibernate 或Java EE 6标准 JPA 。这听起来像是你正在重新创造一个非常复杂和非常耗时的路径 - 从来不是一个好主意。

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.

更新:响应评论

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

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


  1. 如果你依赖完全动态的类,你是要引用其他类的类和对象吗?通过反思?

  2. 您必须重新启动系统时会发生什么?您将如何重新创建所有这些动态类?

  3. 使用完全动态的数据库,您将如何对其进行任何性能优化?例如索引。

我只能强烈建议你重新考虑你的架构。动态数据模型是一个糟糕的开始,旁边不可能维护,优化和调试。我看过基于它的系统,它不漂亮。 IBM Lotus WCM是数据模型恐怖的一个主要例子。

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.

将此与动态运行时ORM类的组合完全是不可能的维持(和理解)。

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

这篇关于如何动态创建Java POJO类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆