use of jakarta.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class SemanticQueryBuilder method visitEntityNaturalIdReference.
@Override
public SqmPath<?> visitEntityNaturalIdReference(HqlParser.EntityNaturalIdReferenceContext ctx) {
final SqmPath<Object> sqmPath = consumeDomainPath((HqlParser.PathContext) ctx.getChild(2));
final DomainType<?> sqmPathType = sqmPath.getReferencedPathSource().getSqmPathType();
if (sqmPathType instanceof IdentifiableDomainType<?>) {
@SuppressWarnings("unchecked") final IdentifiableDomainType<Object> identifiableType = (IdentifiableDomainType<? super Object>) sqmPathType;
final List<? extends PersistentAttribute<Object, ?>> attributes = identifiableType.findNaturalIdAttributes();
if (attributes == null) {
throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which does not define a natural id", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
} else if (attributes.size() > 1) {
throw new SemanticException(String.format("Path '%s' resolved to entity type '%s' which defines multiple natural ids", sqmPath.getNavigablePath().getFullPath(), identifiableType.getTypeName()));
}
@SuppressWarnings("unchecked") SingularAttribute<Object, ?> naturalIdAttribute = (SingularAttribute<Object, ?>) attributes.get(0);
return sqmPath.get(naturalIdAttribute);
}
throw new SemanticException("Path does not resolve to an entity type '" + sqmPath.getNavigablePath().getFullPath() + "'");
}
use of jakarta.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class AbstractManagedType method getSingularAttribute.
@Override
@SuppressWarnings("unchecked")
public <Y> SingularPersistentAttribute<? super J, Y> getSingularAttribute(String name, Class<Y> type) {
SingularAttribute attribute = findSingularAttribute(name);
checkTypeForSingleAttribute(attribute, name, type);
return (SingularPersistentAttribute) attribute;
}
use of jakarta.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class AbstractManagedType method getDeclaredSingularAttribute.
@Override
@SuppressWarnings("unchecked")
public <Y> SingularPersistentAttribute<J, Y> getDeclaredSingularAttribute(String name, Class<Y> javaType) {
final SingularAttribute attr = findDeclaredSingularAttribute(name);
checkTypeForSingleAttribute(attr, name, javaType);
return (SingularPersistentAttribute) attr;
}
use of jakarta.persistence.metamodel.SingularAttribute in project hibernate-orm by hibernate.
the class EmbeddedTypeTest method testSingularAttributeAccessByName.
@Test
@TestForIssue(jiraKey = "HHH-4702")
public void testSingularAttributeAccessByName(EntityManagerFactoryScope scope) {
scope.inTransaction(entityManager -> {
SingularAttribute soldDate_ = entityManager.getMetamodel().embeddable(ShelfLife.class).getSingularAttribute("soldDate");
assertEquals(Date.class, soldDate_.getBindableJavaType());
assertEquals(Date.class, soldDate_.getType().getJavaType());
assertEquals(Date.class, soldDate_.getJavaType());
});
}
use of jakarta.persistence.metamodel.SingularAttribute 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());
}
Aggregations