Skip to content

Commit

Permalink
hibernate sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsamson7 committed Sep 27, 2024
1 parent 7f03d51 commit 409a014
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 36 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/AttributeDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ class AttributeDescriptor<T:Any>(name: String, val type: Type<*>, val isPrimaryK
override fun isAttribute() : Boolean {
return true
}

// override Object
override fun toString(): String {
return "${this.name}"
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/ObjectDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class ObjectDescriptor(val name: String, val properties: Array<PropertyDescripto
fun property(property: String) : PropertyDescriptor<Any> {
return properties.find { prop -> prop.name == property}!!
}

// override Object
override fun toString(): String {
return "${this.name}"
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/org/sirius/dorm/model/RelationDescriptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ open class RelationDescriptor<T:Any>(name: String, val target: String, val multi
fun isOwner() : Boolean {
return owner
}

// override Object
override fun toString(): String {
return "${this.name}"
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/org/sirius/dorm/object/Attribute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}
8 changes: 4 additions & 4 deletions src/main/kotlin/org/sirius/dorm/object/MultiValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, PropertyEntity>()
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down
32 changes: 13 additions & 19 deletions src/main/kotlin/org/sirius/dorm/object/SingleValuedRelation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<PropertyEntity> = 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<PropertyEntity> = 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()
}
}

0 comments on commit 409a014

Please sign in to comment.