javax.persistence.InheritanceType Java Examples

The following examples show how to use javax.persistence.InheritanceType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EntityMapping.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public javax.persistence.InheritanceType getInheritanceStrategy(
		Mapping context, ClassOutline classOutline, Entity entity) {
	if (isRootClass(context, classOutline)) {
		if (entity.getInheritance() != null
				&& entity.getInheritance().getStrategy() != null) {
			return InheritanceType.valueOf(entity.getInheritance()
					.getStrategy());
		} else {
			return javax.persistence.InheritanceType.JOINED;
		}
	} else {
		final ClassOutline superClassOutline = getSuperClass(context,
				classOutline);
		final Entity superClassEntity = context.getCustomizing().getEntity(
				superClassOutline);

		return getInheritanceStrategy(context, superClassOutline,
				superClassEntity);
	}
}
 
Example #2
Source File: EntityMapping.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void createEntity$Inheritance(Mapping context,
		ClassOutline classOutline, final Entity entity) {
	final InheritanceType inheritanceStrategy = getInheritanceStrategy(
			context, classOutline, entity);

	if (isRootClass(context, classOutline)) {
		if (entity.getInheritance() == null
				|| entity.getInheritance().getStrategy() == null) {
			entity.setInheritance(new Inheritance());
			entity.getInheritance().setStrategy(inheritanceStrategy.name());
		}
	} else {
		if (entity.getInheritance() != null
				&& entity.getInheritance().getStrategy() != null) {
			entity.setInheritance(null);
		}
	}
}
 
Example #3
Source File: AnnotationBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static PersistentClass makePersistentClass(
		InheritanceState inheritanceState,
		PersistentClass superEntity,
		MetadataBuildingContext metadataBuildingContext) {
	//we now know what kind of persistent entity it is
	if ( !inheritanceState.hasParents() ) {
		return new RootClass( metadataBuildingContext );
	}
	else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
		return new SingleTableSubclass( superEntity, metadataBuildingContext );
	}
	else if ( InheritanceType.JOINED.equals( inheritanceState.getType() ) ) {
		return new JoinedSubclass( superEntity, metadataBuildingContext );
	}
	else if ( InheritanceType.TABLE_PER_CLASS.equals( inheritanceState.getType() ) ) {
		return new UnionSubclass( superEntity, metadataBuildingContext );
	}
	else {
		throw new AssertionFailure( "Unknown inheritance type: " + inheritanceState.getType() );
	}
}
 
Example #4
Source File: InheritanceFactory.java    From celerio with Apache License 2.0 6 votes vote down vote up
private void putEntityByTableNameForEntityWithInheritance() {
    // Attention, for SINGLE_TABLE inheritance strategy, we only put the root entity.

    for (EntityConfig entityConfig : config.getCelerio().getEntityConfigs()) {
        Entity entity = config.getProject().getEntityByName(entityConfig.getEntityName());

        if (entity.hasInheritance() && !config.getProject().hasEntityBySchemaAndTableName(entity.getTable().getSchemaName(), entity.getTable().getName())) {
            InheritanceType inheritanceType = entity.getInheritance().getStrategy();

            if (inheritanceType == InheritanceType.SINGLE_TABLE) {
                if (entity.isRoot()) {
                    config.getProject().putEntity(entity);
                }
            } else if (inheritanceType == InheritanceType.JOINED || inheritanceType == InheritanceType.TABLE_PER_CLASS) {
                config.getProject().putEntity(entity);
            } else {
                log.warning("Invalid case, there should be an inheritance type");
            }
        }
    }
}
 
Example #5
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithJoinedInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final String nodeLabelBase = "ENTITY_CLASS";
    final String nodeLabelA = "ENTITY_CLASS_A";
    final String nodeLabelB = "ENTITY_CLASS_B";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Table.class).param("name", nodeLabelBase);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.JOINED);

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);
    jSubclassA.annotate(Table.class).param("name", nodeLabelA);

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);
    jSubclassB.annotate(Table.class).param("name", nodeLabelB);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabelA),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(subClassA));
}
 
Example #6
Source File: EntityMapping.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void createEntity$Table(Mapping context, ClassOutline classOutline,
		Entity entity) {
	final InheritanceType inheritanceStrategy = getInheritanceStrategy(
			context, classOutline, entity);
	switch (inheritanceStrategy) {
	case JOINED:
		if (entity.getTable() == null) {
			entity.setTable(new Table());
		}
		createTable(context, classOutline, entity.getTable());
		break;
	case SINGLE_TABLE:
		if (isRootClass(context, classOutline)) {
			if (entity.getTable() == null) {
				entity.setTable(new Table());
			}
			createTable(context, classOutline, entity.getTable());
		} else {
			if (entity.getTable() != null) {
				entity.setTable(null);
			}
		}
		break;
	case TABLE_PER_CLASS:
		if (entity.getTable() == null) {
			entity.setTable(new Table());
		}
		createTable(context, classOutline, entity.getTable());
		break;
	default:
		throw new IllegalArgumentException("Unknown inheritance strategy.");
	}
}
 
