org.hibernate.mvn.AbstractHbm2xMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-tools-maven-plugin
Show all versions of hibernate-tools-maven-plugin
Maven plugin to provide hibernate-tools reverse engineering and code/schema generation abilities.
package org.hibernate.mvn;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.tools.ant.BuildException;
import org.hibernate.cfg.reveng.OverrideRepository;
import org.hibernate.cfg.reveng.ReverseEngineeringSettings;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.tool.api.metadata.MetadataDescriptor;
import org.hibernate.tool.api.metadata.MetadataDescriptorFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public abstract class AbstractHbm2xMojo extends AbstractMojo {
// For reveng strategy
/** The default package name to use when mappings for classes are created. */
@Parameter
private String packageName;
/** The name of a property file, e.g. hibernate.properties. */
@Parameter
private File revengFile;
/** The class name of the reverse engineering strategy to use.
* Extend the DefaultReverseEngineeringStrategy and override the corresponding methods, e.g.
* to adapt the generate class names or to provide custom type mappings. */
@Parameter(defaultValue = "org.hibernate.cfg.reveng.DefaultReverseEngineeringStrategy")
private String revengStrategy;
/** If true, tables which are pure many-to-many link tables will be mapped as such.
* A pure many-to-many table is one which primary-key contains exactly two foreign-keys pointing
* to other entity tables and has no other columns. */
@Parameter(defaultValue = "true")
private boolean detectManyToMany;
/** If true, a one-to-one association will be created for each foreignkey found. */
@Parameter(defaultValue = "true")
private boolean detectOneToOne;
/** If true, columns named VERSION or TIMESTAMP with appropriate types will be mapped with the appropriate
* optimistic locking corresponding to <version> or <timestamp>. */
@Parameter(defaultValue = "true")
private boolean detectOptimisticLock;
/** If true, a collection will be mapped for each foreignkey. */
@Parameter(defaultValue = "true")
private boolean createCollectionForForeignKey;
/** If true, a many-to-one association will be created for each foreignkey found. */
@Parameter(defaultValue = "true")
private boolean createManyToOneForForeignKey;
// For configuration
/** The name of a property file, e.g. hibernate.properties. */
@Parameter(defaultValue = "${project.basedir}/src/main/resources/hibernate.properties")
private File propertyFile;
// Not exposed for now
private boolean preferBasicCompositeIds = true;
public void execute() {
getLog().info("Starting " + this.getClass().getSimpleName() + "...");
ReverseEngineeringStrategy strategy = setupReverseEngineeringStrategy();
if (propertyFile.exists()) {
executeExporter(createJdbcDescriptor(strategy, loadPropertiesFile()));
} else {
getLog().info("Property file '" + propertyFile + "' cannot be found, aborting...");
}
getLog().info("Finished " + this.getClass().getSimpleName() + "!");
}
private ReverseEngineeringStrategy setupReverseEngineeringStrategy() {
ReverseEngineeringStrategy strategy;
try {
strategy = ReverseEngineeringStrategy.class.cast(Class.forName(revengStrategy).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException | ClassCastException e) {
throw new BuildException(revengStrategy + " not instanced.", e);
}
if (revengFile != null) {
OverrideRepository override = new OverrideRepository();
override.addFile(revengFile);
strategy = override.getReverseEngineeringStrategy(strategy);
}
ReverseEngineeringSettings settings =
new ReverseEngineeringSettings(strategy)
.setDefaultPackageName(packageName)
.setDetectManyToMany(detectManyToMany)
.setDetectOneToOne(detectOneToOne)
.setDetectOptimisticLock(detectOptimisticLock)
.setCreateCollectionForForeignKey(createCollectionForForeignKey)
.setCreateManyToOneForForeignKey(createManyToOneForForeignKey);
strategy.setSettings(settings);
return strategy;
}
private Properties loadPropertiesFile() {
try (FileInputStream is = new FileInputStream(propertyFile)) {
Properties result = new Properties();
result.load(is);
return result;
} catch (FileNotFoundException e) {
throw new BuildException(propertyFile + " not found.", e);
} catch (IOException e) {
throw new BuildException("Problem while loading " + propertyFile, e);
}
}
private MetadataDescriptor createJdbcDescriptor(ReverseEngineeringStrategy strategy, Properties properties) {
return MetadataDescriptorFactory
.createJdbcDescriptor(
strategy,
properties,
preferBasicCompositeIds);
}
protected abstract void executeExporter(MetadataDescriptor metadataDescriptor);
}