How to reproduce relationship in Objects?

5

According to the definition of this site :

  

CARDINALITY

     

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

It is often used in a relational database, and it generally uses the PK of a table to define the relationship between tables through its PK (Primary Key) and FK (Foreign Key) keys

in>.

So, I'd 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:

  • Order 1: N OrderItem
  • Product 1: 1 OrderItem

I know that you can use the id's to determine the relationship, but I do not know how to implement it correctly and would like to know if there is another way to do this.

Follow the complete code for 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;
    }

}
    
asked by anonymous 11.03.2016 / 03:17

2 answers

2

You can use class composition with List for multiple values

Example:

  

Order 1: N OrderItem

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 RequestItem

You would then have

public class Produto {
    private int idProduto;
    private String descricao;
    private double valor;
    private PedidoItem pedidoItem;
    
11.03.2016 / 03:24
2

class RequestItem must contain an attribute of type product

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

Order class has an OrderItem list

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

If you want to make a BIDIRECTIONAL relationship

Put an attribute of type OrderItem in the Product

Place an attribute of type Request in RequestItem

    
11.03.2016 / 03:23