Example #7
Source File: Entity.java    From celerio with Apache License 2.0 5 votes vote down vote up
public boolean is(InheritanceType strategy) {
    Assert.notNull(strategy);
    if (getInheritance() == null) {
        return false;
    }
    return strategy == getInheritance().getStrategy();
}
 
Example #8
Source File: EntityConfigFactory.java    From celerio with Apache License 2.0 5 votes vote down vote up
private void resolveMissingInheritanceStrategyOnEntityConfigs(Map<String, EntityConfig> entityConfigsByEntityName) {
    for (EntityConfig entityConfig : entityConfigsByEntityName.values()) {
        if (!entityConfig.hasInheritance()) {
            continue;
        }

        EntityConfig current = entityConfig;
        while (current.hasParentEntityName()) {
            current = entityConfigsByEntityName.get(current.getParentEntityName().toUpperCase());
            Assert.notNull(current, "The parent entity " + current.getParentEntityName() + " could not be found in the configuration.");
        }
        // root may use default...
        if (!current.getInheritance().hasStrategy()) {
            // default...
            current.getInheritance().setStrategy(InheritanceType.SINGLE_TABLE);
        }

        if (entityConfig.getInheritance().hasStrategy()) {
            Assert.isTrue(
                    entityConfig.getInheritance().getStrategy() == current.getInheritance().getStrategy(),
                    "The entityConfig " + entityConfig.getEntityName()
                            + " must not declare an inheritance strategy that is different from the strategy declared in the root entity "
                            + current.getEntityName());
        }

        // for internal convenient purposes we propagate it
        entityConfig.getInheritance().setStrategy(current.getInheritance().getStrategy());
    }
}
 
Example #9
Source File: MetadataImpl.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected String getEntityNameForIdGeneration(MetaClass metaClass) {
    List<MetaClass> persistentAncestors = metaClass.getAncestors().stream()
            .filter(mc -> tools.isPersistent(mc)) // filter out all mapped superclasses
            .collect(Collectors.toList());
    if (persistentAncestors.size() > 0) {
        MetaClass root = persistentAncestors.get(persistentAncestors.size() - 1);
        Class<?> javaClass = root.getJavaClass();
        Inheritance inheritance = javaClass.getAnnotation(Inheritance.class);
        if (inheritance == null || inheritance.strategy() != InheritanceType.TABLE_PER_CLASS) {
            // use root of inheritance tree if the strategy is JOINED or SINGLE_TABLE because ID is stored in the root table
            return root.getName();
        }
    }
    return metaClass.getName();
}
 
Example #10
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithEmbeddedId() throws Exception {
    // GIVEN
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameB = "SubEntityClass";
    final String compositeIdPropertyName = "compositeKey";
    final String id1PropertyName = "key1";
    final String id2PropertyName = "key2";

    final JPackage jp = jCodeModel.rootPackage();

    final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType");
    jIdTypeClass.annotate(Embeddable.class);
    jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName);
    jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName);

    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);
    final JMethod method = jBaseClass.method(JMod.PUBLIC, jIdTypeClass, "getCompositeKey");
    method.annotate(EmbeddedId.class);
    method.body()._return(JExpr._null());

    final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclass.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> entityClass = loadClass(testFolder.getRoot(), jSubclass.name());

    // WHEN
    final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass);

    // THEN
    assertThat(namesOfIdProperties.size(), equalTo(2));
    assertThat(namesOfIdProperties,
            hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName));
}
 
Example #11
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithEmbeddedId() throws Exception {
    // GIVEN
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameB = "SubEntityClass";
    final String compositeIdPropertyName = "compositeKey";
    final String id1PropertyName = "key1";
    final String id2PropertyName = "key2";

    final JPackage jp = jCodeModel.rootPackage();

    final JDefinedClass jIdTypeClass = jp._class(JMod.PUBLIC, "IdType");
    jIdTypeClass.annotate(Embeddable.class);
    jIdTypeClass.field(JMod.PRIVATE, Integer.class, id1PropertyName);
    jIdTypeClass.field(JMod.PRIVATE, String.class, id2PropertyName);

    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);
    jBaseClass.field(JMod.PRIVATE, jIdTypeClass, compositeIdPropertyName).annotate(EmbeddedId.class);

    final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclass.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> entityClass = loadClass(testFolder.getRoot(), jSubclass.name());

    // WHEN
    final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(entityClass);

    // THEN
    assertThat(namesOfIdProperties.size(), equalTo(2));
    assertThat(namesOfIdProperties,
            hasItems(compositeIdPropertyName + "." + id1PropertyName, compositeIdPropertyName + "." + id2PropertyName));
}
 
