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;
}
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();
}
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());
}
}
Aggregations