Search code examples
javamysqldatabaseoop

Converting database data into objects


I'm building my first real Java application, and I'm confused about following good object-oriented design practices.

My application allows a yoga instructor to design a yoga session. There is a MySQL database with five tables:

  • a table of yoga poses,
  • a table for the warmup section of the session
  • a table for the work section of the session
  • a table for the restore section of the session
  • and finally a table containing the various yoga sessions the user has created.

The sessions are composed of sections, and the sections are composed of poses. I purposefully chose to use a database in this design rather than lists or arrays for the purpose of learning how to integrate a database with Java.

Here is my question: Is it really necessary to make objects out of the poses, sections and sessions?
They are merely data, and they don't have behavior of any kind. The real objects in my application are the windows and tools the user is using to assemble these poses and sections into yoga sessions. It just seems that my code will become unnecessarily inflated and complicated if I force each pose and section and session to be an object. I realize that I may be asking for opinions here, and that is generally discouraged on this forum. But my intent is to understand and follow object-oriented design in what seems to me to be a murky area.


Solution

  • To answer your question, Yes, create objects for each. The main principle of Object oriented programming is having different "types" of Objects and doing stuffs (Behaviors - methods) around them.

    I suggest you to look at the concept of ORM. ORM is the process of mapping Objects to their persistent representation, i.e Database tables.

    If you go for plain JDBC, you may need to write a lot of native SQLs and code to extract individual column's values from your table. When the complexity of your schema increases, it will get difficult to maintain these queries.

    With ORM, you can write simple java programs to get and save data from persistent layers.

    You can go through this thread to look at the ORM advantages

    Hibernate is a great framework available for Java which does ORM