Example #12
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAMethodAnnotatedWithId() throws Exception {
    // GIVEN
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameB = "SubEntityClass";
    final String idPropertyName = "key";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);
    jBaseClass.method(JMod.PUBLIC, jCodeModel.VOID, "getKey").annotate(Id.class);

    final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclass.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> subClass = loadClass(testFolder.getRoot(), jSubclass.name());

    // WHEN
    final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(subClass);

    // THEN
    assertThat(namesOfIdProperties.size(), equalTo(1));
    assertThat(namesOfIdProperties, hasItem(idPropertyName));
}
 
Example #13
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNamesOfIdPropertiesFromAClassHierarchyHavingAFieldAnnotatedWithId() throws Exception {
    // GIVEN
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameB = "SubEntityClass";
    final String idPropertyName = "key";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);
    jBaseClass.field(JMod.PRIVATE, String.class, idPropertyName).annotate(Id.class);

    final JDefinedClass jSubclass = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclass.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> subClass = loadClass(testFolder.getRoot(), jSubclass.name());

    // WHEN
    final List<String> namesOfIdProperties = EntityUtils.getNamesOfIdProperties(subClass);

    // THEN
    assertThat(namesOfIdProperties.size(), equalTo(1));
    assertThat(namesOfIdProperties, hasItem(idPropertyName));
}
 
Example #14
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithJoinedInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.JOINED);

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameB),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(subClassB));
}
 
Example #15
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithTablePerClassInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final String nodeLabelBase = "ENTITY_CLASS";
    final String nodeLabelA = "ENTITY_CLASS_A";
    final String nodeLabelB = "ENTITY_CLASS_B";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Table.class).param("name", nodeLabelBase);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);
    jSubclassA.annotate(Table.class).param("name", nodeLabelA);

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);
    jSubclassB.annotate(Table.class).param("name", nodeLabelB);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabelA),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(subClassA));
}
 
Example #16
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithTablePerClassInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.TABLE_PER_CLASS);

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameB),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(subClassB));
}
 
Example #17
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheTableAnnotationWithSingleTableInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final String nodeLabel = "ENTITY_CLASS";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Table.class).param("name", nodeLabel);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE);
    jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE");

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);
    jSubclassA.annotate(DiscriminatorValue.class).param("value", "A");

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);
    jSubclassB.annotate(DiscriminatorValue.class).param("value", "B");

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(nodeLabel),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(baseClass));
}
 
Example #18
Source File: EntityUtilsTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntityClassFromNodeLabelsHavingTheLabelDeclaredByTheClassNameWithSingleTableInheritance() throws Exception {
    final String simpleClassNameBase = "EntityClass";
    final String simpleClassNameA = "SubEntityClassA";
    final String simpleClassNameB = "SubEntityClassB";

    final JPackage jp = jCodeModel.rootPackage();
    final JDefinedClass jBaseClass = jp._class(JMod.PUBLIC, simpleClassNameBase);
    jBaseClass.annotate(Entity.class);
    jBaseClass.annotate(Inheritance.class).param("strategy", InheritanceType.SINGLE_TABLE);
    jBaseClass.annotate(DiscriminatorColumn.class).param("name", "TYPE");

    final JDefinedClass jSubclassA = jp._class(JMod.PUBLIC, simpleClassNameA)._extends(jBaseClass);
    jSubclassA.annotate(Entity.class);
    jSubclassA.annotate(DiscriminatorValue.class).param("value", "A");

    final JDefinedClass jSubclassB = jp._class(JMod.PUBLIC, simpleClassNameB)._extends(jBaseClass);
    jSubclassB.annotate(Entity.class);
    jSubclassB.annotate(DiscriminatorValue.class).param("value", "B");

    buildModel(testFolder.getRoot(), jCodeModel);

    compileModel(testFolder.getRoot());

    final Class<?> baseClass = loadClass(testFolder.getRoot(), jBaseClass.name());
    final Class<?> subClassA = loadClass(testFolder.getRoot(), jSubclassA.name());
    final Class<?> subClassB = loadClass(testFolder.getRoot(), jSubclassB.name());

    final Class<?> clazz = EntityUtils.getEntityClassFromNodeLabels(Arrays.asList(simpleClassNameBase),
            Arrays.asList(baseClass, subClassA, subClassB));

    assertThat(clazz, equalTo(baseClass));
}
 
