Best way to save a ordered List to the Database while keeping the ordering

前端 未结 12 1109
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 17:27

I was wondering if anyone has a good solution to a problem I\'ve encountered numerous times during the last years.

I have a shopping cart and my customer explicitly

12条回答
  •  感动是毒
    2020-11-30 18:02

    When I use Hibernate, and need to save the order of a @OneToMany, I use a Map and not a List.

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "rule", cascade = CascadeType.ALL)
    @MapKey(name = "position")
    @OrderBy("position")
    private Map    actions             = LazyMap.decorate(new LinkedHashMap<>(), FactoryUtils.instantiateFactory(RuleAction.class, new Class[] { Rule.class }, new Object[] { this }));
    

    In this Java example, position is an Integer property of RuleAction so the order is persisted that way. I guess in C# this would look rather similar.

提交回复
热议问题