@Transactional public void updateStaticTree() { List<TreePoisonDTO> treePoisons = new ArrayList<TreePoisonDTO>(); List<TreePoisonDTO> poisonTrees = new ArrayList<TreePoisonDTO>(); try { treePoisons = treeService.get(); poisonTrees = poisonService.getTreePoisons(); } catch (NoSuchObjectException e) { e.printStackTrace(); } // Обновляем связи объекта PoisonDTO TreePoisonDTO poisonTreeDTO1 = poisonTrees.get(0); poisonTreeDTO1.setTreeDTO(treePoisons.get(0).getTreeDTO()); poisonTreeDTO1.setPoisonDTO(poisonTrees.get(0).getPoisonDTO()); List<TreePoisonDTO> treePoisonDTOs1 = new ArrayList<TreePoisonDTO>(); treePoisonDTOs1.add(poisonTreeDTO1); TreePoisonDTO poisonTreeDTO2 = poisonTrees.get(1); poisonTreeDTO2.setTreeDTO(treePoisons.get(1).getTreeDTO()); poisonTreeDTO2.setPoisonDTO(poisonTrees.get(1).getPoisonDTO()); List<TreePoisonDTO> treePoisonDTOs2 = new ArrayList<TreePoisonDTO>(); treePoisonDTOs2.add(poisonTreeDTO2); // Обновляем связи объекта TreeDTO TreePoisonDTO treePoisonDTO1 = treePoisons.get(0); treePoisonDTO1.setPoisonDTO(poisonTreeDTO1.getPoisonDTO()); treePoisons.set(0, treePoisonDTO1); TreePoisonDTO treePoisonDTO2 = treePoisons.get(1); treePoisonDTO2.setPoisonDTO(poisonTreeDTO2.getPoisonDTO()); treePoisons.set(1, treePoisonDTO2); try { List<PoisonPestDTO> poisonPestDTOs = new ArrayList<PoisonPestDTO>(); poisonService.update(treePoisonDTOs1, poisonPestDTOs); poisonService.update(treePoisonDTOs2, poisonPestDTOs); } catch (NoSuchObjectException e) { e.printStackTrace(); } try { List<TreePoisonDTO> treePoisons1 = new ArrayList<TreePoisonDTO>(); treePoisons1.add(treePoisons.get(0)); treeService.update(treePoisons1); List<TreePoisonDTO> treePoisons2 = new ArrayList<TreePoisonDTO>(); treePoisons2.add(treePoisons.get(1)); treeService.update(treePoisons2); } catch (NoSuchObjectException e) { e.printStackTrace(); } }
@Transactional public void addStaticTree() throws ObjectAlreadyExistsException { TreePoisonDTO treePoisonDTO1 = new TreePoisonDTO(); TreeDTO treeDTO1 = new TreeDTO(); treeDTO1.setName("Береза"); treeDTO1.setHeight("30-45м"); treeDTO1.setDescribe("111"); TreePoisonDTO treePoisonDTO2 = new TreePoisonDTO(); TreeDTO treeDTO2 = new TreeDTO(); treeDTO2.setName("Ясень"); treeDTO2.setHeight("25-35м"); treeDTO2.setDescribe("222"); CareDTO careDTO1 = new CareDTO(); careDTO1.setDescribe("kill"); treeDTO1.setCareDTO(careDTO1); CareDTO careDTO2 = new CareDTO(); careDTO2.setDescribe("bill"); treeDTO2.setCareDTO(careDTO2); treePoisonDTO1.setTreeDTO(treeDTO1); treePoisonDTO2.setTreeDTO(treeDTO2); // Сохраняем объект Tree и возвращаем полученный от Hibernate id // Добавляем этот id в объект TreeDTO try { List<TreePoisonDTO> treePoisonDTOs1 = new ArrayList<TreePoisonDTO>(); treePoisonDTOs1.add(treePoisonDTO1); treeService.add(treePoisonDTOs1); List<TreePoisonDTO> treePoisonDTOs2 = new ArrayList<TreePoisonDTO>(); treePoisonDTOs2.add(treePoisonDTO2); treeService.add(treePoisonDTOs2); } catch (ObjectAlreadyExistsException e) { e.printStackTrace(); } }