java 如何从数据库生成模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6321584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate model from database
提问by
I have an existing database. I need to generate the model classes in Java from it. Are there any tool/library that will allow me to do this. It will be of great help if it can emulate the entity relationships in the database into the model classes as well.
我有一个现有的数据库。我需要从中生成 Java 中的模型类。是否有任何工具/库可以让我做到这一点。如果它能够将数据库中的实体关系也模拟到模型类中,那将是非常有帮助的。
It is acceptable if the tool/library works with only one database vendor. I will create a database there and then generate the model.
如果工具/库只与一个数据库供应商合作,这是可以接受的。我将在那里创建一个数据库,然后生成模型。
Thanks in advance.
提前致谢。
EDIT :I will probably use Hibernateas the ORM framework if I manage to generate the model.
编辑:如果我设法生成模型,我可能会使用Hibernate作为 ORM 框架。
回答by Vineet Reynolds
The Hibernate Tools project(available as an Eclipse plug-in, as well as an Ant task) allows for "reverse-engineering" of database schemas into appropriate entity classes.
在休眠的工具项目(可以作为Eclipse插件,以及一个Ant任务)允许数据库架构的“逆向工程”为适当的实体类。
This project is also available in the JBoss Tools project.
这个项目也可以在JBoss Tools 项目中找到。
The facility allows for reverse-engineering of the database metadatainto a Hibernate configuration file. All artifacts (including the .java files) are generated from this config file.
该工具允许将数据库元数据逆向工程到 Hibernate 配置文件中。所有工件(包括 .java 文件)都是从这个配置文件生成的。
You can control the nature of the reverse engineering processto suit your database structure. In other words, you can specify the schemas that you wish to have the tool reverse-engineer. You could also override the JDBC type mapping, apart from limiting the reverse-engineering process to a selected set of tables.
您可以控制逆向工程过程的性质以适合您的数据库结构。换句话说,您可以指定您希望工具逆向工程的模式。除了将逆向工程过程限制为一组选定的表之外,您还可以覆盖 JDBC 类型映射。
Obligatory link:
必填链接:
- Screencast on Reverse engineering and code generation
- 关于逆向工程和代码生成的截屏视频
回答by lgu
Telosysdoes exactly this job
Telosys正是这样做的
Let's have a look : http://www.telosys.org/
让我们来看看:http: //www.telosys.org/
回答by xflorian
Minuteproject 4 JPA2 (http://minuteproject.wikispaces.com/JPA2) track does this task. Minuteproject can be run from console or command line.
Minuteproject 4 JPA2 ( http://minuteproject.wikispaces.com/JPA2) 轨道执行此任务。Minuteproject 可以从控制台或命令行运行。
You can have a quick result by generating from the console where generate a maven project containing the JPA2 mapping classes in java or groovy. If you use the command line then you need to fill an xml file that can contain additional customisation of your generated code such as packaging, enum, aliasing etc...
您可以通过从控制台生成一个快速的结果,其中生成一个包含 java 或 groovy 中的 JPA2 映射类的 maven 项目。如果您使用命令行,那么您需要填写一个 xml 文件,该文件可以包含对生成的代码的其他自定义,例如打包、枚举、别名等...
You can also try other track built on top JPA2 such as DAO with spring or EJB; REST; front end with Primefaces or Openxava; etc...
您也可以尝试其他建立在顶级 JPA2 之上的轨道,例如带有 spring 或 EJB 的 DAO;休息; 前端使用 Primefaces 或 Openxava;等等...
回答by Danny D'Amours
Hibernate has an Eclipse plugin Hibernate Tools http://www.hibernate.org/subprojects/tools.htmlthat has reverse engineering capabilities.
Hibernate 有一个 Eclipse 插件 Hibernate Tools http://www.hibernate.org/subprojects/tools.html,它具有逆向工程功能。
See: http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html/plugins.html#refeng_codegenfor more details on how to run and customize the reverse engineering process.
请参阅:http: //docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html/plugins.html#refeng_codegen有关如何运行和自定义逆向工程过程的更多详细信息。