作为父母(OneToMany)和孩子(ManyToOne)的Spring Roo自我关系

时间:2014-07-26 04:45:13

标签: spring hibernate jpa spring-roo

我有以下域名,不保存父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

我的工作是:

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.

注意:在流程中生成每个ID。

问题是,每个Item子项的父ID为null,父ID未保存。

这里接收步骤1和处理步骤2,3和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;

日志显示正确:

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]

我按照下面的线索没有成功:

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

1 个答案:

答案 0 :(得分:1)

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

正如您所看到的,在查询中没有任何对 IdItemParen 字段的引用/更新,因此,我有哪些替代方案?如果插入/更新位于“父/子”两侧,则上述代码有什么问题。

伙计们,我认为这是如何工作的,默认情况下Roo添加了(insertable = false,updatable = false)参数,我不得不将它们更改为TRUE,因此该文件可用于插入/更新,现在工作正常。大多数技巧都没有这些参数,所以,这就是那些工作正常而且我很糟糕的原因,因为我从一开始就没有发布。