Skip to content

Commit

Permalink
hibernate sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 26, 2024
1 parent a0b3564 commit 7f03d51
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ class ObjectDescriptorBuilder(val manager: ObjectManager, val name: String) {
private val properties = ArrayList<PropertyDescriptor<Any>>()

init {
property(id)
define(id)
}

// fluent

fun property(propertyBuilder: PropertyBuilder) : ObjectDescriptorBuilder {
fun define(propertyBuilder: PropertyBuilder) : ObjectDescriptorBuilder {
properties.add(propertyBuilder.build())

return this
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/org/sirius/dorm/object/Attribute.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, PropertyEntity>()
Expand All @@ -88,15 +90,20 @@ 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

// deleted

for (deleted in targetMap.values) {
println("delete ${deleted.entity.id}.${property!!.attribute} from entity[${obj.id}].${relation.name}.${n}")
relations.remove(deleted)
} // if
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/sirius/dorm/object/Property.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ abstract class Property(var property: PropertyEntity?) {
return false
}

open fun validate() {}

open fun flush() {}
}
45 changes: 40 additions & 5 deletions src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ 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")],
inverseJoinColumns = [JoinColumn(name = "TO_ATTR"), JoinColumn(name = "TO_ENTITY")]
)
val targets : MutableSet<PropertyEntity> = 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<PropertyEntity> = HashSet(),
) {
// override Object
Expand Down

0 comments on commit 7f03d51

Please sign in to comment.