Sagar
Sagar

Reputation: 997

Convert hibernate orm entities into complex objects and vice versa

I'm new to ORM and hibernate. my application is of complex design pattern. with builders, fluent interface. and those objects also throws exceptions while creating illieagel objects. and I use orm mapping to access database. I convert those ORM entities to my complex objects and vise versa. is it good idea or any other alternate.

Upvotes: 0

Views: 670

Answers (2)

Ean V
Ean V

Reputation: 5439

As a general rule, you should create business objects (in your case they exist already) when you truly need them. So, if your application needs those complex objects, that is fine (but keep in mind that they are hard to maintain as you will have to change a bunch of objects when you make a change in your database and Hibernate objects). If you could get rid of those complex objects, you could use Hibernate's detached entities as simple DTOs all over your application and you won't have the difficulty of maintaining two sets of objects. On the other hand, using business objects can make your web layer (or other layers) independent of Hibernate and its entities, so makes your life easier if somehow in the future you decide not to use Hibernate. From my experience, if the recent is not your case and you are thinking of Hibernate as a long term solution, using Hibernate's detached entities is a much easier solution.

Upvotes: 2

Martin Podval
Martin Podval

Reputation: 1117

Do you have some requirements which says that you need two kind of entities: those rich/complex and those ORM based?

I used ORM along with domain driven design and it worked fine. We decoupled rich entities (and value objects) from services and those entities were persisted from aggregate downwards.

You certainly must slightly change those entities when you want to use hibernate mapping but I haven't find anything which would break our DDD model. E.g. parameterless constructor can be private etc.

As we used fluent/xml mapping, model was completely separated from persistence layer, see term persistence ignorance

Upvotes: 1

Related Questions