Example #19
Source File: JPAOverriddenAnnotationReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Inheritance getInheritance(Element tree, XMLContext.Default defaults) {
	Element element = tree != null ? tree.element( "inheritance" ) : null;
	if ( element != null ) {
		AnnotationDescriptor ad = new AnnotationDescriptor( Inheritance.class );
		Attribute attr = element.attribute( "strategy" );
		InheritanceType strategy = InheritanceType.SINGLE_TABLE;
		if ( attr != null ) {
			String value = attr.getValue();
			if ( "SINGLE_TABLE".equals( value ) ) {
				strategy = InheritanceType.SINGLE_TABLE;
			}
			else if ( "JOINED".equals( value ) ) {
				strategy = InheritanceType.JOINED;
			}
			else if ( "TABLE_PER_CLASS".equals( value ) ) {
				strategy = InheritanceType.TABLE_PER_CLASS;
			}
			else {
				throw new AnnotationException(
						"Unknown InheritanceType in XML: " + value + " (" + SCHEMA_VALIDATION + ")"
				);
			}
		}
		ad.setValue( "strategy", strategy );
		return AnnotationFactory.create( ad );
	}
	else if ( defaults.canUseJavaAnnotations() ) {
		return getPhysicalAnnotation( Inheritance.class );
	}
	else {
		return null;
	}
}
 
Example #20
Source File: AnnotationBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * For the mapped entities build some temporary data-structure containing information about the
 * inheritance status of a class.
 *
 * @param orderedClasses Order list of all annotated entities and their mapped superclasses
 *
 * @return A map of {@code InheritanceState}s keyed against their {@code XClass}.
 */
public static Map<XClass, InheritanceState> buildInheritanceStates(
		List<XClass> orderedClasses,
		MetadataBuildingContext buildingContext) {
	Map<XClass, InheritanceState> inheritanceStatePerClass = new HashMap<>(
			orderedClasses.size()
	);
	for ( XClass clazz : orderedClasses ) {
		InheritanceState superclassState = InheritanceState.getSuperclassInheritanceState(
				clazz, inheritanceStatePerClass
		);
		InheritanceState state = new InheritanceState( clazz, inheritanceStatePerClass, buildingContext );
		if ( superclassState != null ) {
			//the classes are ordered thus preventing an NPE
			//FIXME if an entity has subclasses annotated @MappedSperclass wo sub @Entity this is wrong
			superclassState.setHasSiblings( true );
			InheritanceState superEntityState = InheritanceState.getInheritanceStateOfSuperEntity(
					clazz, inheritanceStatePerClass
			);
			state.setHasParents( superEntityState != null );
			final boolean nonDefault = state.getType() != null && !InheritanceType.SINGLE_TABLE
					.equals( state.getType() );
			if ( superclassState.getType() != null ) {
				final boolean mixingStrategy = state.getType() != null && !state.getType()
						.equals( superclassState.getType() );
				if ( nonDefault && mixingStrategy ) {
					LOG.invalidSubStrategy( clazz.getName() );
				}
				state.setType( superclassState.getType() );
			}
		}
		inheritanceStatePerClass.put( clazz, state );
	}
	return inheritanceStatePerClass;
}
 
Example #21
Source File: InheritanceState.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void extractInheritanceType() {
	XAnnotatedElement element = getClazz();
	Inheritance inhAnn = element.getAnnotation( Inheritance.class );
	MappedSuperclass mappedSuperClass = element.getAnnotation( MappedSuperclass.class );
	if ( mappedSuperClass != null ) {
		setEmbeddableSuperclass( true );
		setType( inhAnn == null ? null : inhAnn.strategy() );
	}
	else {
		setType( inhAnn == null ? InheritanceType.SINGLE_TABLE : inhAnn.strategy() );
	}
}
 
Example #22
Source File: BaseEntityManagerFunctionalTestCase.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static boolean isSingleTable(Class<?> entity) {
  Inheritance inheritance = entity.getSuperclass().getAnnotation(Inheritance.class);
  return inheritance != null && inheritance.strategy() == InheritanceType.SINGLE_TABLE;
}
 
Example #23
Source File: InheritanceState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setType(InheritanceType type) {
	this.type = type;
}
 
Example #24
Source File: InheritanceState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public InheritanceType getType() {
	return type;
}
 
Example #25
Source File: EntityConfig.java    From celerio with Apache License 2.0 4 votes vote down vote up
public boolean is(InheritanceType strategy) {
    return hasInheritance() && getInheritance().is(strategy);
}
 
Example #26
Source File: Inheritance.java    From celerio with Apache License 2.0 4 votes vote down vote up
public boolean is(InheritanceType strategy) {
    return this.strategy == strategy;
}
 
Example #27
Source File: InheritanceState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
boolean hasDenormalizedTable() {
	return hasParents() && InheritanceType.TABLE_PER_CLASS.equals( getType() );
}
 
Example #28
Source File: InheritanceState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
boolean hasTable() {
	return !hasParents() || !InheritanceType.SINGLE_TABLE.equals( getType() );
}