Hello....
Pls guide me on how to model many to many relationship for entity beans. Here's the scenario....
1. A project has a list of consultants.
2. A consultant can be a member of one or more projects.
How do you model this in EJB?
Your comments will be very much appreciated. Thanks.
Jerson
-
Urgent: Many to Many Relationship (5 messages)
- Posted by: Jerson Chua
- Posted on: July 31 2000 00:46 EDT
Threaded Messages (5)
- Urgent: Many to Many Relationship by Jeff anderson on July 31 2000 01:41 EDT
- Urgent: Many to Many Relationship by Jerson Chua on July 31 2000 02:20 EDT
-
Urgent: Many to Many Relationship by alex black on July 31 2000 03:31 EDT
- Urgent: Many to Many Relationship by Jeff anderson on July 31 2000 06:47 EDT
-
Urgent: Many to Many Relationship by alex black on July 31 2000 03:31 EDT
- Urgent: Many to Many Relationship by Jerson Chua on July 31 2000 02:20 EDT
- Urgent: Many to Many Relationship by Jerson Chua on August 05 2000 04:40 EDT
-
Urgent: Many to Many Relationship[ Go to top ]
- Posted by: Jeff anderson
- Posted on: July 31 2000 01:41 EDT
- in response to Jerson Chua
Ok I am new to EJB but my answer would be (this asumes ejb1.1)
1)have the consultant entity bean have a vector that would contain a reference to all related project beans.
2)when the consultant bean is loaded have it retrieve a list of all project primary keys that this consultant belongs to and store it in the vector.
2)use lazy loading for the policy beans to replace the primary keys stored in the vector with entity beans on a as needed basis.(I think there is a pattern here on Lazy Loading).
3)project bean will also contain a vector that will be used to track consultants in the exact same way mentioned above.
Hope this makes sense
Jeff -
Urgent: Many to Many Relationship[ Go to top ]
- Posted by: Jerson Chua
- Posted on: July 31 2000 02:20 EDT
- in response to Jeff anderson
Thanks you very much Jeff...
Hmmm... That's actually what I'm also thinking. I saw something like that in the pet store example and in Richard Monson-Haefel book but the examples are for one to many relationship so I wasn't sure if i can also do this for many to many relationship.
Another thing, what's your opinion on using finder method? like using findbyConsultant in Project which returns a collection of project and same goes with consultant where you have findByProject. In this design, we don't actually have association between the two entities instead we query the list of projects and list of consultants. So when do we use finder method or use association in order?
I think for our case. Your solution is much appropriate.
Other alternatives are still welcome...
Thanks....
Jerson -
Urgent: Many to Many Relationship[ Go to top ]
- Posted by: alex black
- Posted on: July 31 2000 15:31 EDT
- in response to Jerson Chua
Hi Jerson,
I use finders when I have a 1 to many relationship. In the database this is represented by a foreign key in the child object's table. Then the child has a findByParentKey() finder.
For many to many, in the database I have an association table, with two columns for the primaryKeys of each object in the association. and I create an entity bean that represents that association, it has two finders, one for each type of object in the association.
If you write a finder for a many-many relationship in the child object, ie findByParentKey(), then the SQL for that finder probably joins the association table and the child table, by the parentKey. This seems awkward. I prefer the SQL for an object to only access that object's table.
- Alex -
Urgent: Many to Many Relationship[ Go to top ]
- Posted by: Jeff anderson
- Posted on: July 31 2000 18:47 EDT
- in response to alex black
Jerson
I think finder methods are a good way of getting a list of the dependant Entity beans, so is using an lookup table for many to many. (that is the way I would model them in the database) I don't think there is one specific way that is wrong or right in tackling this type of problem, rather there are a number of different solutions. A project bean could be responsible for returning consultant beans using a finder method on demand with a method like getAllCOnsultants(), that's probably a better idea and easier than storing the keys in a vector. Of course you would have to do some of your own querying because you are not likely to have the project keys stored in a consultant table ,rather you would need to get a list from an association table like the one mentioned by Alex. I beleive EJB 2.0 handles alot of this stuff transparantly,I hope it gains support soon, having to hard code database relationships seems a little backward to me.
Jeff -
Urgent: Many to Many Relationship[ Go to top ]
- Posted by: Jerson Chua
- Posted on: August 05 2000 04:40 EDT
- in response to Jerson Chua
Hello
Thanks to both of you....
I think I'm going for the entity bean which represent the relationship of the two entity beans. The one suggested by Alex and by Ed Roman in his JavaOne presentation.
Pls check this pseudo code, if I understand it right.
class Works_OnEJB {
private int consultantID;
private int projectID;
private ProjectEJB project;
private ConsultanttEJB consultant;
Collection findByConsultantID(int consultantID) {
....
}
Collection findByProjectID(int consultantID) {
....
}
ProjectEJB getProject() {
if (project == null)
project = ProjectEJBHome.findbyPrimaryKey(projectID);
return project;
}
ConsultantEJB getProject() {
if (consultant == null)
consultant = ConsultantEJBHome.findbyPrimaryKey(consultantID);
return consultant;
}
}
For example, I need to query the list of consultants under one project sorted by the consultant name but I don't actually need the details of the consultant. Do I use this Works-OnEJB entity bean for querying the list of consultants or do i directly query the database using JDBC. Cause the application only requires a collection of object identity holder, which contains the name and the id of the consultant. In my opinion, I prefer using the jdbc approach since it's much more efficient. But if I choose to do this, the finder method for the Works_OnEJB will be of no use. The Works_OnEJB will merely be used for adding and deleting association between the consultant and the project. what's your opinion? Is this ok?
Please guide me. I'm really new to EJB.
Thank you very much.
Jerson