10 Replies Latest reply on Oct 10, 2011 11:57 AM by Adam Warski

    EnversHibernateToolTask

    Adriano Saturno Newbie

      I'm still trying to generate envers schema with ant, but failing...

       

      He have a task that "overrides" schemaexport this way:

       

      final EnversHibernateToolTask enversHibernateToolTask = new EnversHibernateToolTask();

      enversHibernateToolTask.setProject(project);

       

      final ConfigurationTask configurationTask = enversHibernateToolTask.createConfiguration();

      configurationTask.setProject(project);

      configurationTask.setNamingStrategy(namingStrategy);

      String randomUUID = UUID.randomUUID().toString();

      File tmpFile = File.createTempFile(randomUUID.substring(randomUUID.length() - 10, randomUUID.length()- 1), ".tmp");

       

      Files.write("hibernate.dialect="+dialect , tmpFile, Charset.defaultCharset());

      configurationTask.setPropertyFile(tmpFile);

       

      Configuration configuration = configurationTask.getConfiguration();

      String[] configFiles = configurationFileNames.split(",");

      for (int i = 0; i < configFiles.length; i++) {

                configuration.configure(configFiles[i]);

      }

       

      final Hbm2DDLExporterTask exporterTask = new Hbm2DDLExporterTask(enversHibernateToolTask);

      exporterTask.setDrop(drop);

      exporterTask.setCreate(create);

      exporterTask.setExport(export);

      exporterTask.setOutputFileName(outputFileName);

      exporterTask.setDelimiter(delimiter != null ? delimiter : DELIMITER);

      exporterTask.setFormat(FORMAT);

      exporterTask.execute();

       

      I bet that I'm missing something.

        • 1. Re: EnversHibernateToolTask
          Adriano Saturno Newbie

          Ops, I forgot the xml ant file...

           

          <target name="schemaexport" description="Gera ddls do amplis baseados nas parametrizações do hibernate">

            <schemaExportTask configurationfiles="hibernate.cfg.xml,patrimonio.mapeamento.xml"

              includes="**/*.hbm.xml"

              outputfilename="${PATH}/${ddl-dir}/myproject-ddl.sql"

              namingstrategy="br.com.ymf.myproject.base.hibernate.HibernateNamingDictionary"

              drop="false"

              create="true"

              export="false"

              delimiter=";" />   

            <DDLSplitterTask srcPath="${PATH}/${ddl-dir}/myproject-ddl.sql" />

          </target>

           

          <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.EnversHibernateToolTask">

            <classpath>

              <fileset dir="${library-dir}">

                <include name="**/*.jar" />

              </fileset>

            </classpath>

          </taskdef>

           

          <taskdef name="DDLSplitterTask" classname="br.com.ymf.myproject.tools.data.DDLSplitterTask">

            <classpath id="tools" path="../MY_PROJECT/target/classes" />

            <classpath>

              <fileset dir="${library-dir}">

                <include name="**/*.jar" />

              </fileset>

            </classpath>

          </taskdef>

           

          <taskdef name="schemaExportTask" classpathref="toolsClassPath"          

            classname="br.com.ymf.myproject.tools.build.hibernate.SchemaExportTask"/>

          • 2. Re: EnversHibernateToolTask
            Adam Warski Master

            Could you debug and check if the AuditConfiguration.getFor get invoked? I'm not so fluent in the ant config so I can't spot any errors right now

             

            Adam

            • 3. Re: EnversHibernateToolTask
              Adriano Saturno Newbie

              Yep, I debugged and ConfigurationTaskWithEnvers is invoking AuditConfiguration.getFor(configuration).

               

              BTW I'm using hibernate-envers-3.6.0.Final.jar

               

              While debugging I realize that RevisionInfoConfigurationResult.configure can't find any classes in the 1st line:

               

                      Iterator<PersistentClass> classes = (Iterator<PersistentClass>) cfg.getClassMappings();

               

              Maybe the problem is that I'm not using org.hibernate.tool.hbm2ddl.SchemaExport.

              • 4. Re: EnversHibernateToolTask
                Adriano Saturno Newbie

                Trying to isolate the issue, I started to use the SchemeExport as recommended in Chapter 6: Generating schema with Ant

                 

                So I ignored my SchemaExport implementation and tried to use the hibernate tool's one:

                 

                  <target name="schemaexport" description="Exports a generated schema to DB and file">

                    <taskdef name="hibernatetool"

                      classname="org.hibernate.tool.ant.EnversHibernateToolTask"

                      classpathref="toolsClassPath"/>

                    <hibernatetool destdir=".">

                      <classpath>

                        <fileset dir="${library-dir}" >

                          <include name="**/*.hbm.xml" />

                        </fileset>

                      </classpath>

                      <configuration configurationfile="hibernate.cfg.xml"/>

                      <hbm2ddl

                        drop="false"

                        create="true"

                        export="false"

                        outputfilename="ddl.sql"

                        delimiter=";"

                        format="true"/>

                    </hibernatetool>

                  </target>

                 

                After some debuggin, the same problem, no audit tables created.

                 

                ConfigurationTaskWithEnvers is still invoking AuditConfiguration.getFor(configuration). Still the same empty classes on RevisionInfoConfigurationResult.configure, line  Iterator<PersistentClass> classes = (Iterator<PersistentClass>) cfg.getClassMappings();

                • 5. Re: EnversHibernateToolTask
                  Adriano Saturno Newbie

                  Well, I finally have some answers from my investigation.

                   

                  Here is the catch:

                   

                  final AnnotationConfigurationTaskWithEnvers configurationTask = (AnnotationConfigurationTaskWithEnvers) enversHibernateToolTask

                                                          .createAnnotationConfiguration();

                   

                  yes,

                  createAnnotationConfiguration(), not createConfiguration()

                   

                  and:

                   

                  configurationTask.setConfigurationFile(new File("PROJECT_DIR\\src\\main\\resources\\hibernate.cfg.xml"));

                   

                  and voilà.

                   

                  But, if you, like me, have more than one hibernate.cfg.xml, you have a big problem. There´s no way to use more than one configuration file.

                   

                  configurationTask.addConfiguredFileSet() doesn't work, because of this:

                   

                  public class AnnotationConfigurationTaskWithEnvers extends AnnotationConfigurationTask {

                      protected void doConfiguration(Configuration configuration) {

                   

                  /* Here we should have something like this:

                  for (int i = 0; i < configFiles.length; i++) {

                            configuration.configure(configFiles[i]);

                  }

                  configFiles represents all your hibernate.cfg.xml files

                  */

                                      super.doConfiguration(configuration);

                                      configuration.buildMappings();

                                      AuditConfiguration.getFor(configuration);

                      }

                  }

                  • 6. Re: EnversHibernateToolTask
                    Adam Warski Master

                    Hmm, I thought that the super.doConfiguration call will take care of things like that.

                    So you managed to get your code to work? What modifiactions exactly where needed?
                    I'd be happy to include any patches that would be necessary so that others don't have the same problem.

                     

                    Adam

                    • 7. Re: EnversHibernateToolTask
                      Adriano Saturno Newbie

                      Extracted from super.doConfiguration:

                       

                      if (configurationFile != null) configuration.configure( configurationFile );

                       

                      So, before invoking, we must have a configurationFile.

                       

                      If we use addConfiguredFileSet(), this strategy doens't work, because we have to invoke configure() after involking addConfiguredFileSet(). But we can't do this, because we need to invoke getConfiguration() to get the configuration, and getConfiguration() do this:

                       

                                          if(cfg==null) {

                                                    cfg = createConfiguration();

                                                    doConfiguration(cfg);

                                                    cfg.buildMappings(); // needed otherwise not all assocations are made!

                                          }

                                          return cfg;

                       

                      at this time, cfg is null, but we have no configuration files.

                       

                      If we invoke configure() right after this, to use the configuration files, cfg is not null, so, doConfiguration is not called anymore.

                      • 8. Re: EnversHibernateToolTask
                        Adam Warski Master

                        But if call getConfiguration(), then add the files, then call AuditConfiguration.getFor(cfg), and then do the schema export, it works?

                         

                        Generally this looks like a chicken-egg problem

                         

                        Adam

                        • 9. Re: EnversHibernateToolTask
                          Adriano Saturno Newbie

                          Yep, a chicken-egg problem.

                           

                          What would be the side effects of removing the  if (verCfg == null) check of the AuditConfiguration.getFor(Configuration cfg)?

                           

                            public synchronized static AuditConfiguration getFor(Configuration cfg) {

                              AuditConfiguration verCfg = cfgs.get(cfg);

                           

                              if (verCfg == null) {

                                verCfg = new AuditConfiguration(cfg);

                                cfgs.put(cfg, verCfg);

                               

                                cfg.buildMappings();

                              }

                           

                              return verCfg;

                            }

                          • 10. Re: EnversHibernateToolTask
                            Adam Warski Master

                            Well this checks if a configuraiton was built already. Generally, it whould be called once for a complete Hibernate Configuration object.


                            Adam