在我的实体中,我有一个Player对象映射如下
@JoinColumn(name = "player_id", referencedColumnName = "player_id")
@ManyToOne(optional = false)
private Player player;
但是现在如果我需要访问玩家的ID,我需要通过玩家对象。有没有办法将引用的列直接映射到实体?
答案 0 :(得分:0)
如果你需要在不加载对象的情况下获取玩家ID,那么你可以执行以下操作。
@JoinColumn(name = "player_id", referencedColumnName = "player_id")
@ManyToOne(optional = false)
private Player player;
@Column(name="player_id", insertable=false, updatable=false)
private Long playerId;
由于列player_id
是重复映射,因此必须将其标记为只读(如上所述)。