我有两个实体:Contrato
和Historico
,结构如下:
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();
}
}
它正在运行的所有system.print,所以我想我一直在将这些Historico实例添加到Contrato.historicos
答案 0 :(得分:0)
当我从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)