Using Exposed, is it possible to define tables and entities in one class?
I'm generally liking Exposed, but I've got a feeling explicitly defining both tables and entities will get tiresome as I build a larger app.
Is it possible to do it in one class, as other ORMs (e.g. Hibernate) allow? I'm open to slightly hacky solutions.
Alternatively, does anyone have recommendations for Kotlin ORMs? I know Hibernate is an option but I'm keen to try something that's designed specifically for Kotlin.
Posted Feb 9, 2022, 9:57 PM UTC
28
A better question is, why would you want to mix each table and entity into one single class? I aint sure how many LOCs you'd save by combining them into one class. Is it really that tedious for you to define them separately?
Theres a good reason why Exposed is designed this way. The tables are persistence logic that deal with mapping entity to database record, while the entity is where your business logic resides. They technically belong to different layers of your application.
By mixing domain and persistence concerns into one class, your class will violate SRP and separation of concerns/layers. Its not a good idea to couple your domain logic with persistence, it makes your classes harder to maintain and more costly to change with new business requirements.
So my advice is, dont do this even if you may be able to. Always aspire to write maintainable software instead, it will pay off in a long run.
This is like a parody of a stack overflow non-answer.
What I'm really looking for is transparent persistence of my domain objects. Defining the persistence layer automatically is something many other ORMs do - and result in perfectly maintainable software in the long run.
I'm not sure how can he LOCs of code I'll save. Probably only a few hundred in a large project. Enough to make me want to ask the question.
Your problem is that you chose a library that is not what you're looking for. Exposed is not an ORM and does not want to transparently persist your domain object. Exposed is intentionally one step lower than an ORM.
You want Hibernate or something similar.
Well he specifically asked about others as well
I'm not the OP, but I had a similar "concern" (not really a question because I know the answer is "you can't").
My issue with your take here (or is this explicitly mentioned somewhere by Exposed?) is that I don't see the entities as part of the domain layer. The entities are simply a representation of a row in the table. My domain models are always defined separately from the tables+entities, even if they happen to map 1-1. Because some of my domain models are created from rows from multiple tables, or a subset of rows from a table.
So, yes, you should separate domain models from persistence logic, but no, entities are not part of the domain. Especially not in Exposed, because in Exposed your entities are decorated with all kinds of persistence junk. From the README:
You shouldn't be using the above class in your business logic, IMO.