Hi,
I have a question about a programming problem that I encounter for the first time; I think it comes up only with user/database interaction. I have a solution that will "work", but it's so ugly! If someone please could tell me how to do this properly...
I read an existing database using JDBC. The tables in the database correspond exactly to the objects I'd like to have in Java, and so to create an object, I send the primary key to the object constructor, which makes the sql call and initializes all the private members.
This works perfectly except for one object. I'll use the time-honored Vehicle analogy for simplicity.
In the database I have one Vehicle table with the common elements, one element containing the type of vehicle, and a table for each type type of vehicle with the elements specific to each type. The tables all share the same primary key, so for each key there is one record in the Vehicle table and one record in only one of the sub-tables.
In Java, I would like a Vehicle class extended by a subclass for each vehicle. My problem is, how to read in the elements?
When I call the Vehicle constructor with the primary key, I'll have no idea which specific kind of Vehicle should be created, and as far as I can see there's no way of a constructor deciding that the new object should actually be on instance of a subclass?
Next thought is using an external method that makes a db call, determines the type of vehicle, and creates an object of appropriate type that then makes a second identical db call . . . ugly!
So I thought of a Vehicle class containing a VehicleSpecific object, so that the Vehicle constructor decides on the kind of VehicleSpecific object and creates a instance of the class of that name (reflection is cool). But I won't be able to access the common Vehicle variables from the VehicleSpecific methods without some kind of pointer toward the including object, right? Seems dirty and I don't know how.
Next step is googling and FAQ searching, but I can't find anything. Next step is asking for help...
I have a question about a programming problem that I encounter for the first time; I think it comes up only with user/database interaction. I have a solution that will "work", but it's so ugly! If someone please could tell me how to do this properly...
I read an existing database using JDBC. The tables in the database correspond exactly to the objects I'd like to have in Java, and so to create an object, I send the primary key to the object constructor, which makes the sql call and initializes all the private members.
This works perfectly except for one object. I'll use the time-honored Vehicle analogy for simplicity.
In the database I have one Vehicle table with the common elements, one element containing the type of vehicle, and a table for each type type of vehicle with the elements specific to each type. The tables all share the same primary key, so for each key there is one record in the Vehicle table and one record in only one of the sub-tables.
In Java, I would like a Vehicle class extended by a subclass for each vehicle. My problem is, how to read in the elements?
When I call the Vehicle constructor with the primary key, I'll have no idea which specific kind of Vehicle should be created, and as far as I can see there's no way of a constructor deciding that the new object should actually be on instance of a subclass?
Next thought is using an external method that makes a db call, determines the type of vehicle, and creates an object of appropriate type that then makes a second identical db call . . . ugly!
So I thought of a Vehicle class containing a VehicleSpecific object, so that the Vehicle constructor decides on the kind of VehicleSpecific object and creates a instance of the class of that name (reflection is cool). But I won't be able to access the common Vehicle variables from the VehicleSpecific methods without some kind of pointer toward the including object, right? Seems dirty and I don't know how.
Next step is googling and FAQ searching, but I can't find anything. Next step is asking for help...