use of javax.persistence.metamodel.ListAttribute in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToMany.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-N.
*/
public void testBasicWithFromJoinOneToMany() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Farm> crit = cb.createQuery(Farm.class);
Root<Farm> candidate = crit.from(Farm.class);
candidate.alias("f");
crit.select(candidate);
Metamodel model = emf.getMetamodel();
ManagedType farmType = model.managedType(Farm.class);
Attribute animalAttr = farmType.getAttribute("animals");
Join animalJoin = candidate.join((ListAttribute) animalAttr);
animalJoin.alias("a");
Path nameField = animalJoin.get("name");
Predicate nameEquals = cb.equal(nameField, "Woolly Sheep");
crit.where(nameEquals);
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT f FROM org.datanucleus.samples.annotations.one_many.bidir.Farm f JOIN f.animals a WHERE (a.name = 'Woolly Sheep')", crit.toString());
Query q = em.createQuery(crit);
List<Farm> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 1, results.size());
Farm farm = (Farm) results.get(0);
assertEquals("Farm is incorrect", "Kiwi Farm", farm.getName());
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.metamodel.ListAttribute in project hibernate-orm by hibernate.
the class AbstractFromImpl method joinList.
@Override
@SuppressWarnings({ "unchecked" })
public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute(attributeName);
if (!attribute.isCollection()) {
throw new IllegalArgumentException("Requested attribute was not a list");
}
final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
if (!PluralAttribute.CollectionType.LIST.equals(pluralAttribute.getCollectionType())) {
throw new IllegalArgumentException("Requested attribute was not a list");
}
return (ListJoin<X, Y>) join((ListAttribute) attribute, jt);
}
use of javax.persistence.metamodel.ListAttribute in project tests by datanucleus.
the class MetamodelTest method testBasic.
/**
* Test for the list of classes and their basic model info
*/
public void testBasic() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> animalType = model.entity(Animal.class);
assertNotNull(animalType);
assertEquals("Number of Animal attributes is wrong", 2, animalType.getAttributes().size());
Class idType = animalType.getIdType().getJavaType();
assertEquals(String.class, idType);
try {
// String field (Id)
Attribute attr = animalType.getAttribute("name");
assertNotNull(attr);
assertEquals(attr.getName(), "name");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "name");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
assertTrue(attr instanceof SingularAttribute);
assertFalse(((SingularAttribute) attr).isOptional());
assertFalse(((SingularAttribute) attr).isVersion());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"name\" field of " + Animal.class.getName());
}
try {
// N-1 field
Attribute attr = animalType.getAttribute("farm");
assertNotNull(attr);
assertEquals(attr.getName(), "farm");
assertEquals(attr.getJavaType(), Farm.class);
assertEquals(attr.getJavaMember().getName(), "farm");
assertFalse(attr.isCollection());
assertTrue(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"farm\" field of " + Animal.class.getName());
}
// Supertype should be null
assertNull(animalType.getSupertype());
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Animal.class.getName());
}
try {
EntityType<?> farmType = model.entity(Farm.class);
assertNotNull(farmType);
assertEquals("Number of Farm attributes is wrong", 2, farmType.getAttributes().size());
try {
// String field
Attribute attr = farmType.getAttribute("name");
assertNotNull(attr);
assertEquals(attr.getName(), "name");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "name");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"name\" field of " + Farm.class.getName());
}
try {
// N-1 field
Attribute attr = farmType.getAttribute("animals");
assertNotNull(attr);
assertEquals(attr.getName(), "animals");
assertEquals(attr.getJavaType(), ArrayList.class);
assertEquals(attr.getJavaMember().getName(), "animals");
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
assertTrue("Attribute for animals is not castable to ListAttribute!", attr instanceof ListAttribute);
ListAttribute listAttr = (ListAttribute) attr;
Type elemType = listAttr.getElementType();
assertEquals("Element type is wrong", Animal.class, elemType.getJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"animals\" field of " + Farm.class.getName());
}
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Farm.class.getName());
}
}
Aggregations