Search in sources :

Example 1 with IdentifiableType

use of javax.persistence.metamodel.IdentifiableType in project hibernate-orm by hibernate.

the class PluralAttributePath method locateNearestSubclassEntity.

private EntityType locateNearestSubclassEntity(MappedSuperclassType mappedSuperclassType, EntityType entityTypeTop) {
    EntityType entityTypeNearestDeclaringType = entityTypeTop;
    IdentifiableType superType = entityTypeNearestDeclaringType.getSupertype();
    while (superType != mappedSuperclassType) {
        if (superType == null) {
            throw new IllegalStateException(String.format("Cannot determine nearest EntityType extending mapped superclass [%s] starting from [%s]; a supertype of [%s] is null", mappedSuperclassType.getJavaType().getName(), entityTypeTop.getJavaType().getName(), entityTypeTop.getJavaType().getName()));
        }
        if (superType.getPersistenceType() == Type.PersistenceType.ENTITY) {
            entityTypeNearestDeclaringType = (EntityType) superType;
        }
        superType = superType.getSupertype();
    }
    return entityTypeNearestDeclaringType;
}
Also used : EntityType(javax.persistence.metamodel.EntityType) IdentifiableType(javax.persistence.metamodel.IdentifiableType)

Example 2 with IdentifiableType

use of javax.persistence.metamodel.IdentifiableType in project webpieces by deanhiller.

the class HibernateLookup method findEntityImpl.

private <T> T findEntityImpl(ParamMeta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate, EntityManager entityManager) {
    ParamMeta m = paramMeta;
    Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
    Metamodel metamodel = entityManager.getMetamodel();
    ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
    IdentifiableType<T> entityType = (IdentifiableType<T>) managedType;
    Class<?> idClazz = entityType.getIdType().getJavaType();
    SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
    String name = idAttribute.getName();
    ParamNode paramNode = tree.get(name);
    String value = null;
    if (paramNode != null) {
        if (!(paramNode instanceof ValueNode))
            throw new IllegalStateException("The id field in the hibernate entity should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ".  bad multipart form?  (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
        ValueNode node = (ValueNode) paramNode;
        value = node.getValue();
    }
    if (value == null)
        return beanCreate.apply(paramTypeToCreate);
    @SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
    Object id = unmarshaller.stringToObject(value);
    UseQuery namedQuery = fetchUseQuery(m.getAnnotations());
    if (namedQuery == null)
        return entityManager.find(paramTypeToCreate, id);
    Query query = entityManager.createNamedQuery(namedQuery.value());
    query.setParameter(namedQuery.id(), id);
    return (T) query.getSingleResult();
}
Also used : IdentifiableType(javax.persistence.metamodel.IdentifiableType) Query(javax.persistence.Query) ParamNode(org.webpieces.router.impl.params.ParamNode) ParamMeta(org.webpieces.router.api.extensions.ParamMeta) ValueNode(org.webpieces.router.impl.params.ValueNode) Metamodel(javax.persistence.metamodel.Metamodel) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter)

Example 3 with IdentifiableType

use of javax.persistence.metamodel.IdentifiableType in project tests by datanucleus.

the class MetamodelTest method testInheritance.

/**
 * Test for the case with inheritance.
 */
public void testInheritance() {
    Metamodel model = emf.getMetamodel();
    try {
        EntityType<?> mgrType = model.entity(Manager.class);
        assertNotNull(mgrType);
        assertEquals("Number of Manager attributes is wrong", 16, mgrType.getAttributes().size());
        assertEquals("Number of Manager singularAttributes is wrong", 13, mgrType.getSingularAttributes().size());
        assertEquals("Number of Manager pluralAttributes is wrong", 3, mgrType.getPluralAttributes().size());
        try {
            // Field in Manager
            Attribute attr = mgrType.getAttribute("subordinates");
            assertNotNull(attr);
            assertEquals(attr.getName(), "subordinates");
            assertEquals(attr.getJavaType(), Set.class);
            assertEquals(attr.getJavaMember().getName(), "subordinates");
            assertTrue(attr.isCollection());
            assertTrue(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"subordinates\" field of " + Manager.class.getName());
        }
        try {
            // Field in Employee
            Attribute attr = mgrType.getAttribute("serialNo");
            assertNotNull(attr);
            assertEquals(attr.getName(), "serialNo");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "serialNo");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"serialNo\" field of " + Employee.class.getName());
        }
        try {
            // Primitive Field in Employee
            Attribute attr = mgrType.getAttribute("salary");
            assertNotNull(attr);
            assertEquals(attr.getName(), "salary");
            assertEquals(attr.getJavaType(), float.class);
            assertEquals(attr.getJavaMember().getName(), "salary");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
            assertTrue(attr instanceof SingularAttribute);
            assertFalse(((SingularAttribute) attr).isOptional());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"salary\" field of " + Employee.class.getName());
        }
        try {
            // Field in Person
            Attribute attr = mgrType.getAttribute("firstName");
            assertNotNull(attr);
            assertEquals(attr.getName(), "firstName");
            assertEquals(attr.getJavaType(), String.class);
            assertEquals(attr.getJavaMember().getName(), "firstName");
            assertFalse(attr.isCollection());
            assertFalse(attr.isAssociation());
        } catch (IllegalArgumentException iae) {
            fail("Didnt find Attribute for \"firstName\" field of " + Person.class.getName());
        }
        // Supertype should not be null
        IdentifiableType empType = mgrType.getSupertype();
        assertNotNull(empType);
        IdentifiableType persType = empType.getSupertype();
        assertNotNull(persType);
        IdentifiableType superType = persType.getSupertype();
        assertNull(superType);
    } catch (IllegalArgumentException iae) {
        fail("Didnt find EntityType for " + Manager.class.getName());
    }
    try {
        EntityType<?> mugType = model.entity(ModeratedUserGroup.class);
        assertNotNull(mugType);
        assertNotNull(mugType.getId(long.class));
        assertEquals("id", mugType.getId(long.class).getName());
    } catch (IllegalArgumentException iae) {
        fail("Error in metamodel tests" + iae.getMessage());
    }
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) VersionedEmployee(org.datanucleus.samples.annotations.versioned.VersionedEmployee) Employee(org.datanucleus.samples.annotations.models.company.Employee) IdentifiableType(javax.persistence.metamodel.IdentifiableType) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Manager(org.datanucleus.samples.annotations.models.company.Manager) Metamodel(javax.persistence.metamodel.Metamodel) VersionedPerson(org.datanucleus.samples.annotations.versioned.VersionedPerson) Person(org.datanucleus.samples.annotations.models.company.Person)

Aggregations

IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Metamodel (javax.persistence.metamodel.Metamodel)2 Query (javax.persistence.Query)1 Attribute (javax.persistence.metamodel.Attribute)1 EntityType (javax.persistence.metamodel.EntityType)1 ListAttribute (javax.persistence.metamodel.ListAttribute)1 MapAttribute (javax.persistence.metamodel.MapAttribute)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 Employee (org.datanucleus.samples.annotations.models.company.Employee)1 Manager (org.datanucleus.samples.annotations.models.company.Manager)1 Person (org.datanucleus.samples.annotations.models.company.Person)1 VersionedEmployee (org.datanucleus.samples.annotations.versioned.VersionedEmployee)1 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)1 ObjectStringConverter (org.webpieces.router.api.extensions.ObjectStringConverter)1 ParamMeta (org.webpieces.router.api.extensions.ParamMeta)1 ParamNode (org.webpieces.router.impl.params.ParamNode)1 ValueNode (org.webpieces.router.impl.params.ValueNode)1