From 409a0142f554c8f886014656e57f0644e4a8d9c6 Mon Sep 17 00:00:00 2001 From: Andreas Ernst Date: Fri, 27 Sep 2024 09:54:33 +0200 Subject: [PATCH] hibernate sucks --- .../sirius/dorm/model/AttributeDescriptor.kt | 5 +++ .../org/sirius/dorm/model/ObjectDescriptor.kt | 5 +++ .../sirius/dorm/model/RelationDescriptor.kt | 5 +++ .../org/sirius/dorm/object/Attribute.kt | 6 +++ .../sirius/dorm/object/MultiValuedRelation.kt | 8 ++-- .../dorm/object/SingleValuedRelation.kt | 32 ++++++--------- .../sirius/dorm/persistence/ObjectWriter.kt | 2 +- .../dorm/persistence/entity/EntityEntity.kt | 9 ++++- .../dorm/persistence/entity/PropertyEntity.kt | 40 ++++++++++++++----- 9 files changed, 76 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt b/src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt index 7048ff2..f81b3fe 100644 --- a/src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt +++ b/src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt @@ -40,4 +40,9 @@ class AttributeDescriptor(name: String, val type: Type<*>, val isPrimaryK override fun isAttribute() : Boolean { return true } + + // override Object + override fun toString(): String { + return "${this.name}" + } } \ No newline at end of file diff --git a/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt b/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt index 958f774..4f4b18e 100644 --- a/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt +++ b/src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt @@ -41,4 +41,9 @@ class ObjectDescriptor(val name: String, val properties: Array { return properties.find { prop -> prop.name == property}!! } + + // override Object + override fun toString(): String { + return "${this.name}" + } } \ No newline at end of file diff --git a/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt b/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt index 21bbf73..11fa502 100644 --- a/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt +++ b/src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt @@ -57,4 +57,9 @@ open class RelationDescriptor(name: String, val target: String, val multi fun isOwner() : Boolean { return owner } + + // override Object + override fun toString(): String { + return "${this.name}" + } } \ No newline at end of file diff --git a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt index a6b594d..9dc2f26 100644 --- a/src/main/kotlin/org/sirius/dorm/object/Attribute.kt +++ b/src/main/kotlin/org/sirius/dorm/object/Attribute.kt @@ -36,4 +36,10 @@ class Attribute(property: PropertyEntity?, var value: Any) : Property(property) override fun isDirty(snapshot: Any) : Boolean { return value != snapshot } + + // override Object + + override fun toString(): String { + return "${this.value}" + } } \ No newline at end of file diff --git a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt index d2faa40..772d6b8 100644 --- a/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt @@ -73,8 +73,6 @@ 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() @@ -93,7 +91,6 @@ class MultiValuedRelation(relation: RelationDescriptor<*>, status: Status, val o 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 @@ -103,8 +100,11 @@ 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 ( !this.relation.owner) { + deleted.targets.remove(this.property) + } } // if } } diff --git a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt index 6bd6d92..f142be7 100644 --- a/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt +++ b/src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt @@ -64,37 +64,31 @@ class SingleValuedRelation(relation: RelationDescriptor<*>, status: Status, val // adjust relation - val relationenEntities = relations() - - val n = if (relation.isOwner() ) "targets" else "sources" + val rel = relations() if ( target === null) { - if ( relationenEntities.isNotEmpty()) { - if ( relationenEntities.size > 1) - throw Error("iuch") - - val property = relationenEntities.first() + if ( rel.isNotEmpty()) { + val property = rel.first() - relationenEntities.clear() + rel.remove(property) - println("remove ${property.entity.id}.${property.attribute} from entity[${obj.id}].${relation.name}.${n}") - } + if ( !relation.isOwner()) { + property.targets.remove(this.property) + } + } } else { - if ( relationenEntities.size == 1) { - val property = relationenEntities.first() + if ( rel.size == 1) { + val property = rel.first() if (property.entity !== target!!.entity ) { - println("add ${property.entity.id}.${property.attribute} to entity[${obj.id}].${relation.name}.${n}") - relationenEntities.add(property) + rel.add(property) } } - if (relationenEntities.size == 0) { + if (rel.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) + rel.add(property) } - else throw Error("iuch") } } } diff --git a/src/main/kotlin/org/sirius/dorm/persistence/ObjectWriter.kt b/src/main/kotlin/org/sirius/dorm/persistence/ObjectWriter.kt index 4c4da4f..5353153 100644 --- a/src/main/kotlin/org/sirius/dorm/persistence/ObjectWriter.kt +++ b/src/main/kotlin/org/sirius/dorm/persistence/ObjectWriter.kt @@ -33,7 +33,7 @@ class ObjectWriter(private val descriptor: ObjectDescriptor) { var i = 1 for ( writer in writer) { val propertyDescriptor = descriptor.properties[i] - val property = PropertyEntity(obj.entity!!, propertyDescriptor.name, descriptor.name, "", 0, 0.0) + val property = PropertyEntity(0, obj.entity!!, propertyDescriptor.name, descriptor.name, "", 0, 0.0) obj.values[i].property = property diff --git a/src/main/kotlin/org/sirius/dorm/persistence/entity/EntityEntity.kt b/src/main/kotlin/org/sirius/dorm/persistence/entity/EntityEntity.kt index 00af0a5..4a6f351 100644 --- a/src/main/kotlin/org/sirius/dorm/persistence/entity/EntityEntity.kt +++ b/src/main/kotlin/org/sirius/dorm/persistence/entity/EntityEntity.kt @@ -27,9 +27,14 @@ data class EntityEntity( ) { // override Object + override fun toString(): String { + return "${type}[${id}]" + } + override fun equals(other: Any?): Boolean { - if ( other is EntityEntity) - return this === other + if ( other is EntityEntity) { + return this.id == other.id + } else return false } 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 9596c9d..91b13d2 100644 --- a/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt +++ b/src/main/kotlin/org/sirius/dorm/persistence/entity/PropertyEntity.kt @@ -20,15 +20,20 @@ data class PropertyId(private val entity: Long = 0L, private val attribute: Stri Index(columnList = "STRING_VALUE") ] ) -@IdClass(PropertyId::class) +//@IdClass(PropertyId::class) data class PropertyEntity( + @Column(name = "ID") + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id : Long, + @ManyToOne(fetch = FetchType.LAZY) - @MapsId + //@MapsId @JoinColumn(name = "ENTITY") var entity : EntityEntity, @Column(name = "ATTRIBUTE") - @Id + //@Id var attribute : String, @Column(name = "TYPE") @@ -44,32 +49,47 @@ data class PropertyEntity( var doubleValue : Double, @ManyToMany(fetch = FetchType.LAZY) - @JoinTable( + /*@JoinTable( name = "RELATIONS", joinColumns = [JoinColumn(name = "FROM_ATTR"), JoinColumn(name = "FROM_ENTITY")], inverseJoinColumns = [JoinColumn(name = "TO_ATTR"), JoinColumn(name = "TO_ENTITY")] + )*/ + @JoinTable( + name = "RELATIONS", + joinColumns = [JoinColumn(name = "FROM_")], + inverseJoinColumns = [JoinColumn(name = "TO_")] ) val targets : MutableSet = HashSet(), - //@ManyToMany(mappedBy="targets", fetch = FetchType.LAZY) - @ManyToMany(fetch = FetchType.LAZY) - @JoinTable( + //@ManyToMany(fetch = FetchType.LAZY) + @ManyToMany(mappedBy="targets") + /*@JoinTable( + name = "RELATIONS", + joinColumns = [JoinColumn(name = "TO_")], + inverseJoinColumns = [JoinColumn(name = "FROM_")] + )*/ + //@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 + override fun toString(): String { + return "${type}.${attribute}[${id}]" + } + override fun equals(other: Any?): Boolean { if ( other is PropertyEntity) - return this === other + return this.id == other.id else return false } override fun hashCode(): Int { - return type.hashCode() + attribute.hashCode() + return id.hashCode() + type.hashCode() + attribute.hashCode() } } \ No newline at end of file