HHH-6212 Some discriminator related cleanup. Getting rid of DiscriminatorColumnValues

This commit is contained in:
Hardy Ferentschik 2011-07-26 13:05:42 +02:00
parent 57cb51fd14
commit 3ff5c13058
7 changed files with 108 additions and 217 deletions

View File

@ -37,10 +37,6 @@ public class ColumnValuesSourceImpl implements ColumnSource {
this.columnValues = columnValues;
}
protected ColumnValues columnValues() {
return columnValues;
}
@Override
public String getName() {
return columnValues.getName();
@ -53,7 +49,6 @@ public class ColumnValuesSourceImpl implements ColumnSource {
@Override
public String getDefaultValue() {
// todo
return null;
}

View File

@ -1,115 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, 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.metamodel.source.annotations.attribute;
import java.util.List;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.DotName;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
/**
* Container for the properties of a discriminator column.
*
* @author Hardy Ferentschik
*/
public class DiscriminatorColumnValues extends ColumnValues {
public static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "DTYPE";
private static final int DEFAULT_DISCRIMINATOR_LENGTH = 31;
private boolean isForced = true;
private boolean isIncludedInSql = true;
private String discriminatorValue = null;
public DiscriminatorColumnValues(Map<DotName, List<AnnotationInstance>> annotations) {
super();
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_COLUMN
);
if ( discriminatorOptionsAnnotation != null ) {
setName( discriminatorOptionsAnnotation.value( "name" ).asString() );
setLength( discriminatorOptionsAnnotation.value( "length" ).asInt() );
if ( discriminatorOptionsAnnotation.value( "columnDefinition" ) != null ) {
setColumnDefinition( discriminatorOptionsAnnotation.value( "columnDefinition" ).asString() );
}
}
else {
setName( DEFAULT_DISCRIMINATOR_COLUMN_NAME );
setLength( DEFAULT_DISCRIMINATOR_LENGTH );
}
setNullable( false );
setDiscriminatorValue( annotations );
setDiscriminatorOptions( annotations );
setDiscriminatorFormula( annotations );
}
private void setDiscriminatorValue(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorValueAnnotation = JandexHelper.getSingleAnnotation(
annotations, JPADotNames.DISCRIMINATOR_VALUE
);
if ( discriminatorValueAnnotation != null ) {
discriminatorValue = discriminatorValueAnnotation.value().asString();
}
}
private void setDiscriminatorFormula(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorFormulaAnnotation = JandexHelper.getSingleAnnotation(
annotations, HibernateDotNames.DISCRIMINATOR_FORMULA
);
if ( discriminatorFormulaAnnotation != null ) {
// todo
}
}
public boolean isForced() {
return isForced;
}
public boolean isIncludedInSql() {
return isIncludedInSql;
}
public String getDiscriminatorValue() {
return discriminatorValue;
}
private void setDiscriminatorOptions(Map<DotName, List<AnnotationInstance>> annotations) {
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
annotations, HibernateDotNames.DISCRIMINATOR_OPTIONS
);
if ( discriminatorOptionsAnnotation != null ) {
isForced = discriminatorOptionsAnnotation.value( "force" ).asBoolean();
isIncludedInSql = discriminatorOptionsAnnotation.value( "insert" ).asBoolean();
}
}
}

View File

