Search code examples
springhibernatejpaspring-roo

Spring Roo self relationship as parent (OneToMany) and child (ManyToOne)


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


Solution

  • Hibernate: 
    insert 
    into
        dbo.Item
        (CreateTs, CreateUser, Descr, ForAnyCompanyInd, IdIndustry, IdUom, IsClassifiedInd, IsParentInd, LastUpdateTs, LastUpdateUser) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    

    As you can see, in the query there is not any reference to IdItemParen field for insert/update, so, what alternatives do I have? or what's wrong with the code above if the inserts/updates are in both sides "parent/child".

    Folks, I figured it how this works, Roo added (insertable = false, updatable = false) parameters by default, I had to change them to TRUE, so the filed can be available for insert/update and is working fine now. Most of the tips don't have those parameters, so, that is the reason that those worked fine and my bad because I didn't release that from beginning.