英文:
How to allow duplicates in an arrayList when using JPA?
问题
我不会翻译代码,只提供以下内容的翻译:
"I keep getting "java.lang.IllegalStateException: Multiple representations of the same entity" even though I have the @Id set as true and I'm using a one to many relation on my variable.
Here are the classes which I'm trying to relate to one another:
@Entity
@Table(name = "map_area")
public class MapArea extends BasicModel {
@Id
@Column(nullable = false, unique = true)
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "area", referencedColumnName = "name")
public List<AlternativeAreaName> alternativeNames;
public MapArea() {}
public MapArea(String name) {
this.name = name;
this.alternativeNames = new ArrayList<>();
}
}
@Entity
@Table(name = "alternative_area_name")
public class AlternativeAreaName implements Serializable {
@Id
@Column(nullable = false, unique = false)
private String area;
@Column(nullable = true)
private String alternativeName;
public AlternativeAreaName(){}
public AlternativeAreaName(String area, String alternativeName) {
this.area = area;
this.alternativeName = alternativeName;
}
}
I want to have JPA create another table that relates to this one simple based on the name variable but whenever I try to add to the list and save to the DB I get
>java.lang.IllegalStateException: Multiple representations of the same entity
MapArea mapArea = new MapArea("example");
AlternativeAreaName altAreaName1 = new AlternativeAreaName("example", "alt example");
AlternativeAreaName altAreaName2 = new AlternativeAreaName("example", "alt example2");
mapArea.alternativeNames.add(altAreaName2);
mapAreaRepository.save(mapArea);
```"
<details>
<summary>英文:</summary>
I keep getting "java.lang.IllegalStateException: Multiple representations of the same entity" even though I have the @Id set as true and I'm using a one to many relation on my variable.
Here are the classes which I'm trying to relate to one another:
@Entity
@Table(name = "map_area")
public class MapArea extends BasicModel {
@Id
@Column(nullable = false, unique = true)
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "area", referencedColumnName = "name")
public List<AlternativeAreaName> alternativeNames;
public MapArea() {}
public MapArea(String name) {
this.name = name;
this.alternativeNames = new ArrayList<>();
}
}
@Entity
@Table(name = "alternative_area_name")
public class AlternativeAreaName implements Serializable {
@Id
@Column(nullable = false, unique = false)
private String area;
@Column(nullable = true)
private String alternativeName;
public AlternativeAreaName(){}
public AlternativeAreaName(String area, String alternativeName) {
this.area = area;
this.alternativeName = alternativeName;
}
}
I want to have JPA create another table that relates to this one simple based on the name variable but whenever I try to add to the list and save to the DB I get
>java.lang.IllegalStateException: Multiple representations of the same entity
MapArea mapArea = new MapArea("example");
AlternativeAreaName altAreaName1 = new AlternativeAreaName("example", "alt example");
AlternativeAreaName altAreaName2 = new AlternativeAreaName("example", "alt example2");
mapArea.alternativeNames.add(altAreaName2);
mapAreaRepository.save(mapArea);
</details>
# 答案1
**得分**: 0
你已将 `private String area` 字段用作实体 `AlternativeAreaName` 的主键。因此,在尝试添加以下内容时:
```java
AlternativeAreaName altAreaName1 = new AlternativeAreaName("example", "alt example");
AlternativeAreaName altAreaName2 = new AlternativeAreaName("example", "alt example2");
它们都具有相同的主键,因此会引发上述异常。
要为 JPA 实体生成主键,请查看以下链接:
英文:
You have used the private String area
field as the primary key for entity AlternativeAreaName. So when you are trying to add
AlternativeAreaName altAreaName1 = new AlternativeAreaName("example", "alt example");
AlternativeAreaName altAreaName2 = new AlternativeAreaName("example", "alt example2");
Both of them have the same primary key. So it is throwing the above exception.
To generate the primary key for JPA entity, please check
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论