use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class MetamodelTest method testEmbedded.
/**
* Test for embedded types with metamodel.
*/
public void testEmbedded() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> computerType = model.entity(Computer.class);
assertNotNull(computerType);
assertEquals("Number of attributes is wrong", 4, computerType.getAttributes().size());
Class idType = computerType.getIdType().getJavaType();
assertEquals(long.class, idType);
try {
// String field (Id)
Attribute attr = computerType.getAttribute("soundCard");
assertNotNull(attr);
assertEquals(attr.getName(), "soundCard");
assertEquals(attr.getJavaType(), ComputerCard.class);
assertEquals(attr.getJavaMember().getName(), "soundCard");
assertFalse(attr.isCollection());
assertTrue(attr.isAssociation());
assertEquals(Attribute.PersistentAttributeType.EMBEDDED, attr.getPersistentAttributeType());
assertTrue(attr instanceof SingularAttribute);
SingularAttribute sattr = (SingularAttribute) attr;
assertTrue(sattr.isOptional());
assertFalse(sattr.isVersion());
assertEquals(BindableType.SINGULAR_ATTRIBUTE, sattr.getBindableType());
assertEquals(ComputerCard.class, sattr.getBindableJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"soundCard\" field of " + Computer.class.getName());
}
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Computer.class.getName());
}
}
use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class MetamodelTest method testMap.
/**
* Test for class with Map field(s).
*/
public void testMap() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> mapHolderType = model.entity(MapJoinHolder.class);
assertNotNull(mapHolderType);
assertEquals("Number of attributes is wrong", 6, mapHolderType.getAttributes().size());
try {
// long field (id)
Attribute attr = mapHolderType.getAttribute("id");
assertNotNull(attr);
assertTrue(attr instanceof SingularAttribute);
assertEquals("id", attr.getName());
assertEquals(long.class, attr.getJavaType());
assertEquals("id", attr.getJavaMember().getName());
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 \"id\" field of " + MapJoinHolder.class.getName());
}
try {
// String field (name)
Attribute attr = mapHolderType.getAttribute("name");
assertNotNull(attr);
assertTrue(attr instanceof SingularAttribute);
assertEquals("name", attr.getName());
assertEquals(String.class, attr.getJavaType());
assertEquals("name", attr.getJavaMember().getName());
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
assertTrue(attr instanceof SingularAttribute);
assertTrue(((SingularAttribute) attr).isOptional());
assertFalse(((SingularAttribute) attr).isVersion());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"name\" field of " + MapJoinHolder.class.getName());
}
try {
// Map<NonPC, PC>
Attribute attr = mapHolderType.getAttribute("map");
assertNotNull(attr);
assertTrue(attr instanceof MapAttribute);
MapAttribute mapAttr = (MapAttribute) attr;
assertEquals("map", attr.getName());
assertEquals(Map.class, attr.getJavaType());
assertEquals("map", attr.getJavaMember().getName());
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
assertEquals(String.class.getName(), mapAttr.getKeyJavaType().getName());
Type valueType = mapAttr.getElementType();
assertEquals(MapJoinValue.class.getName(), valueType.getJavaType().getName());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"map\" field of " + MapJoinHolder.class.getName());
}
try {
// Map<NonPC, NonPC>
Attribute attr = mapHolderType.getAttribute("map2");
assertNotNull(attr);
assertTrue(attr instanceof MapAttribute);
MapAttribute mapAttr = (MapAttribute) attr;
assertEquals("map2", attr.getName());
assertEquals(Map.class, attr.getJavaType());
assertEquals("map2", attr.getJavaMember().getName());
assertTrue(attr.isCollection());
assertFalse(attr.isAssociation());
assertEquals(Integer.class.getName(), mapAttr.getKeyJavaType().getName());
Type valueType = mapAttr.getElementType();
assertEquals(String.class.getName(), valueType.getJavaType().getName());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"map2\" field of " + MapJoinHolder.class.getName());
}
try {
// Map<NonPC, PC>
Attribute attr = mapHolderType.getAttribute("map3");
assertNotNull(attr);
assertTrue(attr instanceof MapAttribute);
MapAttribute mapAttr = (MapAttribute) attr;
assertEquals("map3", attr.getName());
assertEquals(Map.class, attr.getJavaType());
assertEquals("map3", attr.getJavaMember().getName());
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
assertEquals(String.class.getName(), mapAttr.getKeyJavaType().getName());
Type valueType = mapAttr.getElementType();
assertEquals(MapJoinEmbeddedValue.class.getName(), valueType.getJavaType().getName());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"map3\" field of " + MapJoinHolder.class.getName());
}
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Animal.class.getName());
}
}
use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class MetamodelTest method testManyToOne.
/**
* Test for the identification of relation type.
*/
public void testManyToOne() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> ownerType = model.entity(ManyOneOwner.class);
assertNotNull(ownerType);
assertEquals("Number of attributes is wrong", 2, ownerType.getAttributes().size());
try {
Attribute attr = ownerType.getAttribute("other");
assertNotNull(attr);
assertEquals(attr.getName(), "other");
assertFalse(attr.isCollection());
assertTrue(attr instanceof SingularAttribute);
assertEquals(Attribute.PersistentAttributeType.MANY_TO_ONE, attr.getPersistentAttributeType());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"other\" field of " + ManyOneOwner.class.getName());
}
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + ManyOneOwner.class.getName());
}
}
use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToOne.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-1.
*/
public void testBasicWithFromJoinOneToOne() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Employee> crit = cb.createQuery(Employee.class);
Root<Employee> candidate = crit.from(Employee.class);
candidate.alias("e");
crit.select(candidate);
Metamodel model = emf.getMetamodel();
ManagedType empType = model.managedType(Employee.class);
Attribute bAttr = empType.getAttribute("account");
Join accountJoin = candidate.join((SingularAttribute) bAttr);
accountJoin.alias("a");
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT e FROM org.datanucleus.samples.annotations.models.company.Employee e JOIN e.account a", crit.toString());
Query q = em.createQuery(crit);
List<Employee> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
Iterator<Employee> iter = results.iterator();
boolean joeExists = false;
boolean nigelExists = false;
while (iter.hasNext()) {
Employee emp = iter.next();
if (emp.getFirstName().equals("Nigel") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 106) {
nigelExists = true;
} else if (emp.getFirstName().equals("Joe") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 105) {
joeExists = true;
}
}
assertTrue("Nigel not present", nigelExists);
assertTrue("Joe not present", joeExists);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of javax.persistence.metamodel.Attribute 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();
}
}
Aggregations