1

I have to implement a web application which search persons and I want to make a model for a person who works in different companies and have a certain grade like engineer or technician or ...

person works in one company
company have many person
company have many grade
grade can be in many company

So what class diagram will be the best:

1)
enter image description here

2)
enter image description here

3)
enter image description here

The web application will search by name or address in table personor by grade or by company or all combined. I will use hibernate for mapping my database (mysql).

Hayi
  • 6,972
  • 26
  • 80
  • 139

3 Answers3

1

1st one makes no sense as there is no direct link from person to grade.

2nd is what you need, a person belongs to a company and has a grade.

EDIT

3rd option after the OP edit seems to fit your needs if you also want to link the grades to a company. This is actually a combination of 1st and 2nd.

A4L
  • 17,353
  • 6
  • 49
  • 70
  • but in my case each company have grades. and i have to search by grade. – Hayi Jan 14 '14 at 19:39
  • @Youssef, what should be the result of your search / what do you wanto to search for using the grade? And you dont say any thing about the mutipliciy, A person can also belong to more that one company and have a grade in each company they belong to. – A4L Jan 14 '14 at 19:41
  • 1
    @Youssef check my edit, try to implement that model in your DB and write some plain SQL-queries to test it, after you can move to OR-mapping with JPA. – A4L Jan 14 '14 at 19:54
  • so even if i have a loop in 3 tables it doesn't matter ? – Hayi Jan 14 '14 at 19:56
  • 1
    @Youssef why loop? you can write one query to get the result you want at once, see [JPA query language](http://en.wikipedia.org/wiki/Java_Persistence_Query_Language). [(oracle docs)](http://docs.oracle.com/javaee/5/tutorial/doc/bnbtg.html). Once you have the entities you are searching for you can access all their related data using the member fields which you have annotated accordingly. – A4L Jan 14 '14 at 20:00
  • thanks and yes i use hibernate take a look [here](http://stackoverflow.com/questions/21123228/query-on-many-to-one-relationship) – Hayi Jan 14 '14 at 20:26
1

I agree that #3 is the way to go. If you're implementing the database structure yourself, which it sounds like, I recommend drawing a detailed (EER) diagram, showing the types of relationships (like one-to-many) and listing the keys.

I know you didn't explicitly ask, but this would be your next step. What would be the primary key of each element? Do you need 'intermediary' tables to handle many-to-many relationships, like the GRADE and COMPANY tables?

lmcgowin
  • 23
  • 7
  • exactly i want to know if i need 'intermediary' table for GRADE and COMPANY ? because i have no extra information i would put in this table. For the rest primary key and foreign key it's okey. – Hayi Jan 14 '14 at 20:24
  • I posted earlier a topic ([here](http://stackoverflow.com/questions/21113593/many-to-many-relation-and-relational-model)) but i didn't found a good solution. – Hayi Jan 14 '14 at 20:27
  • it depends on what technologies you're going to use, but an intermediary table will give you the most normalized option. – lmcgowin Jan 17 '14 at 19:43
1

enter image description here

Don't forget the aggregation - for the connection person-firm it should be, for person-grade it can be.

Don't forget the multiplicity - A person can have job or not. A person always has some best grade he/she reached. For any grade there are at least one person. A company can have any number of employees, including 0.

In the real life I name connections ends only when their names are different form the classes they point to. But for study it is better to use end names always - for better understanding.

And the main thing - arrows. A person has references to a company, but not vice versa. And the same with the grade.

Notice for the future - by setting arrows/crosses, multiplicities and aggregations/compositions (if there are some), you can check the correctness of the diagram. If you tried to finish your diagrams, you would notice, that you CANNOT do it for diagrams #1 and #3. So, they are bad.

Of course, you cannot check this way the connection between your model and real life, only the inner logic of the model. But it is a good and very useful check. Don't neglect it.

As for the third diag, it seems, you are mixing terms "grade" and "specialization". If you need to take into account both of them, you need the following diag:

enter image description here

The "is next greater than" connection sets the staircase of grades.

If you need only specializations and no grades, simplify the diagram - put out all connections to "grade" and its class. Notice, you'll get a diag you haven't mentioned at all.

Gangnus
  • 24,044
  • 16
  • 90
  • 149