Skip to content

Hibernate mapping between different database and domain model

I have created a domain and database model. The domain model is for a rest api. The both have similar components like product, category, customer, retailer, etc… But if you take a closer look at both models. You can see some differences. For example, you can see that in the database model the retailer has multiple products. But in the domain model you can see that each product has a single retailer. The reason for doing it this way is because i need to be able to return a list of multiple products with different retailers.

You can also see in the domain model that the product has productproperties. In the database this is also done in a different way than the domain model. The database model has a column of properties(for example: height, weight, resolution, etc..). In the database a product has a productproperty which has a value and and a property.

Domain model:

Domain model

Database model/design:

Database model

I did some research on Hibernate and made a test project to see how this mapping works. I got a simple project working. But when i started on creating a project with these models i got stuck. I couldn’t find a way to make it work.I did read something about “POJOs” So maybe a solution could be to create pojos for the database and then kind of map these to my domain models, but i’m not sure about that. So my question is how do create a database like the model with domain model with hibernate mapping?

PS: i know that the database model is not completely up to date with the domain model.



This is question dosen’t have a simple answer, because you are asking for something like:

please make this work.

You should either generate the DDL from your hibernate java classes (top down) or you generated the hibernate java classes form your DB-Schema (bottom up).

The top down approach is supported by hibertnate directly – just google for generate DDL with hibernate.

The bottom up approach is supported with IntelliJ IDEA. This blog posts explains how this can be done:

I would suggest that you follow the top down approach, because this is tool independet and better supported.

Developing hibernate classes and data base schema for different purposes lead to mapping issues you don’t want to have.

Furthermore, you are talking about your domain model that is used for a REST API. I would suggest, that you use your hibernate java class model as an internal domain model – may be not perfect but good enough. The REST API class model is exposed to the outer world and changes to that model should be done with care, because you may break some of your consumers when you change that model.

How should you continue?

You should go for the top down approach. Create java classes reflecting your Entity Relation Diagramm es much as possible, add hibernate annotations to them, use drop-create to drop and recerate the schema at startup (read and make sure the application starts, create the schema successfully as fast as possible.

With that approach you get immediate feedback if you mapping works.

More advance: Use Unit Tests against a real database to make sure your hibernate model works as expected.

User contributions licensed under: CC BY-SA
4 People found this is helpful