@ -31,32 +31,30 @@ import org.hibernate.metamodel.source.binder.RelationalValueSource;
* @author Hardy Ferentschik
*/
public class DiscriminatorSourceImpl implements DiscriminatorSource {
private final DiscriminatorColumnValues discriminatorColumnValues;
private final Class<?> discriminatorType;
private final EntityClass entityClass;
public DiscriminatorSourceImpl(EntityClass entityClass) {
this.discriminatorColumnValues = entityClass.getDiscriminatorColumnValues();
this.discriminatorType = entityClass.getDiscriminatorType();
this.entityClass = entityClass;
}
@Override
public boolean isForced() {
return discriminatorColumnValues.isForced();
return entityClass.isDiscriminatorForced();
}
@Override
public boolean isInserted() {
return discriminatorColumnValues.isIncludedInSql();
return entityClass.isDiscriminatorIncludedInSql();
}
@Override
public RelationalValueSource getDiscriminatorRelationalValueSource() {
return new ColumnValuesSourceImpl( discriminatorColumnValues );
return new ColumnValuesSourceImpl( entityClass.getDiscriminatorColumnValues() );
}
@Override
public String getExplicitHibernateTypeName() {
return discriminatorType.getName();
return entityClass.getDiscriminatorType().getName();
}
}

View File

