wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Mapping Classes to Tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping Classes to Tables" Watch "Mapping Classes to Tables" New topic
Author

Mapping Classes to Tables

Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hai All,

This is my first shot in doing a design the object oriented way. I am not sure if i am doing it correct though. I have learn how to program in java but havent done a serious design from scratch.

Currently I am reading the book Head First OOAD on how to correctly design my classes correctly.

If I have an Employee Abstract Class and I have subclasses RegularEmployee, ContractualEmployee and TransientEmployee, how will I map this classes into actual tables in my database?

My first design was to build two tables Employee Table and EmployeeType Table. Then I will shift the key of the EmployeeType table to the Employee Table as foreign key.

According to me, this is how I will design it. But I am not sure if there is flaw in my design. Likewise, I still dont know yet how I will map my many classes in my class diagrams to actual tables in my DB. I am not sure of my understanding, but should I have a complete class diagrams first before I create my table design?

Looking forward to your comments. Thanks.


Sean Clark ---> I love this place!!!
Me ------> I definitely love this place!!!
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1756
    
    7

The difficulties you are facing in trying to map your Java classes to database tables are due to the paradigm mismatch between the object-ori´┐Żnted and relational approaches to representing (the same) data.
One way of dealing with this problem is a technique called object-relational mapping (ORM).
In your case ORM would allow you to map your Java classes to a relational database, using some form of meta-data (e.g. mapping files or annotations) to describe how the objects should be mapped to the database (including properties and associations to other objects).
Developing an ORM solution is no trivial task, but luckily there are a lot of ready-to-use solutions out there, such as Hibernate and Toplink.
Note that this (ORM) might not be the best solution to your particlar problem, but you should definately look into it.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi, I really appreciate your response.

I dont know that I am running into some trouble with what I am doing. Since this will be the first time I am doing some sort of design. My first assumption is that once I am finished creating the class diagram of my design then I can proceed to mapping it in my database tables.

I have'nt done some reading about ORM but thanks to you, I do have some overview on what an ORM use is.

Judging from what you have taught then perhaps I should fall back first into designing tables relationally.

Just one question though, should I opt not to study ORM as of the moment then should the first step in my design is to design my tables first then after which proceed to doing other UML diagrams that might be needed?

Is that how object oriented software is being developed? My assumption is that ORM is not yet implemented here and all calls would be purely JDBC.

Thanks
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
From my exp:

OOAD can be split into two phases:
1. Analysis Phase
2. Design Phase

Analysis phase is a technology agnositic phase. In this phase, the class diagrams are independent of technology.
The diagrams in this phase are of less detail than in the Design phase.

Classes in the Analysis or Design phase have different sterotypes, Entity, Control and Boundary classes.
Entity classes represent persistent objects.

Originally posted by mark reyes:
My first assumption is that once I am finished creating the class diagram of my design then I can proceed to mapping it in my database tables.
.....
Judging from what you have taught then perhaps I should fall back first into designing tables relationally.



Class diagram (boundary, control, entity) cannot be finished in one sitting. It keeps evolving and slowly stabalizes.

If you plan to use ORM soultion for persistence, then don't think in relational way.
You create your entity object model and your persistence engine should create the relational model for you. Don't worry how
many tables are used to store RegularEmployee, ContractualEmployee etc. Just worry on your object model.


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
hi, thank you for your detailed explanation.

I had already read halfway through this Great Book HFOOAD, and I have learned a lot. Head First Books really rock!

My only concern is that I had skim even through the last chapters but no chapters related to DB Mapping my classes was discussed. Currently all the projects I am working are DB Intensive so I was looking for a chapter on mapping.

Right now, I have no knowledge implementing ORMS solutions so I was thinking of a way on how to map my classes to actual tables. Do you know some good resource/books/links that I could read/buy to further strengthen my knowledge on how OO Software? I was overwhelmed on the FAQ of this forum and was quite confuse on terms OOAD, Design Patterns, Refactoring... Thanks.
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Originally posted by mark reyes:

Right now, I have no knowledge implementing ORMS solutions so I was thinking of a way on how to map my classes to actual tables.

If the tables are already exisitng; then you have to use some tools to genereate Entity classes from your existing schema. Explore Hibernate tools section for this.

Mapping of classes to actual tables can be done in several ways; depends on the persistence engine; for e.g. for hibernate, you can use annotations or configuration/mapping files etc.

Read Hibernate primer articles on the internet, that should give you a head start.

Originally posted by mark reyes:

Do you know some good resource/books/links that I could read/buy to further strengthen my knowledge on how OO Software? I was overwhelmed on the FAQ of this forum and was quite confuse on terms OOAD, Design Patterns, Refactoring... Thanks.


OOtips

Some books on Amazon
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
Hi Rajah, thanks again for the links.

By the way, I am almost over reading the book but it left me hanging at one point, does OOAD have no stage on designing the tables that you will use from the static classes that you have formed? or designing tables would warrant another phase in your development design independent of what you have developed in the design phase?

I have read primer on ORM but that would require long read and another day. I am not sure of these, but is ORM my only choice to mapped my classes into physical tables?

I assume that my table design from my earlier question regarding the Employee and its subclasses would be enough for my current data needs. Thanks again.
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Usually, the next step when you get a set of requirements - is to come up with a domain model i.e. high level entity model or data model.
This means, OOAD phase starts with the entity classes, their relations. This gives an understanding on the relationship between different classes and these result as the tables in the database. Everything else is built above this entity model i.e. controller classes, boundary classes etc.

Now I'm sure either one of us is confused :

In your case - there are two activities 1) Physical table design 2) Class design and you are struck with when and how to map the 2 and 1.

In my approach, I have only Class design (Entity classes) and this is becines my table / schema design.

If it is new system, I do the entity model and generate my physical model (using hibernate).
If it is already existing system and there is some enhancement required, then I reverse engg and get the entity model from the phyical model to start my development.

Request others to share their insights .....
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
In your case - there are two activities 1) Physical table design 2) Class design and you are struck with when and how to map the 2 and 1.


hi, your absolutely correct. I have been looking for the right words to express this.

Most of the project that I had participated were all already built and what I am doing are just some small enhancements. I just change the logic or tweak the GUI but I think its not that much.

I have less than a year experience programming in Java but I think I am comfortable with basic OO concepts and programming itself.
Now I want to build my own system at home apart from what I am doing at work and I want to learn how to design it correctly.
I cant wait for my boss to give me a new system before I could practice how to design.
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
Hi Mark, if I can explain my point of view, I think that relational layer and object diagram layer should remain separated. This because you can tune better each layer if you design it separately from the others. When you're done with both the two layers, I'd then start thinking how to map object diagram to tables and viceversa. At this point, tools like Hibernate can come in hand to handle this type of work. But if you let these tools make your table diagram starting from the object one or viceversa, the result *could* be not as good or fine grained as the one you'd do by yourself, although of course it will probably work fine. Please note that I use the term "could", meaning that I'm not stating they'll do for sure a horrible work, but simply that the final result could not be so smart as the one you design specifically to your project.
As for the designs, there are near always strategies and tecniques you can apply to both the layers to make a better design (normalization on DB tables, design patterns on class diagrams and so on). That's why, in my opinion, to handle the design of these layers is quite crucial and could make the difference when it comes to mantain a growing system or adding new features.
Mark Reyes
Ranch Hand

Joined: Jul 09, 2007
Posts: 426
hi all, thanks for all your insights. I will try to read more regarding this topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping Classes to Tables
 
Similar Threads
Joining tables
mapping for subclass - forcing query to join-
How many class diagrams do we really needed?
UML / Relational Modeling
Hibernate problem