Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using Hibernate ORM should i model first a class diagram or DB diagram?

i'm fairly new to Java and Hibernate. At work we are developing a medium sized, formprocessing J2EE web application using Spring, Hibernate, JBOSS and so on. What is the correct approach using Hibernate? Should i create first a class diagram and the map it using hibernate to DB Tables or should i first model the DB Tables and then map it to Hibernate Entities? Or does it depend? If it depends than on what? Has either of this approaches drawbacks against the other? Is it possible to map "any" class diagram to a DB using Hibernate 4?

like image 628
Robert Niestroj Avatar asked Oct 08 '11 15:10

Robert Niestroj


2 Answers

Both approaches are correct, but used in different situations.

  1. When creating a new application (new model) ir is ussual to create the entities first and let hibernate/JPA to create the tables. It is more simple and will probably generate better object model (since you are creating it directly). But you still have to have in mind, that you are creating DB tables to, so you should think also about DB normalization etc.
  2. You will use the approach with tables first, when mapping some legacy schema, where you have no choice but to do it. The object model might be a little clumsy...

But I repeat, both approaches are valid, if you are more DB engineer than Java programmer, you wold probably do 2) because it might be more natural to you. I as a Java programmar do (almost) always 1)...

You can the same results with both approaches and in both you have to think about, what will hibernate generate for you...

like image 180
malejpavouk Avatar answered Oct 12 '22 23:10

malejpavouk


It doesn't make any difference: either is mappable to the other, but you need to be aware of both (the ORM impedance mis-match).

If you're doing greenfield development, IMO, go from class diagram => DB tables; it's easier to think in classes. In general, rational class structures map nicely to DB tables, but keep efficiency in mind (the "be aware" part).

like image 26
Dave Newton Avatar answered Oct 13 '22 00:10

Dave Newton