How to play relationship in Objects?

Asked

Viewed 396 times

5

According to the definition of this website:

CARDINALITY

Is the maximum and minimum number of occurrences of an entity that are associated with the occurrences of another participating entity relationship. That is, the cardinality is important to help the define the relationship as it defines the number of occurrences in a relationship.

It is widely used in relational databases, and generally uses the identifier (PK) of a table to define the relationship between tables through their keys PK (Primary Key) and FK (Foreign Key).

Therefore, I would like to know how I could reproduce the relationship in Objects that represent my tables programmatically using object orientation?

For this I created the following example to illustrate the situation:

Class Produto containing the following attributes:

private int idProduto;
private String descricao;
private double valor;

Class Pedido containing the following attributes:

private int idPedido;
private double valorTotal;

Class PedidoItem containing the following attributes:

private int idPedidoItem;
private int quantidade;

Their respective cardinalities are:

  • Request 1:N Pedidoitem
  • Product 1:1 Pedidoitem

I know you can use the id’s to determine the relationship, but I don’t know how to implement it in the right way and I wonder if there is another way to do it.

Follows the complete code of the three classes.

Class Produto:

package cardinalidadeemobjeto;

public class Produto {
    private int idProduto;
    private String descricao;
    private double valor;

    public Produto(){}

    public int getIdProduto() {
        return idProduto;
    }

    public void setIdProduto(int idProduto) {
        this.idProduto = idProduto;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public double getValor() {
        return valor;
    }

    public void setValor(double valor) {
        this.valor = valor;
    }
}

Class Pedido:

package cardinalidadeemobjeto;

public class Pedido {
    private int idPedido;
    private double valorTotal;

    public Pedido(){}

    public int getIdPedido() {
        return idPedido;
    }

    public void setIdPedido(int idPedido) {
        this.idPedido = idPedido;
    }

    public double getValorTotal() {
        return valorTotal;
    }

    public void setValorTotal(double valorTotal) {
        this.valorTotal = valorTotal;
    }        
}

Class PedidoItem:

package cardinalidadeemobjeto;

public class PedidoItem {
    private int idPedidoItem;
    private int quantidade;

    public PedidoItem(){}

    public int getIdPedidoItem() {
        return idPedidoItem;
    }

    public void setIdPedidoItem(int idPedidoItem) {
        this.idPedidoItem = idPedidoItem;
    }

    public int getQuantidade() {
        return quantidade;
    }

    public void setQuantidade(int quantidade) {
        this.quantidade = quantidade;
    }

}
  • A doubt. You will not use JPA?

  • I’m not using any API.

2 answers

2

Pedidoitem class must contain an attribute of the product type

public class PedidoItem{
    ...
    private Produto produto;
    ...

Request class has a list of Pedidoitem

public class Pedido{
   ...
   private List<PedidoItem> pedidoItems;
   ...

If you want to make a BIDIRECTIONAL relationship

Place a Pedidoitem attribute in the Product

Place a Request attribute in Pedidoitem

2


You can use class composition with List for multiple values

Example:

Request 1:N Pedidoitem

In your Request class, you would have:

public class Pedido {
    private int idPedido;
    private double valorTotal;
    private List<PedidoItem> pedidoItem = new ArrayList<PedidoItem>();

To

Product 1:1 Pedidoitem

You would then have

public class Produto {
    private int idProduto;
    private String descricao;
    private double valor;
    private PedidoItem pedidoItem;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.