我正在创建一个从数据库生成java类的任务。我用ant创建了一个gradle任务。奇怪的是,该任务期望revengfile
属性的hibernate.cfg.xml文件。
这是我的任务:
task hibernate{
description "Generates Java Classes from the Database Schema."
group "Hibernate"
doLast{
ant {
def base = "$projectDir/src/main/java"
taskdef(name: 'hibernatetool',
classname: 'org.hibernate.tool.ant.HibernateToolTask',
classpath: configurations.compile.asPath )
hibernatetool( destdir : "$base" ) {
jdbcconfiguration(
propertyfile:"src/main/resources/hibernate.properties", revengfile:"src/main/resources/hibernate.reveng.xml", //expects hibernate.cfg.xml instead
packagename: springProperties["hibernate.generate.package"], detectmanytomany:"true" )
hbm2java(ejb3:true)
}
}
}
}
revengfile
属性会覆盖propertyfile
。我检查了documentation,有hibernate.reveng.xml
的示例,但它实际上只适用于hibernate.cfg.xml
文件。
这是我的hibernate.cfg.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://0.0.0.0:3308/db</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pw</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>
</session-factory>
</hibernate-configuration>
我的hibernate.reveng.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering
SYSTEM "http://hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog=".*" match-schema=".*" match-table=".*" />
<table schema="db" name="string" class="StringEntity"></table>
</hibernate-reverse-engineering>
答案 0 :(得分:0)
对我来说,它似乎是一个错误,因为revengfile
应该指望hibernate.reveng.xml
而不是hibernate.cfg.xml
文件。对于遇到类似问题的任何人,我找到了另一种解决方案来定制逆向工程的配置。 reversestrategy
中的jdbcconfiguration
属性需要一个扩展org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy
的类。
这是我用于逆向工程的Java实现:
public class HibernateReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {
public HibernateReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
super(delegate);
}
@Override
public String tableToClassName(TableIdentifier tableIdentifier) {
return super.tableToClassName(tableIdentifier) + "Entity";
}
}
这是我的蚂蚁任务:
task hibernate{
description "Generates Java Classes from the Database Schema."
group "Hibernate"
doLast{
ant {
def base = "$projectDir/src/main/java"
taskdef(name: 'hibernatetool',
classname: 'org.hibernate.tool.ant.HibernateToolTask',
classpath: configurations.compile.asPath )
hibernatetool( destdir : "$base") {
// needs to be specified in order to find the java class
classpath{
path(location:"${buildDir}/classes/java/main/")
}
jdbcconfiguration(
propertyfile:"src/main/resources/hibernate.properties",
reversestrategy:"mypackage.HibernateReverseEngineeringStrategy",
packagename: springProperties["hibernate.generate.package"]
)
hbm2java(ejb3:true)
}
}
}
}