【问题标题】:Spring Roo self relationship as parent (OneToMany) and child (ManyToOne)Spring Roo 作为父级 (OneToMany) 和子级 (ManyToOne) 的自我关系
【发布时间】:2014-07-26 04:45:12
【问题描述】:

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

【问题讨论】:

    标签: spring hibernate jpa spring-roo


    【解决方案1】:
    Hibernate: 
    insert 
    into
        dbo.Item
        (CreateTs, CreateUser, Descr, ForAnyCompanyInd, IdIndustry, IdUom, IsClassifiedInd, IsParentInd, LastUpdateTs, LastUpdateUser) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    

    如您所见,在查询中没有任何对 IdItemParen 字段的引用以进行插入/更新,那么,我有什么替代方案?或者如果插入/更新都在“父/子”两侧,那么上面的代码有什么问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      相关资源
      最近更新 更多