Discussions

EJB programming & troubleshooting: How do you map one entity bean to multiple tables?

  1. I was curious to how you map an entity bean to multiple tables in the database. My first idea was to simply create finder methods that map SQL in the deployment descriptors that query these mutliple tables however in the description I see a tag defining 'table'.

    Is this is a good way to go about this? I need to implement entity beans that access information from multiple (existing) tables.

    Any ideas would be great. I cant find examples of these in any of my books and have done some searches on the web.

    Thanks
    Ryan

  2. You could 'map' to a table that is actually a view, with the view hiding all the detail - this will have cascading issues when/if you do updates through it.

    You could always have fine-grain entities that are 'mapped' by application logic.

    tom
  3. Yes, entity beans may map to views. However, I suggest that these beans be read-only beans, since how to update and delete a "row" in a view is not defined.

  4. I presume you are looking for a way to do this in CMP. In BMP it should be relatively straight foward - on load you should be able to access both tables via some form of join and on create , remove and store you just need to perform more that one SQL query.

    Stuart
  5. I'm not entirely sure what you are asking. But, I would like to believe that with both CMP and BMP this should be straightforward. I would like to make a (reasonable) assumption that you have a 'domain' object per table.

    So then your EB is coarse grained and made up of more than one 'domain' object. You don't really need a domain object representation, just a logical grouping of data in your EB. Also you may or maynot have 'dependent relationships', i.e. your EB could be Employee which maps to EMPLOYEE and ADDRESS tables.

    With BMP, as someone pointed it out, this should be a matter of coding ejbLoad(), ejbStore(). With CMP, I would like to believe your container lets you map a particular EB field to any database TABLE as long as you can join them together.

    Apologies if I misunderstood the question. Your query about writing finder methods threw me off a little. If all you are looking to do is to return a collection of data that is obtained thru joins on multiple tables, perhaps you don't need EB at all, rather just a session bean method or a 'Fast Lane Reader'

    Regards
  6. Thanks for all the great information. Sorry if my question was abstract. I was confused as to how to implement EB's using multiple tables. I didnt know if I was supposed to create an entity bean for every table and then use a session bean to group related joins, or if I could (and it was a good design) to create a entity bean 'object' that basically consisted of data from several tables in it's own right. For example lets assume I have the following tables:

    EMPLOYEE (empid,empname)
    OFFICES (officeid, address, bldg, empidfk)

    Lets assume I want to have an employee bean that contains everything about him/her including the working address. I would then need to query both tables.

    Scenario 1: Create a CMP table on each table and use a session bean to join them (???)

    Scenario 2: Create a bean (entity or session?) that contains info from both tables to logically create one 'entity'.

    Thanks
    Ryan
  7. hi Ryan,

    I would suggest u to visit www.objectfrontier.com. There u can find a tool named FrontierSuite which helps u in mapping an entity bean to more than one table. You can download the trial and can work on it. This provides u to model the Classes based on UML1.3 fusing to database u prefer and can be deployed in any application servers supported by that tool.