我发现自己处于类似的情况,在搜索了很长一段时间之后,我发现唯一从命令行支持此功能的工具是Apache OpenJPA。
它需要一些配置才能使其正常工作,但它似乎可以完成工作。它的工作原理如下:
- 使用架构工具从现有数据库架构创建.xml文件。
- (可选)根据自己的喜好编辑生成的 xml(我通过 Gradle 任务运行整个过程,因此我使用 Groovy 从架构中删除一些不需要的表)
- 使用反向映射工具从.xml生成 JPA 实体类(抱歉,没有足够的信誉来发布超过 2 个链接)。此工具还可以采用可选的定制器类,您可以使用该类进一步自定义生成的代码。
尽管这些工具的文档指出,在类路径上具有属性.xml文件足以使它们正常工作,但对我来说,它们只有在我显式指向具有“-properties”arg的文件时才有效。
这里有一些代码片段,可以节省阅读本文的人的时间。这是用OpenJPA 2.3.0测试的:
生成架构xml(在我的情况下从MSSQL数据库,因此驱动程序在类路径上):
java -cp openjpa-all-2.3.0.jar;sqljdbc4.jar org.apache.openjpa.jdbc.schema.SchemaTool -properties openjpa.xml -action reflect -file schema.xml
从 xml 生成实体:
java -cp openjpa-all-2.3.0.jar org.apache.openjpa.jdbc.meta.ReverseMappingTool -properties openjpa.xml -metadata none -annotations true -nullableAsObject true -useGenericCollections true -pkg {your package} -directory {output directory} schema.xml
Sample openjpa.xml (再次,对于 MSSQL DB):
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<persistence-unit name="Gaya STG">
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:sqlserver://{ip}"/>
<property name="openjpa.ConnectionDriverName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="openjpa.ConnectionUserName" value="{username}"/>
<property name="openjpa.ConnectionPassword" value="{pass}"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
包含上述所有任务的示例 build.gradle 文件(同样,对于 MSSQL 数据库):
import java.util.regex.Pattern
apply plugin: 'java'
dependencies {
compile "org.apache.openjpa:openjpa-all:2.3.0"
compile "sqljdbc4:sqljdbc4:sqljdbc4"
}
task cleanSchemaXml(type: Delete) {
delete file("schema.xml")
}
task generateSchemaXml(type: JavaExec, dependsOn: cleanSchemaXml) {
classpath = configurations.compile
main = "org.apache.openjpa.jdbc.schema.SchemaTool"
args "-properties", getPropertiesFile(),
"-action", "reflect",
"-file", "schema.xml"
doFirst {
println "Generating schema.xml..."
}
doLast {
println "Done generating schema.xml."
println "Updating schema.xml..."
updateSchema()
println "Done updating schema.xml."
}
}
task cleanEntities(type: Delete) {
delete fileTree(dir: "{path/to/your/entities}")
}
task generateEntities(type: JavaExec, dependsOn: [cleanEntities, clean, generateSchemaXml, jar]) {
classpath = files(configurations.compile, jar.archivePath) // Add this module's jar to the executed classpath, so we can use the EntityCustomizer (which is assumed to be in this module).
main = "org.apache.openjpa.jdbc.meta.ReverseMappingTool"
args "-metadata", "none",
"-annotations", "true",
"-nullableAsObject", "true",
"-useGenericCollections", "true",
"-properties", getPropertiesFile(),
// "-customizerClass", "{path.to.your.EntityCustomizer}",
"-directory", "{path/to/your/entities}",
"-pkg", "{your.entity.package}",
"schema.xml"
doFirst {
println "Generating entity classes from schema.xml..."
}
doLast {
println "Done generating entity classes."
}
}
private String getPropertiesFile() {
// File is read directly from the file-system, will not work from a Jar.
return file("src/main/resources/openjpa.xml").getAbsolutePath()
}
private void updateSchema() {
// Only this schema will be kept.
final def schemasToKeep = ['dbo']
// These tables will be removed from the .xml
final def tablesToRemove = [
'ReplicationMonitor', 'DDLEvents', 'AuditTrail', 'AuditTrailErrorLog', 'sysdiagrams', 'table_relations',
'tasks_queue', 'tasks_queue_archive',
'.*history' // Remove all tables ending with 'history'.
].collect { Pattern.compile(it) }
final File xmlFile = file('schema.xml')
// Read xml.
final def xml = new XmlParser().parse(xmlFile)
// Remove all unnecessary schemas.
filterSchemas(xml, schemasToKeep)
// Remove all unnecessary tables.
filterTables(xml, tablesToRemove)
// Save updated xml file.
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
}
private void filterSchemas(Node xml, List<String> schemasToKeep) {
final List<Node> removedSchemas = []
xml.each { schema ->
final String name = schema.@name
if (!schemasToKeep.contains(name)) {
println("Removing schema: $name")
removedSchemas += schema
}
}
removedSchemas.each { xml.remove(it) }
}
private void filterTables(Node xml, List<Pattern> tablesToRemove) {
xml.each { schema ->
final List<Node> removedTables = []
schema.each { table ->
final String name = table.@name
if (tablesToRemove.any { it.matcher(name).matches() }) {
println("Removing table: $name")
removedTables += table
}
}
removedTables.each { schema.remove(it) }
}
}