Java 有数据库建模库吗?
有谁知道有一个 Java 库可以为分析和操作任意关系数据库模式提供有用的抽象吗? 我正在考虑一些可以做类似的事情
LibraryClass dbLib = ...;
DbSchema schema = dbLib.getSchema("my_schema");
List<DbTable> tables = schema.getTables();
,甚至
DbTable myTable = ...
for(DbColumn col : myTable.getColumns()){
... = col.getType();
}
可以操作表之类的事情,
myTable.addColumn(
new DbColumn("my_new_column", Type.UNSIGNED_INTEGER);
);
DbColumn myColumn = ...
myTable.removeColumn(myColumn);
大多数数据库建模工具都会在内部有这样的抽象,但是Java中有一个我可以使用的东西,还是我必须自己推出一个?
Does anyone know of a Java library that provides a useful abstraction for analyzing and manipulating arbitrary relational database schemata? I'm thinking of something that could do things like
LibraryClass dbLib = ...;
DbSchema schema = dbLib.getSchema("my_schema");
List<DbTable> tables = schema.getTables();
and
DbTable myTable = ...
for(DbColumn col : myTable.getColumns()){
... = col.getType();
}
or even manipulate tables like
myTable.addColumn(
new DbColumn("my_new_column", Type.UNSIGNED_INTEGER);
);
DbColumn myColumn = ...
myTable.removeColumn(myColumn);
Most Database modeling tools will have such an abstraction internally, but is there one in Java that I can use, or will I have to roll my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JDBC本身就有这样的抽象。 查看 java.sql.DatabaseMetaData。 然而,这是标准的可选部分,它是否实现取决于您使用的 JDBC 驱动程序。
JDBC itself has such an abstraction. Look at java.sql.DatabaseMetaData. However, this is an optional part of the standard and it depends on the JDBC driver you are using wether it is implemented or not.
DdlUtils 有您正在寻找的东西。 您可以从 XML(Torque 格式)或实时数据库读取/写入模式,甚至可以用纯 Java 定义数据库模式。 更好的是,阅读在线文档,它非常好。
DdlUtils has what you're looking for. You can read/write schemas to/from XML (in Torque format) or a live database, or even define the database schema in pure Java. Better yet, read the on-line doco, it's quite good.
我已经很多年没有使用它了,但 Hibernate 在构建时曾经有用于操作数据模型的工具时间。 Hibernate 还具有方言这一概念,如果您的目标是多个数据库供应商,这将很有帮助。
I haven't used it in years but Hibernate used to have tools for manipulating data models at build time. Hibernate also has the notion dialects which would be helpful if you're targeting more than one database vendor.
当我在 MetaMatrix 时,我们使用 EMF(Eclipse 建模框架)构建了这样一个东西,我们在 UML 中创建了一个代表性元模型,然后从代码生成它。 元建模的好处是,如果你做得好,你可以跨元模型进行互操作,前提是你针对元元模型做出了正确的选择。
我们还有一个导入器,可以从 JDBC API 导入元数据并创建相应的等效模型对象(数据库、表、列、键等)。
这段代码可能有一天会开源,因为它们被 JBoss 收购了,但我认为还没有。
When I was at MetaMatrix, we built such a thing using EMF (Eclipse Modeling Framework) where we created a representational meta-model in UML, then generated it from code. The nice thing about metamodeling is that if you do it well, you can interoperate things across metamodels, provided you have made good choices against the meta-meta-model.
We also had an importer that would import metadata from the JDBC API and create the appropriate equivalent model objects (database, table, column, keys, etc).
This code might be open source some day since they got bought by JBoss but I don't think it is yet.