再一次:如何解决'Repeated column in mapping for collection'?
And again: How to solve 'Repeated column in mapping for collection'?
我正在尝试在实体 Player
中使用 HashMap
,而我的 HashMap
是 Integer
和另一个实体(实体 ResourceOnPlayerAccount
)的复合体这是一对多的关系,所以我可以防止重复的条目,但仍然可以保留最新的条目。我的代码在 HashSet
下运行良好,但那样我就无法保留最新的条目。
我已经搜索过另一个 post。在这 post 中,我找到了这个
Another Repeated column in mapping for entity error
但是在这个post中,问题好像是某个列的重复调用,但是在我的代码中,我找不到类似的东西。而且我找不到带有 HashMap
.
的示例
我添加了 Player 实体如下:
Player.java
@Entity
@Table(name="player")
public class Player {
// define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="user_name")
private String userName;
@Column(name="password")
private String password;
@Column(name="display_name")
private String displayName;
@Column(name="email")
private String email;
// define relationships
// @OneToMany(cascade = CascadeType.ALL)
// @JoinColumn(name = "player_id")
// private Set<ResourceOnPlayerAccount> resourceOnPlayerAccountSet;
@OneToMany
@MapKeyColumn(name = "player_id")
private Map<Integer, ResourceOnPlayerAccount> resourceOnPlayerAccountMap;
...
注释行是我之前使用 HashSet
实现的,它有效。
以下是另一个实体 class:
ResourceOnPlayerAccount.java
@Entity
@Table(name = "resources_on_player_account")
public class ResourceOnPlayerAccount {
// define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
private double amount;
@Column(name = "resource_id")
private int resourceId;
...
我只想确保玩家的帐户中没有重复的资源,并且可以更新现有的资源量。
@MapKeyColumn
用于Map<Basic, Basic>
情况。
在你的情况下,它是 Map<Basic, Entity>
。为此使用 @MapKey
:
Specifies the map key for associations of type java.util.Map when the
map key is itself the primary key or a persistent field or property of
the entity that is the value of the map.
我正在尝试在实体 Player
中使用 HashMap
,而我的 HashMap
是 Integer
和另一个实体(实体 ResourceOnPlayerAccount
)的复合体这是一对多的关系,所以我可以防止重复的条目,但仍然可以保留最新的条目。我的代码在 HashSet
下运行良好,但那样我就无法保留最新的条目。
我已经搜索过另一个 post。在这 post 中,我找到了这个
Another Repeated column in mapping for entity error
但是在这个post中,问题好像是某个列的重复调用,但是在我的代码中,我找不到类似的东西。而且我找不到带有 HashMap
.
我添加了 Player 实体如下:
Player.java
@Entity
@Table(name="player")
public class Player {
// define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="user_name")
private String userName;
@Column(name="password")
private String password;
@Column(name="display_name")
private String displayName;
@Column(name="email")
private String email;
// define relationships
// @OneToMany(cascade = CascadeType.ALL)
// @JoinColumn(name = "player_id")
// private Set<ResourceOnPlayerAccount> resourceOnPlayerAccountSet;
@OneToMany
@MapKeyColumn(name = "player_id")
private Map<Integer, ResourceOnPlayerAccount> resourceOnPlayerAccountMap;
...
注释行是我之前使用 HashSet
实现的,它有效。
以下是另一个实体 class:
ResourceOnPlayerAccount.java
@Entity
@Table(name = "resources_on_player_account")
public class ResourceOnPlayerAccount {
// define fields
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "amount")
private double amount;
@Column(name = "resource_id")
private int resourceId;
...
我只想确保玩家的帐户中没有重复的资源,并且可以更新现有的资源量。
@MapKeyColumn
用于Map<Basic, Basic>
情况。
在你的情况下,它是 Map<Basic, Entity>
。为此使用 @MapKey
:
Specifies the map key for associations of type java.util.Map when the map key is itself the primary key or a persistent field or property of the entity that is the value of the map.