Hi, I'm don't expect that it's possible to impleme...
# exposed
o
Hi, I'm don't expect that it's possible to implement the structure like this directly with Exposed DAO. The trickiest place I see is querying multiple table (Cat, Dog) for resolving one field
Household::pets
. Which workaround I can imaginge. In one project I needed different states of one entity, I used for it
state(json)
column with actual data and
discriminator(enum)
column for resolving different states in different kotlin classes (I created custom json parser for it). Positive moment is that you need only one table, and one Exposed Table/Entity definion. Disadvantage is that you store json in the database (so if you have IDs inside state, or you want make complicated queries you may have problems) Another option I see is creating table
pet
with columns for every specific pet. For example:
Copy code
object Dog : IntIdTable("og")
    
    object Cat : IntIdTable("cat")
    
    object Pet : IntIdTable("pet") {
        val dog = integer("dog").references(Dog.id).nullable()
        val cat = integer("cat").references(Cat.id).nullable()
    }
This table could be used as an
PetEntity
with nullable links to other optional pets (
DogEntity
,
CatEntity
. Inside
PetEntity
the special function could be defined, that will return just any non null
Pet
. Here is the problem I see, is that adding any new pet is quite painful: you need to add new table, new column to
pet
table, new Table/Entity pair, and not forget update
PetEntity
to resolve new type of pets.
i
RE: polymorphic object relational mapping with Exposed. I have built something that meets my requirements and can support both class table inheritance and concrete table inheritance (and also a mix of both which is quite neat and what I'm rolling forward with). an experience share: I had to dig into Exposed internals to see how I could implement it in an Exposed idiomatic way. wasn't easy but wasn't too hard either, had something going within ~20 dev hours. these are patterns that Hibernate has support for out of the box and work (too) magically. the Exposed equivalent is a good bit wordier - and I am not including the general purpose Exposed extensions as part of that assessment, just the application specific dsl and dao mappings. now that the frameworking slog is behind me and I've built more applications with it, I actually appreciate the control that comes from stripping away the magic for "real" code and this new hybrid polymorphic strategy that I can do with it. that being said, I don't necessarily see everyone being able to go through the same journey to get there. now what? I want to see if there is any interest at all in this sort of thing and, if there is, what the community thinks should be done with it. some options I see: 1) it can be open sourced as a standalone library. main downside to this is that it would not be able to utilize Exposed caching mechanism as most of that is not part of the public API. 2) it can be contributed to Exposed proper, again if there is interest both from the user and maintainer sides. this is a much bigger undertaking in terms of testing, documentation, and integrating the internal caching mechanisms that I would likely need support for from other contributors.