All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.myjavadoc.hibernate4.configuration.JDBCComponentConfiguration Maven / Gradle / Ivy

Go to download

Hibernate 4 maven plugin, run hibernate tools from inside maven builds. Based off Codehaus hibernate3-maven-plugin 2.2. The configuration is the same but the performance is alot better. See source for example database reverse engineering to JPA.

The newest version!
package com.myjavadoc.hibernate4.configuration;

import org.codehaus.plexus.component.annotations.Component;
//import org.apache.maven.plugins.annotations.Component;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.JDBCMetaDataConfiguration;
import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;

import com.myjavadoc.hibernate4.HibernateUtils;
import com.myjavadoc.hibernate4.util.Util;

import org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.OverrideRepository;

import java.io.File;
import java.lang.reflect.Constructor;

@Component( role = ComponentConfiguration.class)
public class JDBCComponentConfiguration
    extends AbstractComponentConfiguration
{
// --------------------- Interface ComponentConfiguration ---------------------

    public String getName()
    {
        return "jdbcconfiguration";
    }

    protected Configuration createConfiguration()
    {
        return new JDBCMetaDataConfiguration();
    }

    protected void doConfiguration( Configuration configuration )
    {
        JDBCMetaDataConfiguration jmdc = (JDBCMetaDataConfiguration) configuration;

        super.doConfiguration( jmdc );

        jmdc.setPreferBasicCompositeIds( getExporterMojo().getComponentProperty( "preferbasiccompositeids", true ) );

        ReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy();

        strategy = loadRevengFile( strategy );

        strategy = loadReverseStrategy( strategy );

        ReverseEngineeringSettings qqsettings = new ReverseEngineeringSettings( strategy );

        String packageName = getExporterMojo().getComponentProperty( "packagename" );
        if ( packageName != null )
        {
            qqsettings.setDefaultPackageName( packageName );
        }

        String detectManyToMany = getExporterMojo().getComponentProperty( "detectmanytomany" );
        if ( "false".equals( detectManyToMany ) )
        {
            qqsettings.setDetectManyToMany( false );
        }

        String detectOptimisticLock = getExporterMojo().getComponentProperty( "detectoptmisticlock" );
        if ( "false".equals( detectOptimisticLock ) )
        {
            qqsettings.setDetectOptimisticLock( false );
        }

        strategy.setSettings( qqsettings );

        jmdc.setReverseEngineeringStrategy( strategy );
        jmdc.readFromJDBC();
    }

    private ReverseEngineeringStrategy loadRevengFile( ReverseEngineeringStrategy delegate )
    {
        String revengFile = getExporterMojo().getComponentProperty( "revengfile" );
        if ( revengFile != null )
        {
            OverrideRepository or = new OverrideRepository();

            File rf = HibernateUtils.getFile( getExporterMojo().getProject().getBasedir(), revengFile );
            if ( rf == null )
            {
                getExporterMojo().getLog().info( "Couldn't find the revengfile in the project. Trying absolute path." );
                rf = HibernateUtils.getFile( null, revengFile );
            }

            if ( rf != null )
            {
                or.addFile( rf );
                return or.getReverseEngineeringStrategy( delegate );
            }
        }
        return delegate;
    }

    private ReverseEngineeringStrategy loadReverseStrategy( ReverseEngineeringStrategy delegate )
    {
        String reverseStrategy = getExporterMojo().getComponentProperty( "reversestrategy" );
        if ( reverseStrategy != null )
        {
            try
            {
                Class clazz = Util.classForName( reverseStrategy );
                //noinspection RedundantArrayCreation
                Constructor constructor = clazz.getConstructor( new Class[]{ReverseEngineeringStrategy.class} );
                //noinspection RedundantArrayCreation
                return (ReverseEngineeringStrategy) constructor.newInstance( new Object[]{delegate} );
            }
            catch ( NoSuchMethodException e )
            {
                try
                {
                    getExporterMojo().getLog().info( "Could not find public " + reverseStrategy +
                        "(ReverseEngineeringStrategy delegate) constructor on ReverseEngineeringStrategy. Trying no-arg version." );
                    Class clazz = Util.classForName( reverseStrategy );
                    ReverseEngineeringStrategy rev = (ReverseEngineeringStrategy) clazz.newInstance();
                    getExporterMojo().getLog().info(
                        "Using non-delegating strategy, thus packagename and revengfile will be ignored." );
                    return rev;
                }
                catch ( Exception eq )
                {
                    getExporterMojo().getLog().error(
                        "Could not create or find " + reverseStrategy + " with default no-arg constructor", eq );
                }
            }
            catch ( Exception e )
            {
                getExporterMojo().getLog().error(
                    "Could not create or find " + reverseStrategy + " with one argument delegate constructor", e );
            }
        }
        return delegate;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy