Hibernate Annotation映射引用的列

时间:2014-05-20 12:11:58

标签: java hibernate

在我的实体中,我有一个Player对象映射如下

@JoinColumn(name = "player_id", referencedColumnName = "player_id")
@ManyToOne(optional = false)
private Player player;

但是现在如果我需要访问玩家的ID,我需要通过玩家对象。有没有办法将引用的列直接映射到实体?

1 个答案:

答案 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是重复映射,因此必须将其标记为只读(如上所述)。