use of jakarta.persistence.metamodel.MappedSuperclassType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testIdentifiableType_getIdType_Method.
public void testIdentifiableType_getIdType_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull(metamodel);
EntityTypeImpl<Manufacturer> entityManufacturer_ = (EntityTypeImpl) metamodel.entity(Manufacturer.class);
assertNotNull(entityManufacturer_);
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
EntityTypeImpl<Computer> entityComputer_ = (EntityTypeImpl) metamodel.entity(Computer.class);
assertNotNull(entityComputer_);
// Actual Test Case
/**
* Return the type that represents the type of the id.
* @return type of id
*/
// Type<?> getIdType();
// Test EntityType
// Test normal path for an [Embeddable] type via @EmbeddedId
expectedIAExceptionThrown = false;
Type<?> locationIdType = null;
try {
locationIdType = entityLocation_.getIdType();
} catch (IllegalArgumentException iae) {
// expecting no exception
iae.printStackTrace();
expectedIAExceptionThrown = true;
}
assertFalse(expectedIAExceptionThrown);
assertNotNull(locationIdType);
assertEquals(PersistenceType.EMBEDDABLE, locationIdType.getPersistenceType());
assertEquals(EmbeddedPK.class, locationIdType.getJavaType());
// check that the elementType and the owningType (managedType) are set correctly
// See issue 50 where some mapping types were not setting the elementType correctly (this includes aggregate types like Embeddable)
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_50:_20090727:_Handle_all_mapping_types_in_the_SingularAttribute_constructor
// Get the ManagedType and check this SingularAttribute PK
Attribute locationIdAttribute = entityLocation_.getAttribute("primaryKey");
assertNotNull(locationIdAttribute);
assertTrue(locationIdAttribute instanceof SingularAttributeImpl);
assertFalse(locationIdAttribute.isCollection());
// non-spec.
assertFalse(((AttributeImpl) locationIdAttribute).isPlural());
ManagedType locationIdAttributeManagedType = locationIdAttribute.getDeclaringType();
assertEquals(entityLocation_, locationIdAttributeManagedType);
ManagedTypeImpl locationIdAttributeManagedTypeImpl = ((SingularAttributeImpl) locationIdAttribute).getManagedTypeImpl();
assertEquals(locationIdType.getJavaType(), ((SingularAttributeImpl) locationIdAttribute).getBindableJavaType());
assertEquals(Bindable.BindableType.SINGULAR_ATTRIBUTE, ((SingularAttributeImpl) locationIdAttribute).getBindableType());
assertEquals(locationIdType.getJavaType(), locationIdAttribute.getJavaType());
Type embeddableType = ((SingularAttributeImpl) locationIdAttribute).getType();
assertNotNull(embeddableType);
assertNotSame(embeddableType, locationIdAttributeManagedType);
// Test normal path for a [Basic] type
expectedIAExceptionThrown = false;
Type<?> computerIdType = null;
try {
computerIdType = entityComputer_.getIdType();
} catch (IllegalArgumentException iae) {
// expecting no exception
iae.printStackTrace();
expectedIAExceptionThrown = true;
}
assertFalse(expectedIAExceptionThrown);
assertNotNull(computerIdType);
assertEquals(PersistenceType.BASIC, computerIdType.getPersistenceType());
assertEquals(Integer.class, computerIdType.getJavaType());
// Test MappedSuperclassType
// Test normal path for a [Basic] type
expectedIAExceptionThrown = false;
Type<?> personIdType = null;
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
MappedSuperclassTypeImpl<Corporation> msCorporation_ = (MappedSuperclassTypeImpl) metamodel.managedType(Corporation.class);
assertNotNull(msCorporation_);
// Verify all types (entities, embeddables, mappedsuperclasses and basic)
// get all 21 types (a non spec function - for testing introspection)
Map<String, TypeImpl<?>> typesMap = ((MetamodelImpl) metamodel).getTypes();
// verify each one
assertNotNull(typesMap);
((MetamodelImpl) metamodel).printAllTypes();
// Note: Since BasicTypes are lazy - loaded into the metamodel-types Map - this test must preceed any test that verifies all BasicType objects like "testIdentifiableType_getIdType_Method"
// You will get a lower number here - if only this single test is run via the Testing Browser
assertEquals(METAMODEL_ALL_TYPES, typesMap.size());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
use of jakarta.persistence.metamodel.MappedSuperclassType in project hibernate-orm by hibernate.
the class MetadataTest method testHierarchy.
@Test
public void testHierarchy(EntityManagerFactoryScope scope) {
final EntityType<Cat> cat = scope.getEntityManagerFactory().getMetamodel().entity(Cat.class);
assertNotNull(cat);
assertEquals(7, cat.getAttributes().size());
assertEquals(1, cat.getDeclaredAttributes().size());
ensureProperMember(cat.getDeclaredAttributes());
assertTrue(cat.hasVersionAttribute());
assertEquals("version", cat.getVersion(Long.class).getName());
verifyDeclaredVersionNotPresent(cat);
verifyDeclaredIdNotPresentAndIdPresent(cat);
assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, cat.getSupertype().getPersistenceType());
MappedSuperclassType cattish = (MappedSuperclassType) cat.getSupertype();
assertEquals(6, cattish.getAttributes().size());
assertEquals(1, cattish.getDeclaredAttributes().size());
ensureProperMember(cattish.getDeclaredAttributes());
assertTrue(cattish.hasVersionAttribute());
assertEquals("version", cattish.getVersion(Long.class).getName());
verifyDeclaredVersionNotPresent(cattish);
verifyDeclaredIdNotPresentAndIdPresent(cattish);
assertEquals(Type.PersistenceType.ENTITY, cattish.getSupertype().getPersistenceType());
EntityType<Feline> feline = (EntityType<Feline>) cattish.getSupertype();
assertEquals(5, feline.getAttributes().size());
assertEquals(1, feline.getDeclaredAttributes().size());
ensureProperMember(feline.getDeclaredAttributes());
assertTrue(feline.hasVersionAttribute());
assertEquals("version", feline.getVersion(Long.class).getName());
verifyDeclaredVersionNotPresent(feline);
verifyDeclaredIdNotPresentAndIdPresent(feline);
assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, feline.getSupertype().getPersistenceType());
MappedSuperclassType animal = (MappedSuperclassType) feline.getSupertype();
assertEquals(4, animal.getAttributes().size());
assertEquals(2, animal.getDeclaredAttributes().size());
ensureProperMember(animal.getDeclaredAttributes());
assertTrue(animal.hasVersionAttribute());
assertEquals("version", animal.getVersion(Long.class).getName());
verifyDeclaredVersionNotPresent(animal);
assertEquals("id", animal.getId(Long.class).getName());
final SingularAttribute<Animal, Long> id = animal.getDeclaredId(Long.class);
assertEquals("id", id.getName());
assertNotNull(id.getJavaMember());
assertEquals(Type.PersistenceType.MAPPED_SUPERCLASS, animal.getSupertype().getPersistenceType());
MappedSuperclassType<Thing> thing = (MappedSuperclassType<Thing>) animal.getSupertype();
assertEquals(2, thing.getAttributes().size());
assertEquals(2, thing.getDeclaredAttributes().size());
ensureProperMember(thing.getDeclaredAttributes());
final SingularAttribute<Thing, Double> weight = thing.getDeclaredSingularAttribute("weight", Double.class);
assertEquals(Double.class, weight.getJavaType());
assertEquals("version", thing.getVersion(Long.class).getName());
final SingularAttribute<Thing, Long> version = thing.getDeclaredVersion(Long.class);
assertEquals("version", version.getName());
assertNotNull(version.getJavaMember());
assertNull(thing.getId(Long.class));
assertNull(thing.getSupertype());
}
use of jakarta.persistence.metamodel.MappedSuperclassType in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testIdentifiableType_getIdClassAttributesAcrossMappedSuperclassChain_Method.
public void testIdentifiableType_getIdClassAttributesAcrossMappedSuperclassChain_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull(metamodel);
// Actual Test Case
/**
* Return the attributes corresponding to the id class of the
* identifiable type.
* @return id attributes
* @throws IllegalArgumentException if the identifiable type
* does not have an id class
*/
// java.util.Set<SingularAttribute<? super X, ?>> getIdClassAttributes();
// @IdClass - test normal path
expectedIAExceptionThrown = false;
boolean hasSingleIdAttribute = true;
// 0 id attributes here
Set<SingularAttribute<? super MS_MS_Entity_Leaf, ?>> idClassAttributesLeaf = null;
IdentifiableType<MS_MS_Entity_Leaf> aTypeLeaf = metamodel.entity(MS_MS_Entity_Leaf.class);
// 1 id attribute here
Set<SingularAttribute<? super MS_MS_Entity_Center, ?>> idClassAttributesCenter = null;
MappedSuperclassType<MS_MS_Entity_Center> aTypeCenter = (MappedSuperclassType) metamodel.managedType(MS_MS_Entity_Center.class);
// 3 id attributes here
Set<SingularAttribute<? super MS_MS_Entity_Root, ?>> idClassAttributesRoot = null;
MappedSuperclassType<MS_MS_Entity_Root> aTypeRoot = (MappedSuperclassType) metamodel.managedType(MS_MS_Entity_Root.class);
hasSingleIdAttribute = aTypeLeaf.hasSingleIdAttribute();
// We verify that an an @IdClass exists above
// This tests the IdentifiableType part of the transaction for DI 78
assertFalse(hasSingleIdAttribute);
hasSingleIdAttribute = aTypeCenter.hasSingleIdAttribute();
// We verify that an one part of an @IdClass exists
// This tests the IdentifiableType part of the transaction for DI 78
assertTrue(hasSingleIdAttribute);
hasSingleIdAttribute = aTypeRoot.hasSingleIdAttribute();
// We verify that an @IdClass exists - no single @Id or @EmbeddedId exists
// This tests the IdentifiableType part of the transaction for DI 78
assertFalse(hasSingleIdAttribute);
// idClassAttributesLeaf = aTypeLeaf.getIdClassAttributes(); // expected IAE - leave commented
idClassAttributesCenter = aTypeCenter.getIdClassAttributes();
assertNotNull(idClassAttributesCenter);
assertEquals(1, idClassAttributesCenter.size());
// The following call is not valid because the IdClass attribute is defined one level below
try {
idClassAttributesRoot = aTypeRoot.getIdClassAttributes();
} catch (IllegalArgumentException iae) {
expectedIAExceptionThrown = true;
}
assertTrue(expectedIAExceptionThrown);
expectedIAExceptionThrown = false;
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
Aggregations