From 36770456637466a3fb79c123bd6914d29014db19 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Tue, 1 Oct 2013 12:18:22 -0400 Subject: [PATCH] HHH-8520 apply global quoting to HBM columns --- .../java/org/hibernate/cfg/HbmBinder.java | 22 +++-- .../test/quote/AssociatedDataPoint.java | 58 ++++++++++++ .../hibernate/test/quote/DataPoint.hbm.xml | 65 +++++++++++++ .../org/hibernate/test/quote/DataPoint.java | 92 +++++++++++++++++++ .../test/{annotations => }/quote/House.java | 2 +- .../test/{annotations => }/quote/Person.java | 2 +- .../test/{annotations => }/quote/Phone.java | 2 +- .../quote/QuoteGlobalTest.java | 27 +++++- .../{annotations => }/quote/QuoteTest.java | 2 +- .../test/{annotations => }/quote/Role.java | 2 +- .../test/{annotations => }/quote/User.java | 2 +- 11 files changed, 263 insertions(+), 13 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/quote/AssociatedDataPoint.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.java rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/House.java (97%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/Person.java (97%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/Phone.java (94%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/QuoteGlobalTest.java (79%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/QuoteTest.java (98%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/Role.java (89%) rename hibernate-core/src/test/java/org/hibernate/test/{annotations => }/quote/User.java (97%) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java index 64166cd818..b66785398c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java @@ -1067,12 +1067,13 @@ public final class HbmBinder { column.setValue( simpleValue ); column.setTypeIndex( count++ ); bindColumn( columnElement, column, isNullable ); - final String columnName = columnElement.attributeValue( "name" ); + String columnName = columnElement.attributeValue( "name" ); String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( columnName, propertyPath ); - column.setName( mappings.getNamingStrategy().columnName( - columnName ) ); + columnName = mappings.getNamingStrategy().columnName( columnName ); + columnName = quoteIdentifier( columnName, mappings ); + column.setName( columnName ); if ( table != null ) { table.addColumn( column ); // table=null -> an association // - fill it in later @@ -1122,11 +1123,13 @@ public final class HbmBinder { if ( column.isUnique() && ManyToOne.class.isInstance( simpleValue ) ) { ( (ManyToOne) simpleValue ).markAsLogicalOneToOne(); } - final String columnName = columnAttribute.getValue(); + String columnName = columnAttribute.getValue(); String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( columnName, propertyPath ); - column.setName( mappings.getNamingStrategy().columnName( columnName ) ); + columnName = mappings.getNamingStrategy().columnName( columnName ); + columnName = quoteIdentifier( columnName, mappings ); + column.setName( columnName ); if ( table != null ) { table.addColumn( column ); // table=null -> an association - fill // it in later @@ -1142,7 +1145,9 @@ public final class HbmBinder { Column column = new Column(); column.setValue( simpleValue ); bindColumn( node, column, isNullable ); - column.setName( mappings.getNamingStrategy().propertyToColumnName( propertyPath ) ); + String columnName = mappings.getNamingStrategy().propertyToColumnName( propertyPath ); + columnName = quoteIdentifier( columnName, mappings ); + column.setName( columnName ); String logicalName = mappings.getNamingStrategy().logicalColumnName( null, propertyPath ); mappings.addColumnBinding( logicalName, column, table ); /* TODO: joinKeyColumnName & foreignKeyColumnName should be called either here or at a @@ -3196,6 +3201,11 @@ public final class HbmBinder { recognizeEntities( mappings, element, handler ); } } + + private static String quoteIdentifier(String identifier, Mappings mappings) { + return mappings.getObjectNameNormalizer().isUseQuotedIdentifiersGlobally() + ? StringHelper.quote( identifier ) : identifier; + } private static interface EntityElementHandler { public void handleEntity(String entityName, String className, Mappings mappings); diff --git a/hibernate-core/src/test/java/org/hibernate/test/quote/AssociatedDataPoint.java b/hibernate-core/src/test/java/org/hibernate/test/quote/AssociatedDataPoint.java new file mode 100644 index 0000000000..6d0880bc8b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/AssociatedDataPoint.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * 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, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.test.quote; + +import java.util.List; + +/** + * @author Brett Meyer + */ +public class AssociatedDataPoint { + private long id; + + private AssociatedDataPoint manyToOne; + + private List manyToMany; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public AssociatedDataPoint getManyToOne() { + return manyToOne; + } + + public void setManyToOne(AssociatedDataPoint manyToOne) { + this.manyToOne = manyToOne; + } + + public List getManyToMany() { + return manyToMany; + } + + public void setManyToMany(List manyToMany) { + this.manyToMany = manyToMany; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.hbm.xml new file mode 100644 index 0000000000..25346f0c5c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.hbm.xml @@ -0,0 +1,65 @@ + + + + + + + + org.hibernate.test.quote.DataPoint$DataPointEnum + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.java b/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.java new file mode 100644 index 0000000000..ade4bdb7b6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/DataPoint.java @@ -0,0 +1,92 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * JBoss, Home of Professional Open Source + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors + * as indicated by the @authors tag. All rights reserved. + * See the copyright.txt in the distribution for a + * full listing of individual contributors. + * + * 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, v. 2.1. + * This program is distributed in the hope that it will be useful, but WITHOUT A + * 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, + * v.2.1 along with this distribution; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +package org.hibernate.test.quote; + +import java.util.List; + +/** + * @author Brett Meyer + */ +public class DataPoint { + private long id; + + private String fooProp; + + private DataPointEnum fooEnum; + + private List fooEnumList; + + private List oneToMany; + + private List manyToMany; + + public static enum DataPointEnum { + FOO1, FOO2, FOO3; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getFooProp() { + return fooProp; + } + + public void setFooProp(String fooProp) { + this.fooProp = fooProp; + } + + public DataPointEnum getFooEnum() { + return fooEnum; + } + + public void setFooEnum(DataPointEnum fooEnum) { + this.fooEnum = fooEnum; + } + + public List getFooEnumList() { + return fooEnumList; + } + + public void setFooEnumList(List fooEnumList) { + this.fooEnumList = fooEnumList; + } + + public List getOneToMany() { + return oneToMany; + } + + public void setOneToMany(List oneToMany) { + this.oneToMany = oneToMany; + } + + public List getManyToMany() { + return manyToMany; + } + + public void setManyToMany(List manyToMany) { + this.manyToMany = manyToMany; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/House.java b/hibernate-core/src/test/java/org/hibernate/test/quote/House.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/House.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/House.java index 0e8eca2aab..ff10c21ecb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/House.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/House.java @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Person.java b/hibernate-core/src/test/java/org/hibernate/test/quote/Person.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Person.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/Person.java index b99e250d21..18979c66d2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/Person.java @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Phone.java b/hibernate-core/src/test/java/org/hibernate/test/quote/Phone.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Phone.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/Phone.java index 8fa149b839..48fdb78e3c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Phone.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/Phone.java @@ -1,4 +1,4 @@ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteGlobalTest.java b/hibernate-core/src/test/java/org/hibernate/test/quote/QuoteGlobalTest.java similarity index 79% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteGlobalTest.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/QuoteGlobalTest.java index c179cec84e..2e7f602cc5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteGlobalTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/QuoteGlobalTest.java @@ -21,9 +21,10 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Iterator; @@ -32,6 +33,8 @@ import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; +import org.hibernate.mapping.Column; +import org.hibernate.mapping.Table; import org.hibernate.mapping.UniqueKey; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -75,6 +78,23 @@ public class QuoteGlobalTest extends BaseCoreFunctionalTestCase { assertEquals( "User_Role", configuration().getCollectionMapping( role ).getCollectionTable().getName() ); s.close(); } + + @Test + @TestForIssue(jiraKey = "HHH-8520") + public void testHbmQuoting() { + doTestHbmQuoting( DataPoint.class ); + doTestHbmQuoting( AssociatedDataPoint.class ); + } + + private void doTestHbmQuoting(Class clazz) { + Table table = configuration().getClassMapping( clazz.getName() ).getTable(); + assertTrue( table.isQuoted() ); + Iterator itr = table.getColumnIterator(); + while(itr.hasNext()) { + Column column = (Column) itr.next(); + assertTrue( column.isQuoted() ); + } + } @Override protected void configure(Configuration cfg) { @@ -92,4 +112,9 @@ public class QuoteGlobalTest extends BaseCoreFunctionalTestCase { House.class }; } + + @Override + protected String[] getMappings() { + return new String[] { "quote/DataPoint.hbm.xml" }; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteTest.java b/hibernate-core/src/test/java/org/hibernate/test/quote/QuoteTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteTest.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/QuoteTest.java index b5c33e0313..12ad902f8a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/QuoteTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/QuoteTest.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Role.java b/hibernate-core/src/test/java/org/hibernate/test/quote/Role.java similarity index 89% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Role.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/Role.java index 2123acc425..d210f12cb8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/Role.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/Role.java @@ -1,5 +1,5 @@ //$Id$ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/User.java b/hibernate-core/src/test/java/org/hibernate/test/quote/User.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/test/annotations/quote/User.java rename to hibernate-core/src/test/java/org/hibernate/test/quote/User.java index 81794ea94e..022ea3bd44 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/quote/User.java +++ b/hibernate-core/src/test/java/org/hibernate/test/quote/User.java @@ -1,5 +1,5 @@ //$Id$ -package org.hibernate.test.annotations.quote; +package org.hibernate.test.quote; import java.io.Serializable; import java.util.HashSet;