use of javax.persistence.metamodel.SingularAttribute 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());
}
}
use of javax.persistence.metamodel.SingularAttribute in project tests by datanucleus.
the class MetamodelTest method testInheritanceOfVersion.
/**
* Test for the case with inheritance.
*/
public void testInheritanceOfVersion() {
Metamodel model = emf.getMetamodel();
// Check base class which has the version
try {
EntityType<?> empType = model.entity(VersionedPerson.class);
assertNotNull(empType);
SingularAttribute verAttr = empType.getVersion(long.class);
assertNotNull(verAttr);
assertEquals("version", verAttr.getName());
assertEquals(long.class, verAttr.getJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + VersionedPerson.class.getName());
}
// Check sub-class which doesn't have the version
try {
EntityType<?> empType = model.entity(VersionedEmployee.class);
assertNotNull(empType);
SingularAttribute verAttr = empType.getVersion(long.class);
assertNotNull(verAttr);
assertEquals("version", verAttr.getName());
assertEquals(long.class, verAttr.getJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + VersionedEmployee.class.getName());
}
}
use of javax.persistence.metamodel.SingularAttribute in project tests by datanucleus.
the class MetamodelTest method testGenericIdInheritance.
/**
* Test for use of a generic id in the base class.
*/
public void testGenericIdInheritance() {
Metamodel model = emf.getMetamodel();
try {
EntityType<GenericIdSub> subType = model.entity(GenericIdSub.class);
assertNotNull(subType);
Class idType = subType.getIdType().getJavaType();
assertEquals(Long.class, idType);
SingularAttribute idAttr = subType.getId(Long.class);
assertNotNull(idAttr);
assertEquals("id", idAttr.getName());
// TODO Add this check when metamodel supports generic type info
// assertEquals(Long.class, idAttr.getJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + GenericIdSub.class.getName());
}
}
use of javax.persistence.metamodel.SingularAttribute in project hibernate-search by hibernate.
the class HibernateSearchPartitionMapper method buildPartitionUnitsFrom.
// Can't do much better without adding generics to EntityTypeDescriptor
@SuppressWarnings("unchecked")
private List<PartitionBound> buildPartitionUnitsFrom(EntityManagerFactory emf, StatelessSession ss, EntityTypeDescriptor entityTypeDescriptor, Integer maxResults, int fetchSize, int rowsPerPartition, IndexScope indexScope) {
Class<?> javaClass = entityTypeDescriptor.getJavaClass();
List<PartitionBound> partitionUnits = new ArrayList<>();
Object lowerID;
Object upperID = null;
CriteriaBuilder builder = emf.getCriteriaBuilder();
CriteriaQuery<?> criteria = builder.createQuery();
Root<?> root = criteria.from(javaClass);
entityTypeDescriptor.getIdOrder().addAscOrder(builder, criteria, root);
EntityType<?> model = root.getModel();
Class<?> javaType = model.getIdType().getJavaType();
@SuppressWarnings("rawtypes") SingularAttribute singularAttribute = model.getId(javaType);
criteria.select(root.get(singularAttribute));
Query<?> query = ss.createQuery(criteria);
query.setFetchSize(fetchSize).setReadOnly(true).setCacheable(false).setLockMode(LockModeType.NONE);
if (maxResults != null) {
query.setMaxResults(maxResults);
}
try (ScrollableResults scroll = query.scroll(ScrollMode.SCROLL_SENSITIVE)) {
/*
* The scroll results are originally positioned *before* the first element,
* so we need to scroll rowsPerPartition + 1 positions to advanced to the
* upper bound of the first partition, whereas for the next partitions
* we only need to advance rowsPerPartition positions.
* This handle the special case of the first partition.
*/
scroll.next();
while (scroll.scroll(rowsPerPartition)) {
lowerID = upperID;
upperID = scroll.get(0);
partitionUnits.add(new PartitionBound(javaClass, lowerID, upperID, indexScope));
}
// add an additional partition on the tail
lowerID = upperID;
upperID = null;
partitionUnits.add(new PartitionBound(javaClass, lowerID, upperID, indexScope));
return partitionUnits;
}
}
use of javax.persistence.metamodel.SingularAttribute in project igloo-parent by igloo-project.
the class AbstractTestCase method testMetaModel.
/**
* Méthode utilisée à des fins de tests.
*/
protected void testMetaModel(Attribute<?, ?> attribute, List<Class<?>> classesAutorisees, List<Attribute<?, ?>> ignoredAttributes) throws NoSuchFieldException, SecurityException {
for (Attribute<?, ?> ignoredAttribute : ignoredAttributes) {
if (ignoredAttribute.getJavaMember().equals(attribute.getJavaMember())) {
// champ ignoré
return;
}
}
Enumerated enumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(Enumerated.class);
MapKeyEnumerated mapKeyEnumerated = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKeyEnumerated.class);
MapKey mapKey = attribute.getJavaMember().getDeclaringClass().getDeclaredField(attribute.getName()).getAnnotation(MapKey.class);
// cas des embeddable et des collectionOfElements d'embeddable
if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && EmbeddableTypeImpl.class.isInstance(((PluralAttribute<?, ?, ?>) attribute).getElementType())) {
PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
if (classesAutorisees.contains(pluralAttribute.getElementType().getJavaType())) {
// type autorisé de manière explicite
return;
}
for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) pluralAttribute.getElementType()).getAttributes()) {
testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
}
return;
} else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)) {
SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attribute;
if (classesAutorisees.contains(singularAttribute.getJavaType())) {
// type autorisé de manière explicite
return;
}
if (EmbeddableTypeImpl.class.isInstance(singularAttribute.getType())) {
for (Attribute<?, ?> embeddedAttribute : ((EmbeddableTypeImpl<?>) singularAttribute.getType()).getAttributes()) {
testMetaModel(embeddedAttribute, classesAutorisees, ignoredAttributes);
}
return;
}
}
if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.BASIC) && !classesAutorisees.contains(attribute.getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
throw new IllegalStateException("Champ \"" + attribute.getName() + "\", de type " + attribute.getJavaType().getSimpleName() + " refusé");
} else if (attribute.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION) && PluralAttribute.class.isInstance(attribute) && !classesAutorisees.contains(((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>) attribute;
throw new IllegalStateException("Collection \"" + attribute.getName() + "\" de " + pluralAttribute.getElementType().getJavaType().getSimpleName() + " refusée");
} else if (attribute instanceof MapAttribute) {
MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attribute;
if (Enum.class.isAssignableFrom(mapAttribute.getKeyJavaType()) && (mapKeyEnumerated == null || EnumType.ORDINAL.equals(mapKeyEnumerated.value())) && mapKey == null) /* if @MapKey present, then field format is defined elsewhere and check is useless */
{
throw new IllegalStateException("Map \"" + attribute.getName() + "\" de clés ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
}
if (Enum.class.isAssignableFrom(mapAttribute.getElementType().getJavaType()) && (enumerated == null || EnumType.ORDINAL.equals(enumerated.value()))) {
throw new IllegalStateException("Map \"" + attribute.getName() + "\" de valeurs ordinales " + ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType().getSimpleName() + " refusée");
}
}
}
Aggregations