use of jakarta.persistence.metamodel.Attribute in project eclipselink by eclipse-ee4j.
the class AdvancedJPAJunitTest method testMetamodelMinimalSanityTest.
/**
* This test performs minimal sanity testing on the advanced JPA model
* in order to verify metamodel creation.<p>
* See the metamodel test package suite for full regression tests.
* See SVN rev# 5124
* http://fisheye2.atlassian.com/changelog/~author=mobrien/eclipselink/?cs=5124
*/
public void testMetamodelMinimalSanityTest() {
EntityManager em = createEntityManager();
// pre-clear metamodel to enable test reentry (SE only - not EE)
if (!isOnServer()) {
((EntityManagerFactoryDelegate) em.getEntityManagerFactory()).setMetamodel(null);
}
Metamodel metamodel = em.getMetamodel();
// get declared attributes
EntityType<LargeProject> entityLargeProject = metamodel.entity(LargeProject.class);
Set<Attribute<LargeProject, ?>> declaredAttributes = entityLargeProject.getDeclaredAttributes();
// instead of a assertEquals(1, size) for future compatibility with changes to Buyer
assertTrue(declaredAttributes.size() > 0);
// check that getDeclaredAttribute and getDeclaredAttributes return the same attribute
Attribute<LargeProject, ?> budgetAttribute = entityLargeProject.getDeclaredAttribute("budget");
assertNotNull(budgetAttribute);
Attribute<LargeProject, ?> budgetSingularAttribute = entityLargeProject.getDeclaredSingularAttribute("budget");
assertNotNull(budgetSingularAttribute);
assertEquals(budgetSingularAttribute, budgetAttribute);
assertTrue(declaredAttributes.contains(budgetSingularAttribute));
// check the type
Class<?> budgetClass = budgetSingularAttribute.getJavaType();
// Verify whether we expect a boxed class or not
assertEquals(double.class, budgetClass);
// assertEquals(Double.class, budgetClass);
// Test LargeProject.budget.buyingDays
// Check an EnumSet on an Entity
EntityType<Buyer> entityBuyer = metamodel.entity(Buyer.class);
// public enum Weekdays { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
// private EnumSet<Weekdays> buyingDays;
assertNotNull(entityBuyer);
// check persistence type
assertEquals(PersistenceType.ENTITY, entityBuyer.getPersistenceType());
assertEquals(Buyer.class, entityBuyer.getJavaType());
// verify EnumSet is a SingularAttribute
Attribute<? super Buyer, ?> buyingDaysAttribute = entityBuyer.getAttribute("buyingDays");
assertNotNull(buyingDaysAttribute);
// Check persistent attribute type
assertEquals(PersistentAttributeType.BASIC, buyingDaysAttribute.getPersistentAttributeType());
// Non-spec check on the attribute impl type
// EnumSet is not a Set in the Metamodel - it is a treated as a BasicType single object (SingularAttributeType)
// BasicTypeImpl@8980685:EnumSet [ javaType: class java.util.EnumSet]
assertFalse(((SingularAttributeImpl) buyingDaysAttribute).isPlural());
BindableType buyingDaysElementBindableType = ((SingularAttributeImpl) buyingDaysAttribute).getBindableType();
assertEquals(BindableType.SINGULAR_ATTRIBUTE, buyingDaysElementBindableType);
SingularAttribute<? super Buyer, EnumSet> buyingDaysSingularAttribute = entityBuyer.getSingularAttribute("buyingDays", EnumSet.class);
assertNotNull(buyingDaysSingularAttribute);
assertFalse(buyingDaysSingularAttribute.isCollection());
// http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#DI_74:_20090909:_Implement_IdentifiableType.hasSingleIdAttribute.28.29
// Check for Id that exists
boolean expectedIAExceptionThrown = false;
boolean hasSingleIdAttribute = false;
try {
hasSingleIdAttribute = entityBuyer.hasSingleIdAttribute();
} catch (IllegalArgumentException iae) {
// iae.printStackTrace();
expectedIAExceptionThrown = true;
}
assertFalse(expectedIAExceptionThrown);
assertTrue(hasSingleIdAttribute);
// Verify that the BasicMap Buyer.creditCards is picked up properly
// * @param <X> The type the represented Map belongs to
// * @param <K> The type of the key of the represented Map
// * @param <V> The type of the value of the represented Map
// public class MapAttributeImpl<X, K, V> extends PluralAttributeImpl<X, java.util.Map<K, V>, V>
Attribute<? super Buyer, ?> buyerCreditCards = entityBuyer.getAttribute("creditCards");
assertNotNull(buyerCreditCards);
assertTrue(buyerCreditCards.isCollection());
assertTrue(buyerCreditCards instanceof MapAttributeImpl);
MapAttribute<? super Buyer, ?, ?> buyerCreditCardsMap = entityBuyer.getMap("creditCards");
// Verify owning type
assertNotNull(buyerCreditCardsMap);
assertEquals(entityBuyer, buyerCreditCardsMap.getDeclaringType());
// Verify Map Key
assertEquals(String.class, buyerCreditCardsMap.getKeyJavaType());
// Verify Map Value
assertEquals(Long.class, buyerCreditCardsMap.getElementType().getJavaType());
}
use of jakarta.persistence.metamodel.Attribute in project eclipselink by eclipse-ee4j.
the class FromImpl method join.
@Override
public <T, Y> Join<T, Y> join(String attributeName, JoinType jt) {
Attribute attribute = this.managedType.getAttribute(attributeName);
if (attribute.isCollection()) {
org.eclipse.persistence.expressions.Expression node;
if (jt.equals(JoinType.INNER)) {
node = this.currentNode.anyOf(attribute.getName());
} else if (jt.equals(JoinType.RIGHT)) {
throw new UnsupportedOperationException(ExceptionLocalization.buildMessage("RIGHT_JOIN_NOT_SUPPORTED"));
} else {
node = this.currentNode.anyOfAllowingNone(attribute.getName());
}
Join join;
if (((PluralAttribute) attribute).getElementType().getPersistenceType().equals(PersistenceType.BASIC)) {
if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
join = new BasicCollectionJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
join = new BasicListJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
join = new BasicSetJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else {
join = new BasicMapJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
}
} else {
if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
join = new CollectionJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
join = new ListJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
join = new SetJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
} else {
join = new MapJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), node, (Bindable) attribute, jt);
}
}
this.joins.add(join);
((FromImpl) join).isJoin = true;
return join;
} else {
return join(((SingularAttribute) attribute), jt);
}
}
use of jakarta.persistence.metamodel.Attribute in project eclipselink by eclipse-ee4j.
the class FromImpl method get.
@Override
public <Y> Path<Y> get(String attName) {
Attribute attribute = this.managedType.getAttribute(attName);
Join join;
if (attribute.isCollection()) {
if (!((PluralAttribute) attribute).getElementType().getPersistenceType().equals(PersistenceType.BASIC)) {
if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
join = new CollectionJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
join = new ListJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
join = new SetJoinImpl<X, Y>(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else {
join = new MapJoinImpl(this, metamodel.managedType(((PluralAttribute) attribute).getBindableJavaType()), this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
}
} else {
if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.COLLECTION)) {
join = new BasicCollectionJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.LIST)) {
join = new BasicListJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else if (((PluralAttribute) attribute).getCollectionType().equals(CollectionType.SET)) {
join = new BasicSetJoinImpl<X, Y>(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
} else {
join = new BasicMapJoinImpl(this, this.metamodel, ((PluralAttribute) attribute).getBindableJavaType(), this.currentNode.anyOf(attribute.getName()), (Bindable) attribute);
}
}
} else {
Class<Y> clazz = ((SingularAttribute) attribute).getBindableJavaType();
if (((SingularAttribute) attribute).getType().getPersistenceType().equals(PersistenceType.BASIC)) {
return new PathImpl<Y>(this, this.metamodel, clazz, this.currentNode.get(attribute.getName()), (Bindable) attribute);
} else {
join = new JoinImpl(this, this.metamodel.managedType(clazz), this.metamodel, clazz, this.currentNode.get(attribute.getName()), (Bindable) attribute);
}
}
this.joins.add(join);
return join;
}
use of jakarta.persistence.metamodel.Attribute in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE.
// Test that we are getting an Illegal argument exception when trying to access non-persistent fields from transient classes
public void testAttribute_getAttribute_of_TransientNonEntityNonMappedSuperclass_SuperclassOfEntity_throws_IAE() {
EntityManager em = null;
boolean exceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
// Verify that the non-Entity/non-MappedSuperclass is a BasicType
Type positionNonEntity = ((MetamodelImpl) metamodel).getType(Position.class);
assertNotNull(positionNonEntity);
assertEquals(Type.PersistenceType.BASIC, positionNonEntity.getPersistenceType());
// Get direct inheriting subclass
EntityTypeImpl<GalacticPosition> entityLocation_ = (EntityTypeImpl) metamodel.entity(GalacticPosition.class);
assertNotNull(entityLocation_);
// We will be testing that the non-persistent fields
Attribute anAttributeThatShouldNotHaveBeenInherited = entityLocation_.getAttribute("nonPersistentObject");
// we should never get to the following line - go directly to catch block
assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
} catch (IllegalArgumentException iae) {
// iae.printStackTrace();
exceptionThrown = true;
assertTrue("IllegalArgumentException expected on transient type attribute should not be in subclass for managedType.getAttribute()", exceptionThrown);
} finally {
cleanup(em);
}
}
use of jakarta.persistence.metamodel.Attribute in project eclipselink by eclipse-ee4j.
the class MetamodelMetamodelTest method testManagedType_getDeclaredSingularAttribute_on_MappedSuperclass_Method.
public void testManagedType_getDeclaredSingularAttribute_on_MappedSuperclass_Method() {
EntityManager em = null;
boolean expectedIAExceptionThrown = false;
try {
em = privateTestSetup();
assertNotNull(em);
Metamodel metamodel = em.getMetamodel();
assertNotNull("The metamodel should never be null after an em.getMetamodel() call here.", metamodel);
MappedSuperclassTypeImpl<Person> msPerson_ = (MappedSuperclassTypeImpl) metamodel.managedType(Person.class);
assertNotNull(msPerson_);
// Test case
Attribute anAttribute = msPerson_.getSingularAttribute("name");
assertNotNull(anAttribute);
assertEquals(PersistentAttributeType.BASIC, anAttribute.getPersistentAttributeType());
assertFalse(((AttributeImpl) anAttribute).isPlural());
assertTrue(anAttribute instanceof SingularAttribute);
assertEquals(String.class, anAttribute.getJavaType());
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
expectedIAExceptionThrown = true;
} finally {
cleanup(em);
assertFalse("An IAE exception should not occur here.", expectedIAExceptionThrown);
}
}
Aggregations