HHH-8539 - @Column#table naming primary table fails
This commit is contained in:
parent
2bb866a616
commit
1f9c157db6
|
@ -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(
|
||||
|
|
|
@ -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<String, Join> 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 );
|
||||
|
|
|
@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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<String> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue