此父级的外键存储在子表中为零

时间:2016-06-14 18:48:11

标签: java hibernate jpa

当持久化父对象(与子对象具有一对多关系)时,此父对象的外键将作为零存储在子表中。 我的课程:

从xls文件中提取数据

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;
}

Contrato Class

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;

保存方法

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();
    }
}

1 个答案:

答案 0 :(得分:0)

Fisrt all,我忘了在历史悠久的地方设置contrato。

try {
            row = worksheet.getRow(i);
            cell = row.getCell(u.devolveNumColuna("L"));
            contratoKey = u.devolveCampoLido(cell);

            if (mapa.containsKey(contratoKey)) {
                contrato = mapa.get(contratoKey);
            } else {
                contrato = new Contrato();
            }
            contrato.setContrato(contratoKey);
            historico = new Historico();
            historico.setContrato(contrato); // I was forgetting this line

我对我的代码做了一些修改,如下:

public class Contrato {
@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
private long contratoId;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "contrato", orphanRemoval = true)
private List<Historico> historicos= new ArrayList<Historico>();// instance the list

并在Contrato类中编写此方法:

public void addHistorico(Historico historico) {
    historicos.add(historico);
    historico.setContrato(this);
}

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