在休眠的一对一关系中从数据库加载时获取空列表



我有两个实体: ContratoHistorico具有以下结构:

public class Contrato {
    @Id @GeneratedValue
    private long contratoId;
    @OneToMany(mappedBy = "contrato", targetEntity = Historico.class,
        fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Historico> historicos;

public class Historico {
    @Id @GeneratedValue
    private long historicoId;
    @ManyToOne
    @JoinColumn(name="contratoId")
    private Contrato contrato;

但是,当我从数据库中加载ContratoEntity时,我将获得具有Historico对象的空列表的正确对象。来自数据库的对象的加载:

private void preencheTabelaDePesquisa(List<Contrato> resultList) {
    DefaultTableModel model = pag1.getModelPage1(); 
    HashMap<Integer, Contrato> mapaLinha = pag1.getMapaLinha();
    limpaTabela(model);
    int i = 0;
    for (Contrato contrato : resultList) {
        List<Historico> historicoList = contrato.getHistoricos();
        for(Historico historico: historicoList){
            model.addRow(new Object[] { "", "", ""});
            model.setValueAt(contrato.getContrato(), i, 0);
            model.setValueAt(contrato.getNome(), i, 1);
            model.setValueAt(contrato.getStatus(), i, 2);
            model.setValueAt(historico.getDataVencimento(), i, 3);
            mapaLinha.put(i, contrato);
            i++;
        }
    }
    pag1.setMapaLinha(mapaLinha);
}

和代码保存数据库的代码部分:

public Map<String, Contrato> extraiDados() throws IOException {
    String path = "C:\Users\tathiana.i.oliveira\Desktop\contratos.xls";
    Map<String, Contrato> mapa = new HashMap<String, Contrato>();
    List<Historico> historicosList;
    Util u = new Util();
    FileInputStream fileInputStream = new FileInputStream(path);
    HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
    HSSFSheet worksheet = workbook.getSheetAt(0);
    HSSFCell cell;
    HSSFRow row;
    int rowsCount = worksheet.getLastRowNum();
    Contrato contrato;
    Historico historico;
    String contratoKey;
    for (int i = 1; i <= rowsCount; i++) {
        try {
            row = worksheet.getRow(i);
            cell = row.getCell(u.devolveNumColuna("L"));
            contratoKey = u.devolveCampoLido(cell);
            if (mapa.containsKey(contratoKey)) {
                contrato = mapa.get(contratoKey);
                historicosList = contrato.getHistoricos();
            } else {
                contrato = new Contrato();
                historicosList = new ArrayList<Historico>();
            }
            contrato.setContrato(contratoKey);
            historico = new Historico();
            cell = row.getCell(u.devolveNumColuna("A"));
            contrato.setClassificacao(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("B"));
            contrato.setResponsavel(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("C"));
            contrato.setUf(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("D"));
            contrato.setSigla(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("E"));
            contrato.setStatusContrato(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("F"));
            contrato.setFornecedor(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("G"));
            contrato.setSite(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("H"));
            contrato.setTelefone(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("I"));
            contrato.setCnpj(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("J"));
            contrato.setCodigoFornecedor(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("K"));
            contrato.setHidrometro(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("M"));
            contrato.setNome(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("N"));
            contrato.setEndereco(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("O"));
            contrato.setDespesa(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("P"));
            contrato.setDescricao(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("Q"));
            contrato.setMp(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("T"));
            contrato.setStatus(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("U"));
            contrato.setEstadoLancamento(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("R"));
            historico.setDataCobranca(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("S"));
            historico.setDataVencimento(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("V"));
            historico.setDataEmissao(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("Y"));
            historico.setDataLancamento(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("Z"));
            historico.setDataCompensacao(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("AA"));
            historico.setDataVencimento(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("AB"));
            historico.setDataAtualizacao(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("W"));
            historico.setDocPgt(u.devolveCampoLido(cell));
            cell = row.getCell(u.devolveNumColuna("X"));
            historico.setDocCompensacao(u.devolveCampoLido(cell));
            historicosList.add(historico);
            contrato.setHistoricos(historicosList);
            mapa.put(contrato.getContrato(), contrato);
        } catch (NumberFormatException e) {
            continue;
        } catch (NullPointerException e) {
            continue;
        }
    }
    return mapa;
}

和实际保存时的方法:

public void salva(Map<String,Contrato> contratoMap) {
    Transaction tx = session.beginTransaction();
    try {
        for (Map.Entry<String, Contrato> entry : contratoMap.entrySet()) {
            Contrato contrato = entry.getValue();
            session.save(contrato);
            System.out.println("contrato salvo");
            List<Historico> historicoList = contrato.getHistoricos();
            for(Historico historico: historicoList){
                session.save(historico);
                System.out.println("historico salvo");
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

所有系统。

调试循环

我忘了设置双向关系的另一面,当我阅读来自Excel的数据时。

try {
        row = worksheet.getRow(i);
        cell = row.getCell(u.devolveNumColuna("L"));
        contratoKey = u.devolveCampoLido(cell);
        if (mapa.containsKey(contratoKey)) {
            contrato = mapa.get(contratoKey);
            historicosList = contrato.getHistoricos();
        } else {
            contrato = new Contrato();
            historicosList = new ArrayList<Historico>();
        }
        contrato.setContrato(contratoKey);
        historico = new Historico(); // here I need to set historico.setContrato(contrato)

最新更新