use of io.requery.meta.Cardinality in project requery by requery.
the class AttributeMember method processAssociativeAnnotations.
private void processAssociativeAnnotations(ProcessingEnvironment processingEnvironment, ElementValidator validator) {
Optional<Annotation> oneToOne = annotationOf(OneToOne.class);
Optional<Annotation> oneToMany = annotationOf(OneToMany.class);
Optional<Annotation> manyToOne = annotationOf(ManyToOne.class);
Optional<Annotation> manyToMany = annotationOf(ManyToMany.class);
oneToOne = oneToOne.isPresent() ? oneToOne : annotationOf(javax.persistence.OneToOne.class);
oneToMany = oneToMany.isPresent() ? oneToMany : annotationOf(javax.persistence.OneToMany.class);
manyToOne = manyToOne.isPresent() ? manyToOne : annotationOf(javax.persistence.ManyToOne.class);
manyToMany = manyToMany.isPresent() ? manyToMany : annotationOf(javax.persistence.ManyToMany.class);
if (Stream.of(oneToOne, oneToMany, manyToOne, manyToMany).filter(Optional::isPresent).count() > 1) {
validator.error("Cannot have more than one associative annotation per field");
}
if (oneToOne.isPresent()) {
cardinality = Cardinality.ONE_TO_ONE;
ReflectiveAssociation reflect = new ReflectiveAssociation(oneToOne.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
if (!isForeignKey()) {
isReadOnly = true;
if (!isKey()) {
isUnique = true;
}
}
}
if (oneToMany.isPresent()) {
isIterable = true;
cardinality = Cardinality.ONE_TO_MANY;
isReadOnly = true;
ReflectiveAssociation reflect = new ReflectiveAssociation(oneToMany.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
checkIterable(validator);
processOrderBy();
}
if (manyToOne.isPresent()) {
cardinality = Cardinality.MANY_TO_ONE;
isForeignKey = true;
ReflectiveAssociation reflect = new ReflectiveAssociation(manyToOne.get());
cascadeActions = reflect.cascade();
if (deleteAction == null) {
deleteAction = ReferentialAction.CASCADE;
}
if (updateAction == null) {
updateAction = ReferentialAction.CASCADE;
}
}
if (manyToMany.isPresent()) {
isIterable = true;
cardinality = Cardinality.MANY_TO_MANY;
ReflectiveAssociation reflect = new ReflectiveAssociation(manyToMany.get());
mappedBy = reflect.mappedBy();
cascadeActions = reflect.cascade();
Optional<JunctionTable> junctionTable = annotationOf(JunctionTable.class);
Optional<javax.persistence.JoinTable> joinTable = annotationOf(javax.persistence.JoinTable.class);
if (junctionTable.isPresent()) {
Elements elements = processingEnvironment.getElementUtils();
associativeDescriptor = new JunctionTableAssociation(elements, this, junctionTable.get());
} else if (joinTable.isPresent()) {
associativeDescriptor = new JoinTableAssociation(joinTable.get());
}
isReadOnly = true;
checkIterable(validator);
processOrderBy();
}
if (isForeignKey()) {
if (deleteAction == ReferentialAction.SET_NULL && !isNullable()) {
validator.error("Cannot SET_NULL on optional attribute", ForeignKey.class);
}
// user mirror so generated type can be referenced
Optional<? extends AnnotationMirror> mirror = Mirrors.findAnnotationMirror(element(), ForeignKey.class);
if (mirror.isPresent()) {
referencedType = mirror.flatMap(m -> Mirrors.findAnnotationValue(m, "references")).map(value -> value.getValue().toString()).orElse(null);
} else if (!typeMirror().getKind().isPrimitive()) {
referencedType = typeMirror().toString();
}
}
}
use of io.requery.meta.Cardinality in project requery by requery.
the class EntityGraphValidator method validateRelationship.
private void validateRelationship(ElementValidator validator, AttributeDescriptor source, AttributeDescriptor mapped) {
Cardinality sourceCardinality = source.cardinality();
Cardinality mappedCardinality = mapped.cardinality();
Cardinality expectedCardinality;
switch(sourceCardinality) {
case ONE_TO_ONE:
expectedCardinality = Cardinality.ONE_TO_ONE;
break;
case ONE_TO_MANY:
expectedCardinality = Cardinality.MANY_TO_ONE;
break;
case MANY_TO_ONE:
expectedCardinality = Cardinality.ONE_TO_MANY;
break;
case MANY_TO_MANY:
expectedCardinality = Cardinality.MANY_TO_MANY;
break;
default:
throw new IllegalStateException();
}
if (mappedCardinality != expectedCardinality && mapped.cardinality() != null) {
String message = mappingErrorMessage(source, mapped, expectedCardinality);
validator.error(message);
} else if (sourceCardinality == Cardinality.MANY_TO_MANY) {
Optional<AssociativeEntityDescriptor> sourceAssociation = source.associativeEntity();
Optional<AssociativeEntityDescriptor> mappedAssociation = mapped.associativeEntity();
if (!sourceAssociation.isPresent() && !mappedAssociation.isPresent()) {
validator.error("One side of the ManyToMany relationship must specify the " + "@JunctionTable/@JoinTable annotation");
}
if (sourceAssociation.isPresent() && mappedAssociation.isPresent()) {
validator.error("@JunctionTable should be specified on only one side of a " + "ManyToMany relationship");
}
} else if (sourceCardinality == Cardinality.ONE_TO_ONE) {
if (!source.isForeignKey() && !mapped.isForeignKey()) {
validator.error("One side of the OneToOne relationship must specify the " + "@ForeignKey/@JoinColumn annotation");
}
if (source.isForeignKey() && mapped.isForeignKey() && source != mapped) {
validator.error("Only one side of the OneToOne relationship can specify the " + "@ForeignKey/@JoinColumn annotation");
}
}
}
Aggregations