diff --git a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityClass.kt b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityClass.kt index 1dabc600ad..174190139e 100644 --- a/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityClass.kt +++ b/exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/EntityClass.kt @@ -17,8 +17,22 @@ abstract class EntityClass, out T : Entity>(val table: I internal val klass: Class<*> = entityType ?: javaClass.enclosingClass as Class private val ctor = klass.kotlin.primaryConstructor!! + /** + * Get an entity by its [id] or throws [EntityNotFoundException] if entity is not found. + * + * @param id The id of the entity + * + * @return The entity that has this id or [EntityNotFoundException] entity was found. + */ operator fun get(id: EntityID): T = findById(id) ?: throw EntityNotFoundException(id, this) + /** + * Get an entity by its [id] or throws [EntityNotFoundException] if entity is not found. + * + * @param id The id of the entity + * + * @return The entity that has this id or [EntityNotFoundException] entity was found. + */ operator fun get(id: ID): T = get(DaoEntityID(id, table)) protected open fun warmCache(): EntityCache = TransactionManager.current().entityCache @@ -70,8 +84,31 @@ abstract class EntityClass, out T : Entity>(val table: I } } + /** + * Get an entity by its [id] from cache or returns null if cache does not contain it. + * + * @param id The id of the entity + * + * @return The entity that has this id or null if no entity was found in cache. + */ fun testCache(id: EntityID): T? = warmCache().find(this, id) + /** + * Get an entity by its [id] from cache or returns null if cache does not contain it. + * + * @param id The id of the entity + * + * @return The entity that has this id or null if no entity was found in cache. + */ + fun testCache(id: ID): T? = testCache(DaoEntityID(id, table)) + + /** + * Get a sequence of entities matching given [cacheCheckCondition] from the cache. + * + * @param cacheCheckCondition The condition for selecting the entries from the cache. + * + * @return The sequence of entities from the cache matching the given predicate. + */ fun testCache(cacheCheckCondition: T.() -> Boolean): Sequence = warmCache().findAll(this).asSequence().filter { it.cacheCheckCondition() } fun removeFromCache(entity: Entity) { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityTests.kt index 9ad92b5630..60716bfa0f 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/entities/EntityTests.kt @@ -346,13 +346,17 @@ class EntityTests : DatabaseTestsBase() { withTables(Boards) { val board1 = Board.new { name = "irrelevant" } assertNotNull(Board.testCache(board1.id)) + assertNotNull(Board.testCache(board1.id.value)) board1.delete() assertNull(Board.testCache(board1.id)) + assertNull(Board.testCache(board1.id.value)) val board2 = Board.new { name = "irrelevant" } assertNotNull(Board.testCache(board2.id)) + assertNotNull(Board.testCache(board2.id.value)) Boards.deleteWhere { Boards.id eq board2.id } assertNull(Board.testCache(board2.id)) + assertNull(Board.testCache(board2.id.value)) } }