开发者

Can Hibernate automatically manage the order of child collections?

Given a Parent class which has many Children, can Hibernate automatically manage the order of said children? From reading documentation and searching, it seems that the @OrderColumn annotation may enable this, but I've not found any examples of how to do it. The closest thing I've found is JPA 2.0 @OrderColumn annotation in Hibernate 3.5, which looks a bit discouraging, given that it looks just like what I want to do.

Here is a rough sketch of what I'm trying to do, minus the annotations, since I'm not sure what they would be:

class Parent {
  // I probably need some sort of @OrderColumn annotation here, right?
  private List<Child> children = new ArrayList<Child>;
}

class Child {
  private Parent parent;
  private int order;
}

class SomeBusinessLogic {
  public static void createFamily() {
    Parent dad = new Parent("Tom");
    List<Children> children = dad.getChildren();
    children.add(new Child("Alice");
    children.add(new Child("Bob");
    children.add(new Child("Carl");
    session.save(dad);
  }

  public static void addChild(Parent parent) {  // assuming Tom is passed in
    List<Children> children = parent.getChildren();
    children.add(1, new Child("Zelda");
    session.save(parent);
    // now, the order should be: Alice, Zelda, Bob, Carl
  }
}

If someone can give me enough details to make this toy example work (or 开发者_StackOverflow中文版tell me that it will not work), I would be most appreciative.


In order to store children in its order you should map it with @IndexedColumn annotation it will create indexed column on your child objects table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