Skip to content

Instantly share code, notes, and snippets.

@proteus-russ
Last active July 25, 2019 06:14
Show Gist options
  • Save proteus-russ/e0b3786bd79ccd82a509a129355d73ce to your computer and use it in GitHub Desktop.
Save proteus-russ/e0b3786bd79ccd82a509a129355d73ce to your computer and use it in GitHub Desktop.
abstract class Entity<T : Serializable> {
abstract var id: T
override fun equals(other: Any?): Boolean = when {
this === other -> true
other == null -> false
Hibernate.getClass(other) != Hibernate.getClass(this) -> false
else -> Objects.equals(id, (other as Entity<*>).id)
}
override fun hashCode(): Int = id.hashCode()
override fun toString(): String = "${javaClass.simpleName}#${this.id}"
}
@Entity
@Table(name = SomeRule.TABLE_NAME, schema = Config.SCHEMA)
class SomeRule @JvmOverloads constructor(
@get:Id
@get:GeneratedValue(strategy = GenerationType.SEQUENCE, generator = GENERATOR)
@get:SequenceGenerator(name = GENERATOR, sequenceName = GENERATOR)
@get:Column(name = ID_COLUMN)
@get:NotNull
override var id: Long = 0,
@get:Type(type = ZipRangeUserType.USER_TYPE_NAME)
@get:Column(name = "zipRange", length = 1024)
var zipRange: ZipRange? = null,
@get:NotNull
var baz: String
) : Entity<Long>() {
companion object {
const val TABLE_NAME = "SomeRule"
const val ID_COLUMN = "${TABLE_NAME}_id"
private const val GENERATOR = "${Config.SCHEMA}.${ID_COLUMN}_seq"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment