use of io.micronaut.data.model.Association in project micronaut-data by micronaut-projects.
the class MappedEntityVisitor method computeMappingDefaults.
private void computeMappingDefaults(NamingStrategy namingStrategy, PersistentProperty property, Map<String, DataType> dataTypes, Map<String, String> dataConverters, VisitorContext context) {
AnnotationMetadata annotationMetadata = property.getAnnotationMetadata();
SourcePersistentProperty spp = (SourcePersistentProperty) property;
PropertyElement propertyElement = spp.getPropertyElement();
boolean isRelation = propertyElement.hasStereotype(Relation.class);
DataType dataType = annotationMetadata.getValue(TypeDef.class, "type", DataType.class).orElse(null);
String converter = annotationMetadata.stringValue(MappedProperty.class, "converter").orElseGet(() -> annotationMetadata.stringValue(TypeDef.class, "converter").orElse(null));
if (Objects.equals(converter, Object.class.getName())) {
converter = null;
}
if (converter == null) {
ClassElement type = propertyElement.getGenericType();
converter = TypeUtils.resolveDataConverter(type, dataConverters);
}
if (converter != null) {
if (isRelation) {
context.fail("Relation cannot have converter specified", propertyElement);
return;
}
ClassElement persistedClassFromConverter = getPersistedClassFromConverter(converter, context);
if (persistedClassFromConverter != null) {
propertyElement.annotate(MappedProperty.class, builder -> {
builder.member("converterPersistedType", new AnnotationClassValue<>(persistedClassFromConverter.getCanonicalName()));
});
}
if (dataType == null) {
dataType = getDataTypeFromConverter(propertyElement.getGenericType(), converter, dataTypes, context);
if (dataType == null) {
context.fail("Cannot recognize proper data type. Please use @TypeDef to specify one", propertyElement);
return;
}
}
} else {
if (dataType == null && spp.getType().isEnum()) {
if (spp.getOwner().getAnnotationMetadata().hasAnnotation("javax.persistence.Entity") || spp.getOwner().getAnnotationMetadata().hasAnnotation("jakarta.persistence.Entity")) {
// JPA enums have default ORDINAL mapping for enums
dataType = DataType.INTEGER;
}
}
if (dataType == null) {
ClassElement type = propertyElement.getGenericType();
dataType = TypeUtils.resolveDataType(type, dataTypes);
}
}
if (dataType == DataType.ENTITY && !isRelation) {
propertyElement = (PropertyElement) propertyElement.annotate(Relation.class, builder -> builder.value(Relation.Kind.MANY_TO_ONE));
} else if (isRelation) {
Relation.Kind kind = propertyElement.enumValue(Relation.class, Relation.Kind.class).orElse(Relation.Kind.MANY_TO_ONE);
if (kind == Relation.Kind.EMBEDDED || kind == Relation.Kind.MANY_TO_ONE) {
if (propertyElement.stringValue(Relation.class, "mappedBy").isPresent()) {
context.fail("Relation " + kind + " doesn't support 'mappedBy'.", propertyElement);
}
}
if (kind == Relation.Kind.EMBEDDED) {
// handled embedded
SourcePersistentEntity embeddedEntity = entityResolver.apply(propertyElement.getType());
List<SourcePersistentProperty> persistentProperties = embeddedEntity.getPersistentProperties();
List<AnnotationValue<Property>> embeddedProperties = new ArrayList<>(persistentProperties.size());
for (SourcePersistentProperty embeddedProperty : persistentProperties) {
if (!(embeddedProperty instanceof Association)) {
String mappedName = embeddedProperty.stringValue(MappedProperty.class).orElseGet(() -> namingStrategy.mappedName(property.getName() + embeddedProperty.getCapitilizedName()));
AnnotationValue<Property> av = AnnotationValue.builder(Property.class).value(mappedName).member("name", embeddedProperty.getName()).build();
embeddedProperties.add(av);
}
// else {
// // TODO: handle nested embedded
// }
}
propertyElement.annotate(MappedProperty.class, builder -> builder.member(MappedProperty.EMBEDDED_PROPERTIES, embeddedProperties.toArray(new AnnotationValue[0])));
}
}
Optional<String> mapping = annotationMetadata.stringValue(MappedProperty.class);
if (mappedEntity && !mapping.isPresent()) {
propertyElement.annotate(MappedProperty.class, builder -> builder.value(namingStrategy.mappedName(spp)));
}
if (dataType != DataType.OBJECT) {
DataType finalDataType = dataType;
propertyElement.annotate(MappedProperty.class, builder -> builder.member("type", finalDataType));
}
if (converter != null) {
String finalConverter = converter;
propertyElement.annotate(MappedProperty.class, builder -> builder.member("converter", new AnnotationClassValue<>(finalConverter)));
}
}
Aggregations