0

我有一个有趣的情况。我正在开发一款游戏。我有一个带有二维对象“空间”数组的板。每个“空间”都有四个对对象“边界”的引用(北、东、南、西)。

空间共享对“边界”的引用。例如,位置 (0,0) 的“空间”与位置 (1,0) 的“空间”的西边界共享其东边界。这使我只能在发生某些事情时更新边界(例如,如果被破坏,则从墙变为无)。

知道如何使用 Hibernate 来坚持这样的事情吗?我真的有一个一对多的边界,有很多空间,但我不确定如何将它们具体关联回特定的边界(北、南、西、东)。我也不知道如何设置它,以便两个“空间”对象共享对单个边界对象的引用。

public class Space {
  private Boundary northBoundry;
  private Boundary eastBoundry;
  private Boundary southBoundry;
  private Boundary westBoundry;

  //hero on the Space
  private HeroBase hero;

  private long     spaceId;

  //getters and setters
}

public class Boundary {

  public static final String NONE        = "none";
  public static final String CLOSED_DOOR = "closed_door";
  public static final String OPEN_DOOR   = "open_door";
  public static final String WALL        = "wall";

  private String             type;
  long                       boundaryId;

  //getters and setters
}
4

1 回答 1

0

您描述的是 Space 和每个 Boundary 之间的多对一关系:

public class Space {
  @ManyToOne
  @JoinColumn(name="NORTH_BOUNDARY")
  private Boundary northBoundary;

  @ManyToOne
  @JoinColumn(name="EAST_BOUNDARY")
  private Boundary eastBoundary;

  @ManyToOne
  @JoinColumn(name="SOUTH_BOUNDARY")
  private Boundary southBoundary;

  @ManyToOne
  @JoinColumn(name="WEST_BOUNDARY")
  private Boundary westBoundary;
}
于 2013-02-28T09:50:36.300 回答