@ -54,7 +54,7 @@ import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.DiscriminatorColumnValues;
import org.hibernate.metamodel.source.annotations.attribute.ColumnValues;
import org.hibernate.metamodel.source.binder.ConstraintSource;
import org.hibernate.metamodel.source.binder.TableSource;
@ -95,9 +95,12 @@ public class EntityClass extends ConfiguredClass {
private boolean isLazy;
private String proxy;
private DiscriminatorColumnValues discriminatorColumnValues;
private ColumnValues discriminatorColumnValues;
private Class<?> discriminatorType;
private String discriminatorMatchValue;
private boolean isDiscriminatorForced = true;
private boolean isDiscriminatorIncludedInSql = true;
public EntityClass(
ClassInfo classInfo,
@ -137,7 +140,7 @@ public class EntityClass extends ConfiguredClass {
processDiscriminator();
}
public DiscriminatorColumnValues getDiscriminatorColumnValues() {
public ColumnValues getDiscriminatorColumnValues() {
return discriminatorColumnValues;
}
@ -254,11 +257,18 @@ public class EntityClass extends ConfiguredClass {
return getParent() == null;
}
public boolean isDiscriminatorForced() {
return isDiscriminatorForced;
}
public boolean isDiscriminatorIncludedInSql() {
return isDiscriminatorIncludedInSql;
}
private String determineExplicitEntityName() {
final AnnotationInstance jpaEntityAnnotation = JandexHelper.getSingleAnnotation(
getClassInfo(), JPADotNames.ENTITY
);
return JandexHelper.getValue( jpaEntityAnnotation, "name", String.class );
}
@ -326,13 +336,15 @@ public class EntityClass extends ConfiguredClass {
this.discriminatorMatchValue = discriminatorValueAnnotation.value().asString();
}
final AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
final AnnotationInstance discriminatorColumnAnnotation = JandexHelper.getSingleAnnotation(
getClassInfo(), JPADotNames.DISCRIMINATOR_COLUMN
);
discriminatorColumnValues = new ColumnValues( discriminatorColumnAnnotation );
discriminatorColumnValues.setNullable( false ); // discriminator column cannot be null
Class<?> type = String.class; // string is the discriminator default
if ( discriminatorOptionsAnnotation != null ) {
if ( discriminatorColumnAnnotation != null ) {
DiscriminatorType discriminatorType = Enum.valueOf(
DiscriminatorType.class, discriminatorOptionsAnnotation.value( "discriminatorType" ).asEnum()
DiscriminatorType.class, discriminatorColumnAnnotation.value( "discriminatorType" ).asEnum()
);
switch ( discriminatorType ) {
case STRING: {
@ -351,10 +363,41 @@ public class EntityClass extends ConfiguredClass {
throw new AnnotationException( "Unsupported discriminator type: " + discriminatorType );
}
}
}
discriminatorColumnValues = new DiscriminatorColumnValues( getClassInfo().annotations() );
discriminatorColumnValues.setName(
JandexHelper.getValue(
discriminatorColumnAnnotation,
"name",
String.class
)
);
discriminatorColumnValues.setLength(
JandexHelper.getValue(
discriminatorColumnAnnotation,
"length",
Integer.class
)
);
if ( discriminatorColumnAnnotation.value( "columnDefinition" ) != null ) {
discriminatorColumnValues.setColumnDefinition(
discriminatorColumnAnnotation.value( "columnDefinition" )
.asString()
);
}
}
discriminatorType = type;
AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation(
getClassInfo(), HibernateDotNames.DISCRIMINATOR_OPTIONS
);
if ( discriminatorOptionsAnnotation != null ) {
isDiscriminatorForced = discriminatorOptionsAnnotation.value( "force" ).asBoolean();
isDiscriminatorIncludedInSql = discriminatorOptionsAnnotation.value( "insert" ).asBoolean();
}
else {
isDiscriminatorForced = false;
isDiscriminatorIncludedInSql = true;
}
}
private void processHibernateEntitySpecificAnnotations() {

View File

@ -23,14 +23,16 @@
*/
package org.hibernate.metamodel.source.annotations.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.junit.Test;
import org.hibernate.annotations.DiscriminatorOptions;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.testing.FailureExpected;
import org.hibernate.metamodel.binding.EntityDiscriminator;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
@ -57,7 +59,11 @@ public class InheritanceBindingTest extends BaseAnnotationBindingTestCase {
}
@Test
@Resources(annotatedClasses = { SubclassOfSingleTableInheritance.class, SingleEntity.class, RootOfSingleTableInheritance.class })
@Resources(annotatedClasses = {
SubclassOfSingleTableInheritance.class,
SingleEntity.class,
RootOfSingleTableInheritance.class
})
public void testRootEntityBinding() {
EntityBinding noInheritanceEntityBinding = getEntityBinding( SingleEntity.class );
assertTrue( "SingleEntity should be a root entity", noInheritanceEntityBinding.isRoot() );
@ -72,12 +78,54 @@ public class InheritanceBindingTest extends BaseAnnotationBindingTestCase {
assertSame( rootEntityBinding, getRootEntityBinding( RootOfSingleTableInheritance.class ) );
}
@Test
@Resources(annotatedClasses = { RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class })
public void testDefaultDiscriminatorOptions() {
EntityBinding rootEntityBinding = getEntityBinding( RootOfSingleTableInheritance.class );
EntityDiscriminator discriminator = rootEntityBinding.getHierarchyDetails().getEntityDiscriminator();
assertFalse( "Wrong default value", discriminator.isForced() );
assertTrue( "Wrong default value", discriminator.isInserted() );
}
@Test
@Resources(annotatedClasses = { Base.class, Jump.class })
public void testExplicitDiscriminatorOptions() {
EntityBinding rootEntityBinding = getEntityBinding( Base.class );
EntityDiscriminator discriminator = rootEntityBinding.getHierarchyDetails().getEntityDiscriminator();
assertTrue( "Wrong default value", discriminator.isForced() );
assertFalse( "Wrong default value", discriminator.isInserted() );
}
@Entity
class SingleEntity {
@Id
@GeneratedValue
private int id;
}
@Entity
class RootOfSingleTableInheritance {
@Id
@GeneratedValue
private int id;
}
@Entity
@DiscriminatorValue("foo")
public class SubclassOfSingleTableInheritance extends RootOfSingleTableInheritance {
}
@Entity
@DiscriminatorOptions(force = true, insert = false)
class Base {
@Id
@GeneratedValue
private int id;
}
@Entity
class Jump extends Base {
}
}

View File

@ -1,41 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.metamodel.source.annotations.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Hardy Ferentschik
*/
@Entity
class RootOfSingleTableInheritance {
@Id
@GeneratedValue
private int id;
}

View File

@ -1,37 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.metamodel.source.annotations.entity;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
* @author Hardy Ferentschik
*/
@Entity
@DiscriminatorValue( "foo" )
public class SubclassOfSingleTableInheritance extends RootOfSingleTableInheritance {
}