|
| 1 | +# MetaObjects :: OMDB Kotlin Facade (`omdb-ktx`) |
| 2 | + |
| 3 | +A thin Kotlin extension layer over the Java OMDB engine. |
| 4 | +No forking, no reimplementation — just idiomatic Kotlin syntax on top of the existing Java API. |
| 5 | + |
| 6 | +## Dependency |
| 7 | + |
| 8 | +```xml |
| 9 | +<dependency> |
| 10 | + <groupId>com.metaobjects</groupId> |
| 11 | + <artifactId>metaobjects-omdb-ktx</artifactId> |
| 12 | + <version>${project.version}</version> |
| 13 | +</dependency> |
| 14 | +``` |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Before / After |
| 19 | + |
| 20 | +### Transaction management |
| 21 | + |
| 22 | +**Before** — raw Java engine: |
| 23 | + |
| 24 | +```java |
| 25 | +ObjectConnection conn = omdb.getConnection(); |
| 26 | +try { |
| 27 | + conn.setAutoCommit(false); |
| 28 | + omdb.createObject(conn, widget); |
| 29 | + conn.commit(); |
| 30 | +} catch (Exception e) { |
| 31 | + conn.rollback(); |
| 32 | + throw e; |
| 33 | +} finally { |
| 34 | + omdb.releaseConnection(conn); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +**After** — Kotlin facade: |
| 39 | + |
| 40 | +```kotlin |
| 41 | +omdb.transaction { session -> |
| 42 | + session.create(widget) |
| 43 | +} |
| 44 | +// Commits on normal return, rolls back and rethrows on any exception, |
| 45 | +// always releases the connection. |
| 46 | +``` |
| 47 | + |
| 48 | +The block's return value is propagated, so you can read and return in one call: |
| 49 | + |
| 50 | +```kotlin |
| 51 | +val count: Int = omdb.transaction { session -> |
| 52 | + session.create(widget) |
| 53 | + 42 // returned to the caller |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### CRUD operations |
| 60 | + |
| 61 | +```kotlin |
| 62 | +omdb.transaction { session -> |
| 63 | + session.create(widget) // INSERT |
| 64 | + session.update(widget) // UPDATE |
| 65 | + session.load(widget) // reload fields in-place |
| 66 | + session.delete(widget) // DELETE |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### Querying with the DSL |
| 73 | + |
| 74 | +```kotlin |
| 75 | +val widgetMeta: MetaObject = registry.findMetaObjectByName("acme::Widget") |
| 76 | + |
| 77 | +val results: Collection<*> = omdb.transaction { session -> |
| 78 | + session.find(widgetMeta) { |
| 79 | + where(field("quantity") gte 10) |
| 80 | + orderByAsc("name") |
| 81 | + limit(50) |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +Available infix operators on `field(name)`: |
| 87 | +`eq`, `ne`, `gt`, `lt`, `gte`, `lte` |
| 88 | + |
| 89 | +Compound predicates use the `Expression` instance methods, which Kotlin can call as infix: |
| 90 | + |
| 91 | +```kotlin |
| 92 | +where((field("quantity") gte 10) and (field("name") ne "archived")) |
| 93 | +``` |
| 94 | + |
| 95 | +`session.find` runs the query on the **session's own connection**, so writes made earlier |
| 96 | +in the same transaction are visible immediately (read-your-writes). This contrasts with |
| 97 | +`QueryBuilder.execute()`, which opens a new pool connection and cannot see uncommitted rows. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +### Resolving an object by string reference |
| 102 | + |
| 103 | +```kotlin |
| 104 | +val widget: Widget? = session.findByRef<Widget>("objectref://acme::Widget/42") |
| 105 | +``` |
| 106 | + |
| 107 | +Returns `null` if the reference does not exist. Name resolution (FQN → `MetaObject`) is |
| 108 | +delegated to the engine's global loader registry, which is populated during Spring/OSGi |
| 109 | +bootstrap. In a standalone context without active registry bindings this may throw |
| 110 | +`MetaDataNotFoundException` rather than return `null`. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### Spring-managed connections |
| 115 | + |
| 116 | +When a `@Transactional` Spring method owns the connection lifecycle, use |
| 117 | +`withSpringConnection` instead of `transaction`. It does **not** commit, rollback, or |
| 118 | +close — the surrounding transaction owns all of that. |
| 119 | + |
| 120 | +```kotlin |
| 121 | +@Service |
| 122 | +class WidgetService( |
| 123 | + private val omdb: ObjectManagerDB, |
| 124 | + private val dataSource: DataSource, |
| 125 | + private val widgetMeta: MetaObject, |
| 126 | +) { |
| 127 | + @Transactional |
| 128 | + fun createWidget(widget: Widget) { |
| 129 | + omdb.withSpringConnection(dataSource) { session -> |
| 130 | + session.create(widget) |
| 131 | + val others = session.find(widgetMeta) { |
| 132 | + where(field("name") eq widget.name) |
| 133 | + } |
| 134 | + // session uses the Spring-bound connection — sees the uncommitted create above |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +### Coroutines |
| 143 | + |
| 144 | +`Coroutines.kt` provides suspend wrappers over the engine's async APIs: |
| 145 | + |
| 146 | +```kotlin |
| 147 | +import com.metaobjects.omdb.ktx.awaitGetObjects |
| 148 | +import com.metaobjects.omdb.ktx.awaitExecute |
| 149 | +import kotlinx.coroutines.runBlocking |
| 150 | + |
| 151 | +// Simple suspend fetch (no filter): |
| 152 | +val widgets: Collection<*> = runBlocking { |
| 153 | + omdb.awaitGetObjects(widgetMeta) |
| 154 | +} |
| 155 | + |
| 156 | +// Filtered suspend query via QueryBuilder: |
| 157 | +val filtered: Collection<*> = runBlocking { |
| 158 | + omdb.query(widgetMeta) |
| 159 | + .where(field("quantity") gte 10) |
| 160 | + .awaitExecute() |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +Note: `awaitExecute()` calls `QueryBuilder.executeAsync()`, which opens a new connection |
| 165 | +from the pool. It does not share a session connection, so it cannot see uncommitted writes. |
| 166 | +Use `session.find(...)` inside a `transaction` block when read-your-writes semantics matter. |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## Design notes |
| 171 | + |
| 172 | +- **Thin wrapper, not a fork.** Every call delegates to the same Java engine methods |
| 173 | + (`createObject`, `updateObject`, `getObjects`, etc.). There is no reimplementation of |
| 174 | + persistence logic. |
| 175 | +- **`OmdbSession` is a value pair.** `class OmdbSession(val manager: ObjectManager, val connection: ObjectConnection)` — it exists solely so extension functions (`create`, `find`, …) can read naturally without requiring two separate parameters at every call site. |
| 176 | +- **`find` runs on the session connection.** `QueryBuilder.build()` produces `QueryOptions`; the facade then dispatches through `ObjectManager.getObjects(connection, metaObject, options)` on the session's own connection. This is the one place where the facade does more than forward a call — it selects the right overload to preserve transaction visibility. |
| 177 | +- **`findByRef` uses the engine's global registry.** The engine resolves a string reference to a `MetaObject` via whatever `MetaDataLoaderRegistry` bindings are active. In production (Spring/OSGi bootstrap) this works transparently; in a bare unit-test context you must register the relevant `MetaObject` yourself. |
0 commit comments