Class X Diagram Entity-Relationship Diagram

Asked

Viewed 960 times

1

I’m stuck in the POO X Relational Database. To better substantiate my inquiries follows, in first some (simplified) modeling I did:

1 - Class diagram

inserir a descrição da imagem aqui

Possible programming for this diagram:

1.1 - Item class

public class Item{

    private int codigoItem;
    private String descricaoItem;

    public void setCodigoItem(int codigoItem){
        this.codigoItem = codigoItem;
    }

    public int getCodigoItem(){
        return codigoItem;
    }

    public void setDescricaoitem(String descricaoItem){
        this.descricaoItem = descricaoItem;
    }

    public String getCodigoItem(){
        return codigoItem;
    }
}

1.2 Class Request

public class Pedido{

    private int codigoPedido;
    private List<Item> itens = new ArrayList<>();
    private float quantidade;
    private BigDecimal valorPedido;

    public void setCodigoPedido(int codigoPedido){
        this.codigoPedido = codigoPedido;
    }

    public int getCodigoPedido(){
        return codigoPedido;
    }

    public void setItens(Item itens){
        this.itens.add(itens);
    }

    public List<Item> getItens(){
        return itens;
    }
}

1.3 - Romanticism Class

public class Romaneio implements Requisicao{
    private int codigoRomaneio;
    private List<Pedido> pedidos = new ArrayList<>();
    private BigDecimal valorRomaneio = BigDecimal.ZERO;

    public void setCodigoRomaneio(int codigoRomaneio){
        this.codigoRomaneio = codigoRomaneio;
    }

    public int getCodigoRomaneio(){
        return codigoRomaneio;
    }

    @Override
    public void criarPedido(Pedido pedido){
        this.pedidos.add(pedido);
    }

    public List<Pedido> getPedidos(){
        return pedidos;
    }

    public void setValorRomaneio(BigDecimal valorRomaneio){
        this.valorRomaneio += valorRomaneio;
    }

    public BigDecimal getValorRomaneio(){
        return codigoRomaneio;
    }
}

1.4 Inteface Requisicao

public interface Requisicao{
    public void criarPedido(Pedido pedido);
}

The problem arises (assuming the encoding is correct) when the database will be modeled:

2 - Conceptual Model

inserir a descrição da imagem aqui

3 - Logical Model

inserir a descrição da imagem aqui

On the one hand, in the class diagram, it should be indicated in the objects Pedido a list of objects Item and a list of objects Pedido in class Romaneio. On the other hand, by conceptual and logical modeling, more specifically in the case of the Romanticism-Request relationship, is the table Pedido (which part of the class diagram) receives information from Romaneio (FK). So I’m having trouble coding that. I have to pass the romantics to the requests (database), but the Requests class has no field of romanticism type, but just the opposite, since Romaneio has a wish list. Has anyone ever come across this question? How to resolve it(ram)?

4 - View (returns and rescue methods)

private Item retornarItem(){
    bean.Item item = new bean.Item();

    item.setCodigoItem(codigoItem);
    item.setDescricaoItem(txtDescricao.getText());

    return item;
}

private Pedido retornarPedido(){
    bean.Pedido pedido = new bean.Pedido();

    pedido.setCodigoPedido(codigoPedido);
    pedido.setItem(retornarItem());
    pedido.setQuantidade(Float.parseFloat(txtQuantidade.getText()));
    pedido.setValorPedido(new BigDecimal(txtValorTotal.getText()));
    pedido.setRomaneio(?) // e aqui?
}

private Romaneio retornarRomaneio(){
    bean.Romaneio romaneio = new bean.Romaneio();

    romaneio.setCodigoRomaneio(codigoRomaneio);
    romaneio.setPedido(retonarPedido()); //???
    romaneio.setValorRomaneio(valorPedidoAtual());
}

private boolean salvarPedido(){
        try{
            codigoPedido = dao.Pedido.inserir(retornarPedido());
            return dao.Transacao.completar();

        }catch(SQLException sqle){
            System.out.println("Impossível salvar o pedido. ERRO: " 
                    + sqle.getMessage());

            return false;
        }
}

private boolean salvarRomaneio(){
        try{
            codigoRomaneio = dao.Romaneio.inserir(retornarRomaneio());
            return dao.Transacao.completar();

        }catch(SQLException sqle){
            System.out.println("Impossível salvar o romaneio. ERRO: " 
                    + sqle.getMessage());

            return false;
        }
}

5 - DAO (skeleton)

public class Item {
    public static int[] inserir(bean.Item item) throws SQLException{

    }
}

public class Pedido {
    public static int[] inserir(bean.Pedido pedido) throws SQLException{

    }
}

public class Romaneio {
    public static int inserir(bean.Romaneio Romaneio) throws SQLException{

    }
}

Thanks in advance.

2 answers

1

Dude, your idea is right, the DB will start from Romanticism and then go to Orders, the Orders just have to have this FK to be able to locate and tell who he is "son", but not that his class has to have this information...

As mentioned in the comment below, you use a DAO for CRUD, the easiest way (in my view) is to create a function in the Romaneiodao class that inserts in DB all relevant information to the same, this will make the function very big I know, but avoids closing the connection and reopening the same one-and-a-half thousand times. Here’s an example of how it could be:

//la na classe RomaneioDAO
public boolean inserirRomaneioCompleto(Romaneio romaneio){
    sql = "INSERT INTO romaneio (id...) VALUES (...);
    try{
        stmt = con.prepareStatement(sql);
        stmt.... todos os inserts;
        stmt.execute();
        sql = "SELECT LAST_INSERT_ID() INTO @id"; //esse comando vai pegar o id inserido no romaneio
        stmt = con.prepareStatement(sql);
        stmt.execute();
        //no sql abaixo passa o id que voce pegou ali em cima, que é o FK
        //é só no local onde vai ir o FK você colocar o @id
        sql =  "INSERT INTO pedido (id,...,cod_romaneio) VALUES (....,@id)"
        stmt = con.prepareStatement(sql);
        stmt.execute();
        //e assim sucessivamente ate completar seu insert total..
        //desse modo você não precisa fechar a conexão mil vezes...
    }finally{
    //aqui você fecha a conexão...
    }
}
  • But how do I pass the Romanticism to the save method? In my case I use a method for this and this method returns One Item to save Item, One Request to save a Request and one Romanticism to save a Romanticism. In this case, the Request class should have a Romanticism attribute, since the bank 'asks' the romanticism as FK. How I would bring this information?

  • What kind of database are you using? Use DAO methods?

  • Yes, one DAO for each class.

  • I added more information.

1

Silva, the issue raised depends on your data persistence strategy.

If you are persisting your data in the "nail" using the classes of namespace java sql. and manually writing the commands DML to manipulate this information in the database, you do not necessarily need to have the attribute that represents the FK table Romance in his class of Request. You can for example at a time in your code have a method salvage that receives an instance of Romance with the orders already added to it, place a foreach for each application in that Romance with that you could ride your "Insert" concatenating the information needed to save the request and would have the ID of Romance.

In case you’re wearing a ORM framework for data persistence such as Hibernate, itself already obliges you to use a different strategy, its Request class would have the attribute representing the FK table Romance, yeah, the Framework needs this representation between OBJECT X DATABASE then, what normally occurs each of your class will have an attribute that represents each column of the table that it is associated with. In the case of the class Request, would have the attribute representing the FK, because it’s a column in the table.

  • It’s "nail" @Bruno They’re using DAO.

  • I added more information.

Browser other questions tagged

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