From 1f9c157db6e69c64407514d4baa5e6db9d21a91c Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Sun, 22 Sep 2013 14:41:00 -0500 Subject: [PATCH] HHH-8539 - @Column#table naming primary table fails --- .../org/hibernate/cfg/AnnotationBinder.java | 6 +- .../java/org/hibernate/cfg/Ejb3Column.java | 63 ++++++++++----- .../org/hibernate/cfg/Ejb3JoinColumn.java | 4 +- .../java/org/hibernate/cfg/IndexColumn.java | 2 +- ...thExplicitReferenceToPrimaryTableTest.java | 76 +++++++++++++++++++ 5 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/mapping/ColumnWithExplicitReferenceToPrimaryTableTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java index 6f9e193895..9543d9af26 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/AnnotationBinder.java @@ -1659,7 +1659,7 @@ public final class AnnotationBinder { if ( assocTable != null ) { Join join = propertyHolder.addJoin( assocTable, false ); for ( Ejb3JoinColumn joinColumn : joinColumns ) { - joinColumn.setSecondaryTableName( join.getTable().getName() ); + joinColumn.setExplicitTableName( join.getTable().getName() ); } } final boolean mandatory = !ann.optional() || forcePersist; @@ -1698,7 +1698,7 @@ public final class AnnotationBinder { if ( assocTable != null ) { Join join = propertyHolder.addJoin( assocTable, false ); for ( Ejb3JoinColumn joinColumn : joinColumns ) { - joinColumn.setSecondaryTableName( join.getTable().getName() ); + joinColumn.setExplicitTableName( join.getTable().getName() ); } } //MapsId means the columns belong to the pk => not null @@ -1739,7 +1739,7 @@ public final class AnnotationBinder { if ( assocTable != null ) { Join join = propertyHolder.addJoin( assocTable, false ); for ( Ejb3JoinColumn joinColumn : joinColumns ) { - joinColumn.setSecondaryTableName( join.getTable().getName() ); + joinColumn.setExplicitTableName( join.getTable().getName() ); } } bindAny( diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java index c6a28562d1..d6a1dcf5d6 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3Column.java @@ -55,7 +55,7 @@ public class Ejb3Column { private Column mappingColumn; private boolean insertable = true; private boolean updatable = true; - private String secondaryTableName; + private String explicitTableName; protected Map joins; protected PropertyHolder propertyHolder; private Mappings mappings; @@ -111,8 +111,37 @@ public class Ejb3Column { return formulaString; } + /** + * Deprecated as this is badly named for its use. + * + * @deprecated Use {@link #getExplicitTableName} instead + */ + @Deprecated public String getSecondaryTableName() { - return secondaryTableName; + return explicitTableName; + } + + public String getExplicitTableName() { + return explicitTableName; + } + + /** + * Deprecated as this is badly named for its use. + * + * @deprecated Use {@link #setExplicitTableName} instead + */ + @Deprecated + public void setSecondaryTableName(String explicitTableName) { + setExplicitTableName( explicitTableName ); + } + + public void setExplicitTableName(String explicitTableName) { + if ( "``".equals( explicitTableName ) ) { + this.explicitTableName = ""; + } + else { + this.explicitTableName = explicitTableName; + } } public void setFormula(String formula) { @@ -337,7 +366,10 @@ public class Ejb3Column { * @throws AnnotationException missing secondary table */ public Table getTable() { - if ( table != null ) return table; //association table + if ( table != null ){ + return table; + } + if ( isSecondary() ) { return getJoin().getTable(); } @@ -348,21 +380,19 @@ public class Ejb3Column { public boolean isSecondary() { if ( propertyHolder == null ) { - throw new AssertionFailure( "Should not call getTable() on column wo persistent class defined" ); + throw new AssertionFailure( "Should not call getTable() on column w/o persistent class defined" ); } - if ( StringHelper.isNotEmpty( secondaryTableName ) ) { - return true; - } - // else { - return false; + + return StringHelper.isNotEmpty( explicitTableName ) + && !propertyHolder.getTable().getName().equals( explicitTableName ); } public Join getJoin() { - Join join = joins.get( secondaryTableName ); + Join join = joins.get( explicitTableName ); if ( join == null ) { throw new AnnotationException( "Cannot find the expected secondary table: no " - + secondaryTableName + " available for " + propertyHolder.getClassName() + + explicitTableName + " available for " + propertyHolder.getClassName() ); } else { @@ -374,15 +404,6 @@ public class Ejb3Column { mappingColumn.setNullable( false ); } - public void setSecondaryTableName(String secondaryTableName) { - if ( "``".equals( secondaryTableName ) ) { - this.secondaryTableName = ""; - } - else { - this.secondaryTableName = secondaryTableName; - } - } - public static Ejb3Column[] buildColumnFromAnnotation( javax.persistence.Column[] anns, org.hibernate.annotations.Formula formulaAnn, @@ -472,7 +493,7 @@ public class Ejb3Column { column.setUnique( col.unique() ); column.setInsertable( col.insertable() ); column.setUpdatable( col.updatable() ); - column.setSecondaryTableName( tableName ); + column.setExplicitTableName( tableName ); column.setPropertyHolder( propertyHolder ); column.setJoins( secondaryTables ); column.setMappings( mappings ); diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java index 90c415f671..dff27ebe2f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java @@ -122,7 +122,7 @@ public class Ejb3JoinColumn extends Ejb3Column { setUnique( unique ); setInsertable( insertable ); setUpdatable( updatable ); - setSecondaryTableName( secondaryTable ); + setExplicitTableName( secondaryTable ); setPropertyHolder( propertyHolder ); setJoins( joins ); setMappings( mappings ); @@ -310,7 +310,7 @@ public class Ejb3JoinColumn extends Ejb3Column { final String tableName = !BinderHelper.isEmptyAnnotationValue( annJoin.table() ) ? nameNormalizer.normalizeIdentifierQuoting( getMappings().getNamingStrategy().tableName( annJoin.table() ) ) : ""; - setSecondaryTableName( tableName ); + setExplicitTableName( tableName ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java b/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java index 893850342f..aaf82c375e 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/IndexColumn.java @@ -62,7 +62,7 @@ public class IndexColumn extends Ejb3Column { setUnique( unique ); setInsertable( insertable ); setUpdatable( updatable ); - setSecondaryTableName( secondaryTableName ); + setExplicitTableName( secondaryTableName ); setPropertyHolder( propertyHolder ); setJoins( joins ); setMappings( mappings ); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/mapping/ColumnWithExplicitReferenceToPrimaryTableTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/mapping/ColumnWithExplicitReferenceToPrimaryTableTest.java new file mode 100644 index 0000000000..e0e89d2679 --- /dev/null +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/mapping/ColumnWithExplicitReferenceToPrimaryTableTest.java @@ -0,0 +1,76 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2013, 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.jpa.test.mapping; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Id; +import javax.persistence.Table; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.jpa.boot.spi.Bootstrap; +import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +/** + * @author Steve Ebersole + */ +public class ColumnWithExplicitReferenceToPrimaryTableTest extends BaseUnitTestCase { + @Test + @TestForIssue( jiraKey = "HHH-8539" ) + public void testColumnAnnotationWithExplicitReferenceToPrimaryTable() { + final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { + @Override + public List getManagedClassNames() { + return Arrays.asList( AnEntity.class.getName() ); + } + }; + + + final Map settings = new HashMap(); + settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); + emf.close(); + } + + @Entity + @Table( name = "THE_TABLE" ) + public static class AnEntity { + @Id + public Integer id; + @Column( name = "THE_COLUMN", table = "THE_TABLE" ) + public String theValue; + } +}