• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Beans - representing 1-m relationships

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes..think this is the solution. Thanks to both of you for your help!
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic