我正在构建一个平铺游戏。在我最初的“游戏”中,我的主板有一个
Tile tiles[][];
现在我想用JPA继续这个,因为据我所知你不能坚持使用多维数组。
所以我正在寻找改变它。我可以为要获得的位置创建一个点类
map<Point,Tile>
我相信我会得到类似的东西:
@Entity
public class Board{
@Id
private long id;
...
@OneToMany(mappedBy="board")
@MapKeyJoinColumn(name="POINT_ID")
private Map<Point, Tile> tiles;
...
}
@Entity
public class Tile{
@Id
private long id;
...
@ManyToOne
private Board board;
...
}
@Entity
public class Point{
@Id
private long id;
...
@Column(name = "ROW")
private int row;
@Column(name = "COL")
private int col;
...
}
或者我可以使用
map<Integer, Map<Integer, Tile>>
(不知道如何在JPA中这样做)
我是否在正确的轨道上,最好的方式是什么(表演等......)还是我过度思考这个?
所有。
答案 0 :(得分:0)
通过hibernate-types项目,您可以为JPA和Hibernate实体属性保留多维数组。
因此,如果您需要保留Tile[][]
数组,则可以轻松地做到这一点,而不必将其转换为Map
。
现在,我有一个非常相似的示例,其中包含用于飞机座位预订系统的二维数组。
因此,假设您具有以下plane
数据库表:
CREATE TABLE plane (
id INT8 NOT NULL,
name VARCHAR(255),
seat_grid seat_status[][],
PRIMARY KEY (id)
)
其中seat_status
是PostgreSQL枚举:
CREATE TYPE seat_status
AS ENUM (
'UNRESERVED',
'RESERVED',
'BLOCKED'
);
您可以如下映射plane
数据库表:
@Entity(name = "Plane")
@Table(name = "plane")
@TypeDef(
name = "seat_status_array",
typeClass = EnumArrayType.class
)
public static class Plane {
@Id
private Long id;
private String name;
@Type(
type = "seat_status_array",
parameters = @org.hibernate.annotations.Parameter(
name = "sql_array_type",
value = "seat_status"
)
)
@Column(
name = "seat_grid",
columnDefinition = "seat_status[][]"
)
private SeatStatus[][] seatGrid;
//Getters and setters omitted for brevity
public SeatStatus getSeatStatus(int row, char letter) {
return seatGrid[row - 1][letter - 65];
}
}
因此,您需要声明要使用的适当的休眠类型。对于枚举,您需要使用EnumArrayType
:
@TypeDef(
name = "seat_status_array",
typeClass = EnumArrayType.class
)
@Type
批注允许您将参数传递给Hibernate Type,例如SQL数组类:
@Type(
type = "seat_status_array",
parameters = @org.hibernate.annotations.Parameter(
name = "sql_array_type",
value = "seat_status"
)
)
现在,当您保留以下Post
实体时:
entityManager.persist(
new Plane()
.setId(1L)
.setName("ATR-42")
.setSeatGrid(
new SeatStatus[][] {
{
SeatStatus.BLOCKED, SeatStatus.BLOCKED,
SeatStatus.BLOCKED, SeatStatus.BLOCKED
},
{
SeatStatus.UNRESERVED, SeatStatus.UNRESERVED,
SeatStatus.RESERVED, SeatStatus.UNRESERVED
},
{
SeatStatus.RESERVED, SeatStatus.RESERVED,
SeatStatus.RESERVED, SeatStatus.RESERVED
}
}
)
);
Hibernate将发出正确的SQL INSERT语句:
INSERT INTO plane (
name,
seat_grid,
id
)
VALUES (
'ATR-42',
{
{"BLOCKED", "BLOCKED", "BLOCKED", "BLOCKED"},
{"UNRESERVED", "UNRESERVED", "RESERVED", "UNRESERVED"},
{"RESERVED", "RESERVED", "RESERVED", "RESERVED"}
},
1
)
而且,在获取实体时,一切都会按预期进行:
Plane plane = entityManager.find(Plane.class, 1L);
assertEquals("ATR-42", plane.getName());
assertEquals(SeatStatus.BLOCKED, plane.getSeatStatus(1, 'A'));
assertEquals(SeatStatus.BLOCKED, plane.getSeatStatus(1, 'B'));
assertEquals(SeatStatus.BLOCKED, plane.getSeatStatus(1, 'C'));
assertEquals(SeatStatus.BLOCKED, plane.getSeatStatus(1, 'D'));
assertEquals(SeatStatus.UNRESERVED, plane.getSeatStatus(2, 'A'));
assertEquals(SeatStatus.UNRESERVED, plane.getSeatStatus(2, 'B'));
assertEquals(SeatStatus.RESERVED, plane.getSeatStatus(2, 'C'));
assertEquals(SeatStatus.UNRESERVED, plane.getSeatStatus(2, 'D'));
有关此主题的更多详细信息,请查看this article。