diff --git a/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptorBuilder.kt b/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptorBuilder.kt index 15091fb..8625a4e 100644 --- a/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptorBuilder.kt +++ b/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptorBuilder.kt @@ -122,12 +122,12 @@ class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) { private val properties = ArrayList>() init { - property(id) + define(id) } // fluent - fun property(propertyBuilder: PropertyBuilder) : ObjectDescriptorBuilder { + fun define(propertyBuilder: PropertyBuilder) : ObjectDescriptorBuilder { properties.add(propertyBuilder.build()) return this diff --git a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt index e5177de..a6b594d 100644 --- a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt +++ b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt @@ -1,6 +1,7 @@ package org.sirius.dorm.`object` import org.sirius.dorm.ObjectManager +import org.sirius.dorm.ObjectManagerError import org.sirius.dorm.model.PropertyDescriptor import org.sirius.dorm.persistence.entity.PropertyEntity diff --git a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt index ed19a3b..d2faa40 100644 --- a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt @@ -73,6 +73,8 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o override fun flush() { if (isLoaded()) { + val n = if (relation.isOwner() ) "targets" else "sources" + // synchronize the objects set with the property.relations val targetMap = HashMap() @@ -88,8 +90,12 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o for (target in objects!!) { val key = target.id - if (!targetMap.containsKey(key)) - relations.add(target.values[relation.index].property!!) + if (!targetMap.containsKey(key)) { + val property = target.values[relation.inverseRelation!!.index].property!! + + println("add ${property.entity.id}.${property.attribute} to entity[${obj.id}].${relation.name}.${n}") + relations.add(property) + } else targetMap.remove(key) } // for @@ -97,6 +103,7 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o // deleted for (deleted in targetMap.values) { + println("delete ${deleted.entity.id}.${property!!.attribute} from entity[${obj.id}].${relation.name}.${n}") relations.remove(deleted) } // if } diff --git a/src/main/kotlin/org/sirius/dorm/object/Property.kt b/src/main/kotlin/org/sirius/dorm/object/Property.kt index 0f4828b..efeb2f6 100644 --- a/src/main/kotlin/org/sirius/dorm/object/Property.kt +++ b/src/main/kotlin/org/sirius/dorm/object/Property.kt @@ -29,5 +29,7 @@ abstract class Property(var property: PropertyEntity?) { return false } + open fun validate() {} + open fun flush() {} } \ No newline at end of file diff --git a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt index 4675113..6bd6d92 100644 --- a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt @@ -51,15 +51,50 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val // override + override fun validate() { + if ( target === null && !relation.multiplicity.optional) + throw ObjectManagerError("relation ${obj.type.name}.${relation.name} is required") + } + override fun flush() { if ( isLoaded()) { - relations().clear() - if ( target !== null) { - relations().add(target!!.values[relation.inverseRelation!!.index].property!!) + // validate + + validate() + + // adjust relation + + val relationenEntities = relations() + + val n = if (relation.isOwner() ) "targets" else "sources" + + if ( target === null) { + if ( relationenEntities.isNotEmpty()) { + if ( relationenEntities.size > 1) + throw Error("iuch") + + val property = relationenEntities.first() + + relationenEntities.clear() + + println("remove ${property.entity.id}.${property.attribute} from entity[${obj.id}].${relation.name}.${n}") + } } else { - if ( !relation.multiplicity.optional) - throw ObjectManagerError("relation ${obj.type.name}.${relation.name} is required") + if ( relationenEntities.size == 1) { + val property = relationenEntities.first() + if (property.entity !== target!!.entity ) { + println("add ${property.entity.id}.${property.attribute} to entity[${obj.id}].${relation.name}.${n}") + relationenEntities.add(property) + } + } + + if (relationenEntities.size == 0) { + val property = target!!.values[relation.inverseRelation!!.index].property!! + println("add ${property.entity.id}.${property.attribute} to entity[${obj.id}].${relation.name}.${n}") + relationenEntities.add(property) + } + else throw Error("iuch") } } } diff --git a/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt b/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt index 76c088d..9596c9d 100644 --- a/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt +++ b/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt @@ -43,7 +43,7 @@ data class PropertyEntity( @Column(name = "DOUBLE_VALUE") var doubleValue : Double, - @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.REMOVE]) + @ManyToMany(fetch = FetchType.LAZY) @JoinTable( name = "RELATIONS", joinColumns = [JoinColumn(name = "FROM_ATTR"), JoinColumn(name = "FROM_ENTITY")], @@ -51,7 +51,13 @@ data class PropertyEntity( ) val targets : MutableSet = HashSet(), - @ManyToMany(mappedBy="targets", fetch = FetchType.LAZY) + //@ManyToMany(mappedBy="targets", fetch = FetchType.LAZY) + @ManyToMany(fetch = FetchType.LAZY) + @JoinTable( + name = "RELATIONS", + joinColumns = [JoinColumn(name = "TO_ATTR"), JoinColumn(name = "TO_ENTITY")], + inverseJoinColumns = [JoinColumn(name = "FROM_ATTR"), JoinColumn(name = "FROM_ENTITY")] + ) val sources : MutableSet = HashSet(), ) { // override Object