The diagrams of your BD and your class diagram will look very similar, but be careful not to make confusion when implementing them, each one works in a very different way from the other and the amount of classes and tables are not necessarily the same.
Consider the following example for a supposed class Cliente
:
public class Cliente{
private String nomeCliente;
private String endereco; //supondo que cada cliente só possui um endereco
private List<String> telefones =
new ArrayList<String>(); //0..n telefones
//construtor
//getters and setters
}
First doubt: we have a ArrayList
in class Cliente
, how we keep it in the comic book?
Don’t even think about creating a zillion columns in the table Clientes
in your comic book, like this: telefone1
, telefone2
, telefone3
, ...
, telefone1000
. So you’ve already created some problems such as a lot of columns that most of the time will never be used, a horrible table to read and create a diagram, and a limited amount of your customer’s phones (sure it’s a high limit, but it’s still a limit).
The correct thing is you have two tables with the relationship 1..n
, so you will have a table for customers and one for customers' phones. Doing so will not be unused records causing difficulty in reading them and there will be a limit on the number of phones of each customer. Both are connected by a foreign key in the phone table.
An example of implementing how to click on ArrayList
the phones of each customer would be so:
getClient(String client name)
prestat = conn.prepareStatement("SELECT * FROM Clientes WHERE nomeCliente=?");
prestat.setString(1, nomeCliente);
pw = prestat.executeQuery();
if (pw.next()) {
//criando cliente
cliente = new Cliente(pw.getString(2), pw.getString(3));
// incluindo telefone do cliente
cliente.setTelefones(getTelefonesCliente(pw.getInt(1)));
}
getTelefonesClient(int idClient)
List<TelefoneCliente> telefonesCliente = new ArrayList<TelefoneCliente>();
//..
prestat = conn.prepareStatement("SELECT * FROM TelefonesCliente WHERE idCliente=?");
prestat.setInt(1, idCliente);
pw = prestat.executeQuery();
while (pw.next()) {
telefonesCliente.add(pw.getString(3)));
}
//..
return telefonesCliente;
And the problem is solved :)
This solution even makes it easy to expand your program. For example, suppose that once implemented arises the need for you to need to save the customer’s phone carrier.
You have two options:
- Can create one more
ArrayList
in class Cliente
, however in my opinion it would be a little strange, and might even hurt the principle of sole responsibility; or
- You can do what I think is most natural, create a new class:
Note that you will now have two classes and two tables, but the procedure of loading the data into the objects of these classes is very similar to the one mentioned above. This one stays as homework ;-)
Regarding the components, I always create them at runtime, there is a connection class that does all this... Now to update the data just bring the database again...
– Roberto de Campos
Your question is how to insert and remove data in a BD, how to use a particular Delphi component, or how to make relationships between objects and save them in a BD so that it is possible to restore them properly? It wasn’t clear to me.
– Math
@Math Actually my question is in general about how I can work object-oriented in database applications. In short, as you said "How to make relationships between objects and save them in a BD so that it is possible to restore them properly?".
– adamasan
That’s a very interesting question. I asked myself same question in SO.com a while ago and I didn’t get a decent answer. Is that your question? If yes, take a look at the comment that says "impedance object-relational". I’m on working hours, I’ll try to elaborate later.
– Math