Hi,
I'm trying to figure out how to model my javabeans. Let's say I have the
following tables:
Employees
Employees_Address
Employees_Projects
Address
Projects
So my main table is Employees and the two tables Address and Projects are
1:N relationship tables, with the remaining two tables being the linking
tables. How would I model my beans? Would I just have one really big bean
called EmployeesBean which would have the requisite getAddress() and
getProjects() methods? Or should I have 5 beans?
Right now I am using one bean for all the tables, so, given that assumption:
Right now I have two methods for data access in my bean, commit(id) and
population(id). Basically what I do to edit a bean is:
1. fill up a jsp with the beans properties (i.e. call populate() )
2. let the user change whatever he wants and then submit the jsp page
3. the jsp page updates the bean with the new properties
4. the bean the gets updates the database (ie call commit() )
The problem I am having is that I have some JSP pages where I only change 1
or 2 of the bean's properties, yet I call "populate()" before, which calls
the database and populates all the fields... This means linking 5 tables
together... Then once I call commit() it updates ALL the fields... Seems
like a lot of wasting .... Am I doing this correctly?
thanx,
-Tom
John C. Bollinger - 24 Jul 2003 15:27 GMT
> Hi,
>
[quoted text clipped - 28 lines]
> together... Then once I call commit() it updates ALL the fields... Seems
> like a lot of wasting .... Am I doing this correctly?
You could probably implement it more efficiently, although that issue is
really orthogonal to how your beans map to tables. It does seem like
you might benefit from a more loosely coupled model. Here's a suggestion:
(a) You should have one bean each for the Employees, Address, and
Projects tables. These beans should each manage data for one row of the
target table, having properties for the relevant columns, and the
ability to load, update, and insert rows. It is probably useful for
each to maintain a sense of whether its data has been modified since
loading so as to judge whether it is necessary to update the DB with its
current data.
(b) You should have another bean that manages a complete set of employee
information -- an Employee bean and the associated Address and Projects
beans. The linking tables don't need to be explicitly modeled because
the relationships they describe are implicit in this bean's structure.
Where you need more than one of the tables for a particular JSP, this
bean would manage loading related beans and presenting them to the JSP,
then storing the data when done. It would use the other three beans to
do the dirty work.
Your JSPs could then access and manipulate individual employee, address,
or project info independantly if they need to do, or can get a broader
view if they so desire. You maintain per-row information regarding
whether data needs to be updated, saving your database considerable
work. The logic for managing the bean relationships remains abstracted
into a bean as it is now, relieving the JSPs from having to deal with
it. (A Good Thing)
John Bollinger
jobollin@indiana.edu