简体   繁体   English

休眠-如何保持一对多的“后退”

[英]Hibernate - how to persist a 'backward' one-to-many

I have an interesting situation. 我有一个有趣的情况。 I'm working on a game. 我正在玩游戏。 I have a board with a 2d array of object 'Space'. 我有一个带有2维对象“空间”阵列的电路板。 Each 'Space' has four references to object 'Boundary' (north, east, south, west). 每个“空间”都有对对象“边界”的四个引用(北,东,南,西)。

A space shares a reference to 'Boundary'. 空格共享对“边界”的引用。 For example 'Space' at location (0,0) shares its east boundary with the west boundary of 'Space' at location (1,0). 例如,位置(0,0)的“空间”与位置(1,0)的“空间”的西边界共享其东边界。 This allows me to only update the boundary once when something happens (like from wall to none if the was is destroyed). 这使我仅在发生某些情况时才更新边界(例如,如果墙被破坏,则从墙到无)。

Any idea how to persist something like this using Hibernate? 知道如何使用Hibernate来保持类似的东西吗? I really have a 1-to-many with boundary having many spaces, but I'm not sure how to relate them specifically back to a specific boundary (north,south,west,east). 我确实有一个一对多的边界,边界上有很多空间,但是我不确定如何将它们特别关联回特定的边界(北,南,西,东)。 I also don't know how to set this up so that the two 'Space' object share a reference to the single boundary object. 我也不知道如何进行设置,以使两个“ Space”对象共享对单个边界对象的引用。

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
}

What you describe is a many-to-one relation between Space and each 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM