aspose file tools*
The moose likes JDBC and the fly likes Beans - representing 1-m relationships Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Beans - representing 1-m relationships" Watch "Beans - representing 1-m relationships" New topic
Author

Beans - representing 1-m relationships

Ed Lance
Ranch Hand

Joined: Aug 02, 2000
Posts: 38
We have recently started using java beans and I'm curious about the best way to retrieve and view data from queries involving 1-m realationships. On the veiw (jsp) I only want to display the data from the "1" record once, then loop through the data from the "many" (a basic control-break senario).
I am doing it by loading up a separate vector each time the key in the "1" record changes. In my view, I then use an enumeration to loop through each vector, instantiate the bean and call the properties I need at that point (Data is for display only, so not using a control servlet). Just curious if anyone has a better way.
Thanks!
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
If I understand your problem, here's how I would do it. I'll use an example that might be found in an airline scheduling application. Let's say your '1' object is a 'Flight'. A Flight can contain many 'Passengers'.

I know that isn't technically a bean because I didn't supply a default contsructor and all, but I did that just for brevity. You would have to have a setMethod for flightNo and manually call loadPassengers().
So whereever you make the call to the database to get your list of flights, you would instantiate a new Flight() for each flightNo you got from the database, and then have each Flight object load its passengers. You could then display each flight number followed by a list of the passengers by calling getPassengers().
I would probably do a separate bean (lets call it FlightList) whose function was to make the JDBC call to get the list of flights, instantiate the Flight objects and add them to a Vector, and then have methods for iterating through the vector and returning the proper information, to be called by the JSP. This would keep as much code as possible out of your view. Your view would then only have call FlightList.getFlightTable() for example.
Does this make sense?
Jason Menard
Jason Kilgrow
Ranch Hand

Joined: May 21, 2001
Posts: 47
Hmmm...if you had a vector of BeanOne that stored the "one" record and a vector of BeanMany. BeanMany stored a vector or an array, etc. of your "many" records. Then, you could get the "one" record from BeanOne and a bean that had all of your "many" records. When you were done getting the "many" records from BeanMany, you just get the next BeanOne with the next "one" record and the next BeanMany bean.
Does that make sense?
===============================================================
Originally posted by Ed Lance:
We have recently started using java beans and I'm curious about the best way to retrieve and view data from queries involving 1-m realationships. On the veiw (jsp) I only want to display the data from the "1" record once, then loop through the data from the "many" (a basic control-break senario).
I am doing it by loading up a separate vector each time the key in the "1" record changes. In my view, I then use an enumeration to loop through each vector, instantiate the bean and call the properties I need at that point (Data is for display only, so not using a control servlet). Just curious if anyone has a better way.
Thanks!

Jason Kilgrow
Ranch Hand

Joined: May 21, 2001
Posts: 47
This is good.
Then, in your jsp (or whatever) you could have a vector of Flight objects. Cool.
================================================================
Originally posted by Jason Menard:
[B]If I understand your problem, here's how I would do it. I'll use an example that might be found in an airline scheduling application. Let's say your '1' object is a 'Flight'. A Flight can contain many 'Passengers'.

I know that isn't technically a bean because I didn't supply a default contsructor and all, but I did that just for brevity. You would have to have a setMethod for flightNo and manually call loadPassengers().
So whereever you make the call to the database to get your list of flights, you would instantiate a new Flight() for each flightNo you got from the database, and then have each Flight object load its passengers. You could then display each flight number followed by a list of the passengers by calling getPassengers().
I would probably do a separate bean (lets call it FlightList) whose function was to make the JDBC call to get the list of flights, instantiate the Flight objects and add them to a Vector, and then have methods for iterating through the vector and returning the proper information, to be called by the JSP. This would keep as much code as possible out of your view. Your view would then only have call FlightList.getFlightTable() for example.
Does this make sense?
Jason Menard[/B]

Ed Lance
Ranch Hand

Joined: Aug 02, 2000
Posts: 38
Yes..think this is the solution. Thanks to both of you for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beans - representing 1-m relationships