HHH-9467 : Cannot enable NamingStrategyDelegator implementations using standalone schema tools
This commit is contained in:
parent
1b9cd19ca7
commit
4bf3b162ad
|
@ -46,6 +46,7 @@ import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
|
||||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||||
|
@ -504,6 +505,8 @@ public class SchemaExport {
|
||||||
String propFile = null;
|
String propFile = null;
|
||||||
boolean format = false;
|
boolean format = false;
|
||||||
String delim = null;
|
String delim = null;
|
||||||
|
boolean hasNaming = false;
|
||||||
|
boolean hasNamingDelegator = false;
|
||||||
|
|
||||||
for ( int i = 0; i < args.length; i++ ) {
|
for ( int i = 0; i < args.length; i++ ) {
|
||||||
if ( args[i].startsWith( "--" ) ) {
|
if ( args[i].startsWith( "--" ) ) {
|
||||||
|
@ -541,11 +544,21 @@ public class SchemaExport {
|
||||||
cfg.configure( args[i].substring( 9 ) );
|
cfg.configure( args[i].substring( 9 ) );
|
||||||
}
|
}
|
||||||
else if ( args[i].startsWith( "--naming=" ) ) {
|
else if ( args[i].startsWith( "--naming=" ) ) {
|
||||||
|
hasNaming = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) )
|
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) )
|
||||||
.newInstance()
|
.newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
|
||||||
|
hasNamingDelegator = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
|
||||||
|
.newInstance()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
String filename = args[i];
|
String filename = args[i];
|
||||||
|
@ -592,6 +605,12 @@ public class SchemaExport {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
|
||||||
|
if ( namingSpecified && namingDelegatorSpecified ) {
|
||||||
|
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a List of all Exceptions which occured during the export.
|
* Returns a List of all Exceptions which occured during the export.
|
||||||
*
|
*
|
||||||
|
|
|
@ -36,6 +36,7 @@ import java.util.Properties;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.internal.util.ReflectHelper;
|
import org.hibernate.internal.util.ReflectHelper;
|
||||||
import org.hibernate.internal.util.collections.ArrayHelper;
|
import org.hibernate.internal.util.collections.ArrayHelper;
|
||||||
|
|
||||||
|
@ -82,6 +83,7 @@ public class SchemaExportTask extends MatchingTask {
|
||||||
private boolean haltOnError;
|
private boolean haltOnError;
|
||||||
private String delimiter;
|
private String delimiter;
|
||||||
private String namingStrategy;
|
private String namingStrategy;
|
||||||
|
private String namingStrategyDelegator;
|
||||||
|
|
||||||
public void addFileset(FileSet set) {
|
public void addFileset(FileSet set) {
|
||||||
fileSets.add(set);
|
fileSets.add(set);
|
||||||
|
@ -207,11 +209,20 @@ public class SchemaExportTask extends MatchingTask {
|
||||||
|
|
||||||
private Configuration getConfiguration() throws Exception {
|
private Configuration getConfiguration() throws Exception {
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
|
if ( namingStrategy != null && namingStrategyDelegator != null ) {
|
||||||
|
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
|
||||||
|
}
|
||||||
if (namingStrategy!=null) {
|
if (namingStrategy!=null) {
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
|
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( namingStrategyDelegator != null) {
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
( NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
if (configurationFile != null) {
|
if (configurationFile != null) {
|
||||||
cfg.configure( configurationFile );
|
cfg.configure( configurationFile );
|
||||||
}
|
}
|
||||||
|
@ -249,6 +260,10 @@ public class SchemaExportTask extends MatchingTask {
|
||||||
this.namingStrategy = namingStrategy;
|
this.namingStrategy = namingStrategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
|
||||||
|
this.namingStrategyDelegator = namingStrategyDelegator;
|
||||||
|
}
|
||||||
|
|
||||||
public void setHaltonerror(boolean haltOnError) {
|
public void setHaltonerror(boolean haltOnError) {
|
||||||
this.haltOnError = haltOnError;
|
this.haltOnError = haltOnError;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
import org.hibernate.engine.jdbc.internal.FormatStyle;
|
||||||
import org.hibernate.engine.jdbc.internal.Formatter;
|
import org.hibernate.engine.jdbc.internal.Formatter;
|
||||||
|
@ -121,6 +122,9 @@ public class SchemaUpdate {
|
||||||
// If true then execute db updates, otherwise just generate and display updates
|
// If true then execute db updates, otherwise just generate and display updates
|
||||||
boolean doUpdate = true;
|
boolean doUpdate = true;
|
||||||
String propFile = null;
|
String propFile = null;
|
||||||
|
String outFile = null;
|
||||||
|
boolean hasNaming = false;
|
||||||
|
boolean hasNamingDelegator = false;
|
||||||
|
|
||||||
for ( int i = 0; i < args.length; i++ ) {
|
for ( int i = 0; i < args.length; i++ ) {
|
||||||
if ( args[i].startsWith( "--" ) ) {
|
if ( args[i].startsWith( "--" ) ) {
|
||||||
|
@ -136,11 +140,24 @@ public class SchemaUpdate {
|
||||||
else if ( args[i].startsWith( "--text" ) ) {
|
else if ( args[i].startsWith( "--text" ) ) {
|
||||||
doUpdate = false;
|
doUpdate = false;
|
||||||
}
|
}
|
||||||
|
else if ( args[i].startsWith( "--output=" ) ) {
|
||||||
|
outFile = args[i].substring( 9 );
|
||||||
|
}
|
||||||
else if ( args[i].startsWith( "--naming=" ) ) {
|
else if ( args[i].startsWith( "--naming=" ) ) {
|
||||||
|
hasNaming = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
|
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
|
||||||
|
hasNamingDelegator = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
|
||||||
|
.newInstance()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cfg.addFile( args[i] );
|
cfg.addFile( args[i] );
|
||||||
|
@ -157,7 +174,9 @@ public class SchemaUpdate {
|
||||||
|
|
||||||
StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
|
StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
|
||||||
try {
|
try {
|
||||||
new SchemaUpdate( serviceRegistry, cfg ).execute( script, doUpdate );
|
final SchemaUpdate schemaUpdate = new SchemaUpdate( serviceRegistry, cfg );
|
||||||
|
schemaUpdate.setOutputFile( outFile );
|
||||||
|
schemaUpdate.execute( script, doUpdate );
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
serviceRegistry.destroy();
|
serviceRegistry.destroy();
|
||||||
|
@ -169,6 +188,12 @@ public class SchemaUpdate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
|
||||||
|
if ( namingSpecified && namingDelegatorSpecified ) {
|
||||||
|
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the schema updates
|
* Execute the schema updates
|
||||||
*
|
*
|
||||||
|
|
|
@ -36,6 +36,7 @@ import java.util.Properties;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.internal.util.ReflectHelper;
|
import org.hibernate.internal.util.ReflectHelper;
|
||||||
import org.hibernate.internal.util.collections.ArrayHelper;
|
import org.hibernate.internal.util.collections.ArrayHelper;
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ public class SchemaUpdateTask extends MatchingTask {
|
||||||
private boolean haltOnError;
|
private boolean haltOnError;
|
||||||
private String delimiter;
|
private String delimiter;
|
||||||
private String namingStrategy;
|
private String namingStrategy;
|
||||||
|
private String namingStrategyDelegator;
|
||||||
|
|
||||||
public void addFileset(FileSet set) {
|
public void addFileset(FileSet set) {
|
||||||
fileSets.add(set);
|
fileSets.add(set);
|
||||||
|
@ -170,11 +171,20 @@ public class SchemaUpdateTask extends MatchingTask {
|
||||||
|
|
||||||
private Configuration getConfiguration() throws Exception {
|
private Configuration getConfiguration() throws Exception {
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
|
if ( namingStrategy != null && namingStrategyDelegator != null ) {
|
||||||
|
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
|
||||||
|
}
|
||||||
if (namingStrategy!=null) {
|
if (namingStrategy!=null) {
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
(NamingStrategy) ReflectHelper.classForName( namingStrategy ).newInstance()
|
(NamingStrategy) ReflectHelper.classForName( namingStrategy ).newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( namingStrategyDelegator != null) {
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
(NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
if (configurationFile!=null) {
|
if (configurationFile!=null) {
|
||||||
cfg.configure( configurationFile );
|
cfg.configure( configurationFile );
|
||||||
}
|
}
|
||||||
|
@ -213,6 +223,10 @@ public class SchemaUpdateTask extends MatchingTask {
|
||||||
this.namingStrategy = namingStrategy;
|
this.namingStrategy = namingStrategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
|
||||||
|
this.namingStrategyDelegator = namingStrategyDelegator;
|
||||||
|
}
|
||||||
|
|
||||||
public File getOutputFile() {
|
public File getOutputFile() {
|
||||||
return outputFile;
|
return outputFile;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,6 +34,7 @@ import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.Environment;
|
import org.hibernate.cfg.Environment;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.dialect.Dialect;
|
import org.hibernate.dialect.Dialect;
|
||||||
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
import org.hibernate.engine.jdbc.spi.JdbcServices;
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
|
@ -87,6 +88,8 @@ public class SchemaValidator {
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
|
|
||||||
String propFile = null;
|
String propFile = null;
|
||||||
|
boolean hasNaming = false;
|
||||||
|
boolean hasNamingDelegator = false;
|
||||||
|
|
||||||
for ( int i = 0; i < args.length; i++ ) {
|
for ( int i = 0; i < args.length; i++ ) {
|
||||||
if ( args[i].startsWith( "--" ) ) {
|
if ( args[i].startsWith( "--" ) ) {
|
||||||
|
@ -97,10 +100,21 @@ public class SchemaValidator {
|
||||||
cfg.configure( args[i].substring( 9 ) );
|
cfg.configure( args[i].substring( 9 ) );
|
||||||
}
|
}
|
||||||
else if ( args[i].startsWith( "--naming=" ) ) {
|
else if ( args[i].startsWith( "--naming=" ) ) {
|
||||||
|
hasNaming = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
|
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
|
||||||
|
hasNamingDelegator = true;
|
||||||
|
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
|
||||||
|
.newInstance()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cfg.addFile( args[i] );
|
cfg.addFile( args[i] );
|
||||||
|
@ -129,6 +143,12 @@ public class SchemaValidator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
|
||||||
|
if ( namingSpecified && namingDelegatorSpecified ) {
|
||||||
|
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the validations.
|
* Perform the validations.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -36,6 +36,7 @@ import java.util.Properties;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.cfg.NamingStrategy;
|
import org.hibernate.cfg.NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.NamingStrategyDelegator;
|
||||||
import org.hibernate.internal.util.ReflectHelper;
|
import org.hibernate.internal.util.ReflectHelper;
|
||||||
import org.hibernate.internal.util.collections.ArrayHelper;
|
import org.hibernate.internal.util.collections.ArrayHelper;
|
||||||
|
|
||||||
|
@ -70,6 +71,7 @@ public class SchemaValidatorTask extends MatchingTask {
|
||||||
private File propertiesFile;
|
private File propertiesFile;
|
||||||
private File configurationFile;
|
private File configurationFile;
|
||||||
private String namingStrategy;
|
private String namingStrategy;
|
||||||
|
private String namingStrategyDelegator;
|
||||||
|
|
||||||
public void addFileset(FileSet set) {
|
public void addFileset(FileSet set) {
|
||||||
fileSets.add(set);
|
fileSets.add(set);
|
||||||
|
@ -143,11 +145,20 @@ public class SchemaValidatorTask extends MatchingTask {
|
||||||
|
|
||||||
private Configuration getConfiguration() throws Exception {
|
private Configuration getConfiguration() throws Exception {
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
|
if ( namingStrategy != null && namingStrategyDelegator != null ) {
|
||||||
|
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
|
||||||
|
}
|
||||||
if (namingStrategy!=null) {
|
if (namingStrategy!=null) {
|
||||||
cfg.setNamingStrategy(
|
cfg.setNamingStrategy(
|
||||||
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
|
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
else if ( namingStrategyDelegator != null) {
|
||||||
|
cfg.setNamingStrategyDelegator(
|
||||||
|
(NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
if (configurationFile!=null) {
|
if (configurationFile!=null) {
|
||||||
cfg.configure( configurationFile );
|
cfg.configure( configurationFile );
|
||||||
}
|
}
|
||||||
|
@ -182,4 +193,7 @@ public class SchemaValidatorTask extends MatchingTask {
|
||||||
this.namingStrategy = namingStrategy;
|
this.namingStrategy = namingStrategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
|
||||||
|
this.namingStrategyDelegator = namingStrategyDelegator;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.schematoolsnaming;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity(name="Mtx")
|
||||||
|
public class Matrix {
|
||||||
|
@Id
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Set<Integer> mvalues = new HashSet<Integer>();
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,218 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.schematoolsnaming;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.hibernate.cfg.EJB3NamingStrategy;
|
||||||
|
import org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator;
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||||
|
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||||
|
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
|
||||||
|
import org.hibernate.tool.hbm2ddl.SchemaValidator;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that using {@code --namingdelegator=<naming_delegator_class_name>} works when
|
||||||
|
* using standalone schema tools.
|
||||||
|
*
|
||||||
|
* @author Gail Badner
|
||||||
|
*/
|
||||||
|
@TestForIssue(jiraKey = "HHH-9467")
|
||||||
|
public class StandaloneSchemaToolsNamingTest extends BaseUnitTestCase {
|
||||||
|
private final java.io.File OUTFILE = new java.io.File( "StandaloneSchemaToolsNamingTest.out" );
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDefaultNamingStrategyDelegator() {
|
||||||
|
// using the default NamingStrategyDelegator, the collection table name should be "Matrix_mvalues"
|
||||||
|
doValidTest(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml"
|
||||||
|
},
|
||||||
|
"Matrix_mvalues"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testImprovedNamingStrategyDelegator() {
|
||||||
|
// using ImprovedNamingStrategyDelegator, the collection table name should be "Mtx_mvalues"
|
||||||
|
doValidTest(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
|
||||||
|
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
|
||||||
|
},
|
||||||
|
"Mtx_mvalues"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEJB3NamingStrategy() {
|
||||||
|
// using EJB3NamingStrategy should be the same as using the default NamingStrategyDelegator, so
|
||||||
|
// the collection table name should be "Matrix_mvalues"
|
||||||
|
doValidTest(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
|
||||||
|
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
|
||||||
|
},
|
||||||
|
"Matrix_mvalues"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSchemaExportNamingAndNamingDelegatorSpecified() {
|
||||||
|
OUTFILE.delete();
|
||||||
|
// --naming and --namingdelegator cannot be used together.
|
||||||
|
// when SchemaExport is used standalone, an exception should be logged and OUTFILE should not exist.
|
||||||
|
SchemaExport.main(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
|
||||||
|
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
|
||||||
|
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assertFalse( OUTFILE.exists() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSchemaUpdateNamingAndNamingDelegatorSpecified() {
|
||||||
|
OUTFILE.delete();
|
||||||
|
// --naming and --namingdelegator cannot be used together.
|
||||||
|
// when SchemaUpdate is used standalone, an exception should be logged and OUTFILE should not exist.
|
||||||
|
SchemaUpdate.main(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
|
||||||
|
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
|
||||||
|
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assertFalse( OUTFILE.exists() );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSchemaValidatorNamingAndNamingDelegatorSpecified() {
|
||||||
|
// --naming and --namingdelegator cannot be used together.
|
||||||
|
// when SchemaValidator is used standalone, an exception should be logged.
|
||||||
|
SchemaValidator.main(
|
||||||
|
new String[] {
|
||||||
|
"--output=" + OUTFILE.getName(),
|
||||||
|
"--config=org/hibernate/test/schematoolsnaming/hibernate.cfg.xml",
|
||||||
|
"--naming=" + EJB3NamingStrategy.INSTANCE.getClass().getName(),
|
||||||
|
"--namingdelegator=" + ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE.getClass().getName()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
private void doValidTest(String args[], String collectionTableNameExpected) {
|
||||||
|
OUTFILE.delete();
|
||||||
|
SchemaExport.main( args );
|
||||||
|
assertTrue( OUTFILE.exists() );
|
||||||
|
assertFileContainsTrimmedLineStartingWith( OUTFILE, collectionTableNameExpected );
|
||||||
|
|
||||||
|
SchemaValidator.main( args );
|
||||||
|
|
||||||
|
OUTFILE.delete();
|
||||||
|
// SchemaUpdate should result in an empty file because there should be nothing to update.
|
||||||
|
SchemaUpdate.main( args );
|
||||||
|
assertFileIsEmpty( OUTFILE );
|
||||||
|
|
||||||
|
dropSchema( args );
|
||||||
|
|
||||||
|
OUTFILE.delete();
|
||||||
|
// since schema was dropped, OUTFILE should now contain references to collectionTableNameExpected.
|
||||||
|
SchemaUpdate.main( args );
|
||||||
|
assertTrue( OUTFILE.exists() );
|
||||||
|
assertFileContainsTrimmedLineStartingWith( OUTFILE, collectionTableNameExpected );
|
||||||
|
|
||||||
|
SchemaValidator.main( args );
|
||||||
|
|
||||||
|
dropSchema( args );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertFileContainsTrimmedLineStartingWith(java.io.File file, String text) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader input = new BufferedReader( new FileReader( file ) );
|
||||||
|
String line;
|
||||||
|
/*
|
||||||
|
* readLine is a bit quirky :
|
||||||
|
* it returns the content of a line MINUS the newline.
|
||||||
|
* it returns null only for the END of the stream.
|
||||||
|
* it returns an empty String if two newlines appear in a row.
|
||||||
|
*/
|
||||||
|
while ( ( line = input.readLine() ) != null ) {
|
||||||
|
final String trimmedLine = line.trim();
|
||||||
|
if ( trimmedLine.contains( text ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch ( IOException e ) {
|
||||||
|
fail(
|
||||||
|
String.format(
|
||||||
|
"Failed due to IOException checking file [%s] for a line containing: [%s]",
|
||||||
|
file.getName(),
|
||||||
|
text
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
fail(
|
||||||
|
String.format(
|
||||||
|
"File [%s] does not contain a line containing: [%s]",
|
||||||
|
file.getName(),
|
||||||
|
text
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertFileIsEmpty(java.io.File file) {
|
||||||
|
try {
|
||||||
|
FileReader fileReader = new FileReader( file );
|
||||||
|
assertEquals( -1, fileReader.read() );
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
fail( ex.getMessage() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dropSchema(String[] args) {
|
||||||
|
final String[] argsNew = Arrays.copyOf( args, args.length + 1 );
|
||||||
|
argsNew[ args.length ] = "--drop";
|
||||||
|
SchemaExport.main( argsNew );
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE hibernate-configuration PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||||
|
|
||||||
|
<hibernate-configuration>
|
||||||
|
<session-factory>
|
||||||
|
<mapping package="org.hibernate.test.schematoolsnaming"/>
|
||||||
|
<mapping class="org.hibernate.test.schematoolsnaming.Matrix"/>
|
||||||
|
</session-factory>
|
||||||
|
</hibernate-configuration>
|
Loading…
Reference in New Issue