Search code examples
javahibernatedatabase-designarchitectureentity-relationship

Hibernate does kill my Java architecture?


I have read this great article by Mkyong about one-to-many relationship in Hibernate and I have a big doubt. Mkyong uses two clase in his example: Stock.java and StockDailyRecord.java

I think the class diagram has to be: "A stock has many stockdialyrecords" and therefore as database entities diagram has to be: "stock one-to-many stockdialyrecords".

But Mkyong has gone further, and here is where appear my doubt. He set an attribute named Stock stock on the StockDialyRecords class. That said, he create a bidireccional relationship. This is right? I understand that this way for ORM, we can walk in both directions. But from Diagram class point of view is like it says: "A Stock has many StockDialyRecords and a StockDialyRecords has a Stock"

I say it from point of view of the concept of architecture. Is like it is a cycle or paradoxical.


Solution

  • It's bidirectional as it represents two different relationships:

    • stock has many records (one-to-many)

    • record references stock (many-to-ony)

    Both relationships have sense from business perspective. The latter seems even more important - each record must have a reference to a stock it describes.

    From technical perspective bidirectional relationship has some advantages, e.g. it allows you to build queries from both sides.