0

I am creating a task for generating java classes from the database. I created a gradle task using ant. The strange this is that the task is expecting a hibernate.cfg.xml file for revengfile property.

This is my gradle task:

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)
        } 
    }
  }
}

The revengfile property overrides propertyfile. I checked the documentation, there is an example with hibernate.reveng.xml, but it actually only works with hibernate.cfg.xml file.

This is my hibernate.cfg.xml file:

<?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>

My hibernate.reveng.xml file:

<?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>
NirIzr
  • 3,131
  • 2
  • 30
  • 49
Merve Sahin
  • 1,008
  • 2
  • 14
  • 26

1 Answers1

0

To me it seems to be a bug, since the revengfile should expect a hibernate.reveng.xml instead of an hibernate.cfg.xml file. For anyone who encounters a similar issue I found an alternative solution to customize the configuration for reverse engineering. The reversestrategy property in jdbcconfiguration expects a class that extends org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy.

This is my Java implementation for reverse engineering:

public class HibernateReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

public HibernateReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
    super(delegate);
}

@Override
public String tableToClassName(TableIdentifier tableIdentifier) {
    return super.tableToClassName(tableIdentifier) + "Entity";
}

}

And this is my gradle ant task:

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)
        } 
    }
}

}
Merve Sahin
  • 1,008
  • 2
  • 14
  • 26