/** * Get {@link javax.persistence.AccessType } from {@link javax.persistence.Access @Access} on the * attribute of the given class */ static XMLAccessType getAccessFromAttributeAnnotation( DotName className, String attributeName, IndexBuilder indexBuilder) { Map<DotName, List<AnnotationInstance>> indexedAnnotations = indexBuilder.getIndexedAnnotations(className); if (indexedAnnotations != null && indexedAnnotations.containsKey(ACCESS)) { List<AnnotationInstance> annotationInstances = indexedAnnotations.get(ACCESS); if (MockHelper.isNotEmpty(annotationInstances)) { for (AnnotationInstance annotationInstance : annotationInstances) { AnnotationTarget indexedPropertyTarget = annotationInstance.target(); if (indexedPropertyTarget == null) { continue; } if (JandexHelper.getPropertyName(indexedPropertyTarget).equals(attributeName)) { XMLAccessType accessType = JandexHelper.getValueAsEnum(annotationInstance, "value", XMLAccessType.class); /** here we ignore @Access(FIELD) on property (getter) and @Access(PROPERTY) on field */ XMLAccessType targetAccessType = annotationTargetToAccessType(indexedPropertyTarget); if (accessType.equals(targetAccessType)) { return targetAccessType; } else { LOG.warn( String.format( "%s.%s has @Access on %s, but it tries to assign the access type to %s, this is not allowed by JPA spec, and will be ignored.", className, attributeName, targetAccessType, accessType)); } } } } } return null; }
static XMLAccessType getAccessFromDefault(IndexBuilder indexBuilder) { AnnotationInstance annotationInstance = JandexHelper.getSingleAnnotation( indexBuilder.getAnnotations(), PseudoJpaDotNames.DEFAULT_ACCESS); if (annotationInstance == null) { return null; } else { return JandexHelper.getValueAsEnum(annotationInstance, "value", XMLAccessType.class); } }
private static void bind(MetadataImpl metadata, AnnotationInstance fetchProfile) { String name = JandexHelper.getValueAsString(fetchProfile, "name"); Set<Fetch> fetches = new HashSet<Fetch>(); for (AnnotationInstance override : JandexHelper.getValueAsArray(fetchProfile, "fetchOverrides")) { FetchMode fetchMode = JandexHelper.getValueAsEnum(override, "mode", FetchMode.class); if (!fetchMode.equals(org.hibernate.annotations.FetchMode.JOIN)) { throw new MappingException("Only FetchMode.JOIN is currently supported"); } fetches.add( new Fetch( JandexHelper.getValueAsString(override, "entity"), JandexHelper.getValueAsString(override, "association"), fetchMode.toString().toLowerCase())); } metadata.addFetchProfile(new FetchProfile(name, fetches)); }
// TODO verify that association exists. See former VerifyFetchProfileReferenceSecondPass public static void bind(MetadataImpl metadata, Index jandex) { for (AnnotationInstance fetchProfile : jandex.getAnnotations(HibernateDotNames.FETCH_PROFILE)) { bind(metadata, fetchProfile); } for (AnnotationInstance fetchProfiles : jandex.getAnnotations(HibernateDotNames.FETCH_PROFILES)) { for (AnnotationInstance fetchProfile : JandexHelper.getValueAsArray(fetchProfiles, "value")) { bind(metadata, fetchProfile); } } }
/** * Binds all {@link org.hibernate.annotations.TypeDef} and {@link TypeDefs} annotations to the * supplied metadata. * * @param metadata the global metadata * @param jandex the jandex jandex */ public static void bind(MetadataImplementor metadata, Index jandex) { for (AnnotationInstance typeDef : jandex.getAnnotations(HibernateDotNames.TYPE_DEF)) { bind(metadata, typeDef); } for (AnnotationInstance typeDefs : jandex.getAnnotations(HibernateDotNames.TYPE_DEFS)) { AnnotationInstance[] typeDefAnnotations = JandexHelper.getValue(typeDefs, "value", AnnotationInstance[].class); for (AnnotationInstance typeDef : typeDefAnnotations) { bind(metadata, typeDef); } } }
private static void bind(MetadataImplementor metadata, AnnotationInstance typeDefAnnotation) { String name = JandexHelper.getValue(typeDefAnnotation, "name", String.class); String defaultForType = JandexHelper.getValue(typeDefAnnotation, "defaultForType", String.class); String typeClass = JandexHelper.getValue(typeDefAnnotation, "typeClass", String.class); boolean noName = StringHelper.isEmpty(name); boolean noDefaultForType = defaultForType == null || defaultForType.equals(void.class.getName()); if (noName && noDefaultForType) { throw new AnnotationException( "Either name or defaultForType (or both) attribute should be set in TypeDef having typeClass " + typeClass); } Map<String, String> parameterMaps = new HashMap<String, String>(); AnnotationInstance[] parameterAnnotations = JandexHelper.getValue(typeDefAnnotation, "parameters", AnnotationInstance[].class); for (AnnotationInstance parameterAnnotation : parameterAnnotations) { parameterMaps.put( JandexHelper.getValue(parameterAnnotation, "name", String.class), JandexHelper.getValue(parameterAnnotation, "value", String.class)); } if (!noName) { bind(name, typeClass, parameterMaps, metadata); } if (!noDefaultForType) { bind(defaultForType, typeClass, parameterMaps, metadata); } }
private static XMLAccessType getAccess(Map<DotName, List<AnnotationInstance>> annotations) { if (annotations == null || annotations.isEmpty() || !isEntityObject(annotations)) { return null; } List<AnnotationInstance> accessAnnotationInstances = annotations.get(JPADotNames.ACCESS); if (MockHelper.isNotEmpty(accessAnnotationInstances)) { for (AnnotationInstance annotationInstance : accessAnnotationInstances) { if (annotationInstance.target() != null && annotationInstance.target() instanceof ClassInfo) { return JandexHelper.getValueAsEnum(annotationInstance, "value", XMLAccessType.class); } } } return null; }
@Test public void testTablePerClassDefaultTableName() { @Entity @Inheritance(strategy = javax.persistence.InheritanceType.TABLE_PER_CLASS) class A { @Id @GeneratedValue private int id; } @Entity class B extends A {} Index index = JandexHelper.indexForClass(service, A.class, B.class); AnnotationBindingContext context = new AnnotationBindingContext(index, serviceRegistry); Set<ConfiguredClassHierarchy<EntityClass>> hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies(context); assertEquals("There should be only one hierarchy", 1, hierarchies.size()); Iterator<EntityClass> iter = hierarchies.iterator().next().iterator(); EntityClass entityClass = iter.next(); ClassInfo info = entityClass.getClassInfo(); assertEquals("wrong class", DotName.createSimple(A.class.getName()), info.name()); assertTrue(entityClass.hasOwnTable()); Assert.assertEquals( "wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()); Assert.assertEquals("wrong table name", "A", entityClass.getPrimaryTableName()); assertTrue(iter.hasNext()); entityClass = iter.next(); info = entityClass.getClassInfo(); assertEquals("wrong class", DotName.createSimple(B.class.getName()), info.name()); assertTrue(entityClass.hasOwnTable()); Assert.assertEquals( "wrong inheritance type", InheritanceType.TABLE_PER_CLASS, entityClass.getInheritanceType()); Assert.assertEquals("wrong table name", "B", entityClass.getPrimaryTableName()); assertFalse(iter.hasNext()); }