1

I'm currently working on improving some old uni assignments moving them from serializable files to any other form of storage, mainly SQL Databases. I understand the concept of relational database design and the similarities with OOP Classes, however, I'm not entirely sure how to approach this issue from an OOP design perspective.

Right now I have a Hotel class with a List of Rooms as property, each Room has a list of Guests as property (full code here)

Back when using files I could mark these classes with the Serializable interface and store the parent object in a single file. But when using relational DB, I store each list as a single table and use separate queries to obtain the corresponding results. Same goes for the add() operation: with databases, I can do something like Guest.add() and add all the required fields directly to the database, whereas with my current design I need to call Room.getGuestList().add() (or a similar approach).

I totally understand that neither of both approaches is ideal, as both classes should be only worried about storing the data and not about the implementation of an add method, but even if I separate this in a single class, shall I still define a List property within each class?

I'm pretty sure I'm missing a design pattern here, but I cannot find the one that would solve this problem or maybe it's just that I've been taught wrong.

Thanks for your answers

Edit: I've decided thanks to the answers provided to transform my implementation following the DAO pattern as explained in this question and the Oracle documentation.

Narshe
  • 427
  • 8
  • 22
  • 1
    Maybe you are looking for an object-relational mapping solution like JPA? – erosb Feb 19 '18 at 19:20
  • I don't recommend JPA, especially for simple tasks like this. First for OOD take a look at the DAO/DTO pattern: http://www.oracle.com/technetwork/java/dataaccessobject-138824.html Second instead of ORM consider a lighter weight library instead: https://commons.apache.org/proper/commons-dbutils/ – markspace Feb 19 '18 at 20:26
  • Thanks @markspace your links have proven very informative. I already thought of having an inteface to perform CRUD operations, but I had it mixed with the actions to Load and Save the file. Thanks to these links I've noticed that I need to split both interfaces, one for CRUD, the other for Load/Save and also realized how to best approach the creation of the List object itself. – Narshe Feb 19 '18 at 21:57
  • "I understand the concept of relational database design and the similarities with OOP Classes". Hmmm. That sentence alone makes me doubt it. The thing is, they're fundamentally different paradigms. So if you start by thinking about the "similarities", it's almost certain to lead you astray. If you want to leverage the power of a relational model, you should do so by working within the framework of that model. Trying to force-fit another paradigm over the top is certain to be sub-optimal from both ends. (This is a fundamental issue I have with ORMs in general btw.) – Disillusioned Feb 20 '18 at 00:54
  • Might I suggest you'd be better off trying to tackle learning relational databases from a clean slate first. Only after that, see how best you can get the 2 working together. – Disillusioned Feb 20 '18 at 00:57

1 Answers1

2

Normally you would have 3 tables: hotels, rooms, guests. Rooms would have relation to hotel (hotel id) and guest would have relation to room(room id). That's it. Those relations can be easily reflected in OOP using some sort of ORM. JPA with Hibernate is an excellent example. Check that out. You will be able to get hotel, its rooms and all guests of hotel just like you described without using a single SQL query in your code.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Thanks, I'll take a look at JPA. The SQL design is already done, with the relationships defined by their own Foreign keys, so I just need to refactor my current project into something that will bring the data into the Java project without breaking the serialization in case I want(need) to go back to files. – Narshe Feb 19 '18 at 19:40
  • You wil have respectivly class `Hotel` that has a property `List rooms` etc. so `hotel.getRooms()` will be valid approach. – Antoniossss Feb 19 '18 at 20:03