Kest

Search

Search the documentation

Data

MikroORM

Collect changes, then flush them together.

MikroORM's EntityManager collects changes and writes them together on flush(). Kest has the same useful core as UnitOfWork:

val uow = UnitOfWork(transactionManagers.get("main"))
val order = uow.persist(orders, Order(customerId = id, total = 40))        // queued, nothing written yet
uow.persist(stock, stock.findById("A1")!!.copy(qty = 9))
uow.remove(carts, cart)
uow.flush()                                                              // runs everything in ONE transaction
order.get()                                                              // the saved order, with its generated id
  • All or nothing. If one write fails, none is applied and the queue is kept so you can inspect it.
  • Identity map. uow.find(repo, id) returns the same instance for the same id during the unit's life.
  • Entities are immutable data classes, so there is no automatic change detection: you persist the changed copy (copy(qty = 9)).
  • A unit is for one piece of work (a request) and is not thread-safe.

@Transactional still groups writes in a method; use UnitOfWork when the writes are decided across several calls.

Read more