I have the following domain, which not saves the parent ID.
privileged aspect Item_Roo_DbManaged {
@OneToMany(mappedBy = "idItemParent")
private Set<Item> Item.items; //item children
@ManyToOne
@JoinColumn(name = "IdItemParent", referencedColumnName = "IdItem", insertable = false, updatable = false)
private Item Item.idItemParent; //item parent
What I do is:
1- To create a List with the Item objects and save each one (just to get the item ID first, for insert/update).
2- Assign the parent ID to each child item. -- I tried saving this first without success
3- Assign to each parent its child list -- I tried saving this first without success
4- Update each item in database. -- I tried this first saving each item without success.
Note: Each ID is being generated n the process.
The problem is, that the parent ID of each Item child is null, the parent ID is not being saved.
Here the process receiving the steps 1 and processing steps 2, 3 and 4:
private List<Item> setItemsParent(List<Item> itemList) {
Map<Long, Item> parents= new HashMap<Long, Item>();
LinkedList<Item> newItemList= new LinkedList<Item>();
Iterator<Item> it= itemList.iterator();
Set<Item> children= null;
while ( it.hasNext() ) {
Item item= it.next();
Long key= item.getHierarchyNbr().longValue() - 1l;
Item parent= parents.get(key);
if (parent != null) {
children= parent.getItems() == null? new LinkedHashSet<Item>() :parent.getItems();
children.add(item);
parent.setItems(children);
itemRepository.saveAndFlush(parent);
log.debug("PARENTi:" + parent.toString());
}
item.setIdItemParent(parent);
itemRepository.saveAndFlush(item);
parents.put(item.getHierarchyNbr(), item);
log.debug("CHILDi:" + item.toString());
newItemList.add(item);
log.debug("NewListi:" + newItemList.toString());
it.remove();
}
return newItemList;
The log displays which is correct:
651 - CHILDi:Item [ItemId:10192; hierarchyNbr:0; Desc:item1; ParentId: null]
664 - PARENTi:Item [ItemId:10192; hierarchyNbr:0; Desc:item1; ParentId: null]
681 - CHILDi:Item [ItemId:10193; hierarchyNbr:1; Desc:item2; ParentId: 10192:item1]
688 - PARENTi:Item [ItemId:10193; hierarchyNbr:1; Desc:item2; ParentId: 10192:item1]
695 - CHILDi:Item [ItemId:10194; hierarchyNbr:2; Desc:item3; ParentId: 10193:item2]
711 - PARENTi:Item [ItemId:10194; hierarchyNbr:2; Desc:item3; ParentId: 10193:item2]
717 - CHILDi:Item [ItemId:10195; hierarchyNbr:3; Desc:item4; ParentId: 10194:item3]
731 - PARENTi:Item [ItemId:10194; hierarchyNbr:2; Desc:item3; ParentId: 10193:item2]
739 - CHILDi:Item [ItemId:10196; hierarchyNbr:3; Desc:item5; ParentId: 10194:item3]
747 - PARENTi:Item [ItemId:10194; hierarchyNbr:2; Desc:item3; ParentId: 10193:item2]
753 - CHILDi:Item [ItemId:10197; hierarchyNbr:3; Desc:item6; ParentId: 10194:item3]
757 - PARENTi:Item [ItemId:10192; hierarchyNbr:0; Desc:item1; ParentId: null]
764 - CHILDi:Item [ItemId:10198; hierarchyNbr:1; Desc:item2.1; ParentId: 10192:item1]
773 - PARENTi:Item [ItemId:10198; hierarchyNbr:1; Desc:item2.1; ParentId: 10192:item1]
777 - CHILDi:Item [ItemId:10199; hierarchyNbr:2; Desc:item2.1.1; ParentId: 10198:item2.1]
785 - PARENTi:Item [ItemId:10198; hierarchyNbr:1; Desc:item2.1; ParentId: 10192:item1]
801 - CHILDi:Item [ItemId:10200; hierarchyNbr:2; Desc:item2.1.2; ParentId: 10198:item2.1]
804 - PARENTi:Item [ItemId:10198; hierarchyNbr:1; Desc:item2.1; ParentId: 10192:item1]
808 - CHILDi:Item [ItemId:10201; hierarchyNbr:2; Desc:item2.1.3; ParentId: 10198:item2.1]
I followed the clues below without success:
Adding a one to many relationship to a self reference parent/child
Hibernate saving self Reference parent/child
JPA: How to have one-to-many relation of the same Entity type