Open manymotes opened 2 years ago
I noticed as well
I think it boils down to serializing to interfaces. Interfaces dont know what constructor to use. I wished we could tell the the interfaces what object/constructor to use.
We chose to stop using the interfaces. But to keep the schema, and java objects in sync, I just created an annotation. It uses reflection to check that our pojo's have all the fields required to support the autogenerated objects fields. something like this:
@Retention(RetentionPolicy.RUNTIME)
@Target(TYPE)
public @interface ZonosFieldParity {
Class<?> parent();
}
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.hibernate.AnnotationException;
import org.reflections.Reflections;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class ZonosFieldParityAnnotationComponent implements InitializingBean {
//fields we have automated in our db stuff
private Set<String> ignoreNames = Stream.of("createdAt", "createdBy", "updatedAt", "updatedBy", "mode").collect(Collectors.toSet());
@Override
public void afterPropertiesSet() throws Exception {
var clazzes = new Reflections("com.zonos").getTypesAnnotatedWith(ZonosFieldParity.class);
for (Class<?> clazz : clazzes) {
Annotation[] annotations = clazz.getAnnotations();
Set<String> childClassMethods = new HashSet<>();
var classType = Class.forName(clazz.getName());
for (Field field : classType.getDeclaredFields()) {
childClassMethods.add(field.getName());
}
ZonosFieldParity inheritance = (ZonosFieldParity) annotations[0];
Class c = inheritance.parent();
Arrays.stream(c.getDeclaredFields()).forEach(e -> {
if (!ignoreNames.contains(e.getName())) {
if (!childClassMethods.contains(e.getName())) {
throw new AnnotationException("Zonos Field Parity error: " + clazz.getName() + " does not have " + e.getName() + " field");
}
}
});
}
}
}
generateInterfaces = true
with a schema type like:
and a hibernate entity like this: