HHH-4358 - Having to use @ForceDiscriminator kind of breaks JPA compatibility

This commit is contained in:
Steve Ebersole 2012-01-24 13:42:57 -06:00
parent 75bd6b319b
commit 23a62802c8
8 changed files with 141 additions and 10 deletions

View File

@ -546,4 +546,6 @@ public interface AvailableSettings {
* either a class name or instance.
*/
public static final String CUSTOM_ENTITY_DIRTINESS_STRATEGY = "hibernate.entity_dirtiness_strategy";
public static final String FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT = "hibernate.discriminator.force_in_select";
}

View File

@ -3002,9 +3002,6 @@ public class Configuration implements Serializable {
return properties;
}
private Boolean useNewGeneratorMappings;
public void addDefaultGenerator(IdGenerator generator) {
this.addGenerator( generator );
defaultNamedGenerators.add( generator.getName() );
@ -3055,15 +3052,31 @@ public class Configuration implements Serializable {
map.put( property.getPropertyName(), property );
}
private Boolean useNewGeneratorMappings;
@SuppressWarnings({ "UnnecessaryUnboxing" })
public boolean useNewGeneratorMappings() {
if ( useNewGeneratorMappings == null ) {
final String booleanName = getConfigurationProperties().getProperty( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS );
final String booleanName = getConfigurationProperties()
.getProperty( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS );
useNewGeneratorMappings = Boolean.valueOf( booleanName );
}
return useNewGeneratorMappings.booleanValue();
}
private Boolean forceDiscriminatorInSelectsByDefault;
@Override
@SuppressWarnings( {"UnnecessaryUnboxing"})
public boolean forceDiscriminatorInSelectsByDefault() {
if ( forceDiscriminatorInSelectsByDefault == null ) {
final String booleanName = getConfigurationProperties()
.getProperty( AvailableSettings.FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT );
forceDiscriminatorInSelectsByDefault = Boolean.valueOf( booleanName );
}
return forceDiscriminatorInSelectsByDefault.booleanValue();
}
public IdGenerator getGenerator(String name) {
return getGenerator( name, null );
}

View File

@ -542,10 +542,14 @@ public final class HbmBinder {
// ( (Column) discrim.getColumnIterator().next() ).setType(type);
}
entity.setPolymorphic( true );
if ( "true".equals( subnode.attributeValue( "force" ) ) )
entity.setForceDiscriminator( true );
if ( "false".equals( subnode.attributeValue( "insert" ) ) )
final String explicitForceValue = subnode.attributeValue( "force" );
boolean forceDiscriminatorInSelects = explicitForceValue == null
? mappings.forceDiscriminatorInSelectsByDefault()
: "true".equals( explicitForceValue );
entity.setForceDiscriminator( forceDiscriminatorInSelects );
if ( "false".equals( subnode.attributeValue( "insert" ) ) ) {
entity.setDiscriminatorInsertable( false );
}
}
public static void bindClass(Element node, PersistentClass persistentClass, Mappings mappings,

View File

@ -754,4 +754,6 @@ public interface Mappings {
public PropertyData getPropertyAnnotatedWithIdAndToOne(XClass entityType, String propertyName);
void addToOneAndIdProperty(XClass entity, PropertyData property);
public boolean forceDiscriminatorInSelectsByDefault();
}

View File

@ -230,9 +230,10 @@ public class EntityBinder {
rootClass.setCacheRegionName( cacheRegion );
rootClass.setLazyPropertiesCacheable( cacheLazyProperty );
}
if(forceDiscriminator != null) {
rootClass.setForceDiscriminator( forceDiscriminator );
}
boolean forceDiscriminatorInSelects = forceDiscriminator == null
? mappings.forceDiscriminatorInSelectsByDefault()
: forceDiscriminator;
rootClass.setForceDiscriminator( forceDiscriminatorInSelects );
if( insertableDiscriminator != null) {
rootClass.setDiscriminatorInsertable( insertableDiscriminator );
}

View File

@ -0,0 +1,42 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.annotations.inheritance.discriminatoroptions;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Hardy Ferentschik
*/
@Entity
@DiscriminatorValue("B")
public class BaseClass2 {
@Id
@GeneratedValue
private long id;
}

View File

@ -25,6 +25,7 @@ package org.hibernate.test.annotations.inheritance.discriminatoroptions;
import org.junit.Test;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.RootClass;
@ -54,4 +55,33 @@ public class DiscriminatorOptionsTest extends BaseUnitTestCase {
assertTrue( "Discriminator should be forced", root.isForceDiscriminator() );
assertFalse( "Discriminator should not be insertable", root.isDiscriminatorInsertable() );
}
@Test
public void testBaseline() throws Exception {
Configuration configuration = new Configuration()
.addAnnotatedClass( BaseClass2.class )
.addAnnotatedClass( SubClass2.class );
configuration.buildMappings();
PersistentClass persistentClass = configuration.getClassMapping( BaseClass2.class.getName() );
assertNotNull( persistentClass );
assertTrue( persistentClass instanceof RootClass );
RootClass root = ( RootClass ) persistentClass;
assertFalse( "Discriminator should not be forced by default", root.isForceDiscriminator() );
}
@Test
public void testPropertyBasedDiscriminatorForcing() throws Exception {
Configuration configuration = new Configuration()
.setProperty( AvailableSettings.FORCE_DISCRIMINATOR_IN_SELECTS_BY_DEFAULT, "true" )
.addAnnotatedClass( BaseClass2.class )
.addAnnotatedClass( SubClass2.class );
configuration.buildMappings();
PersistentClass persistentClass = configuration.getClassMapping( BaseClass2.class.getName() );
assertNotNull( persistentClass );
assertTrue( persistentClass instanceof RootClass );
RootClass root = ( RootClass ) persistentClass;
assertTrue( "Discriminator should be forced by property", root.isForceDiscriminator() );
}
}

View File

@ -0,0 +1,37 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.annotations.inheritance.discriminatoroptions;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* @author Hardy Ferentschik
*/
@Entity
@DiscriminatorValue("B")
public class SubClass2 extends BaseClass2 {
}