如何在使用JPA时允许ArrayList中有重复项?

huangapple go评论75阅读模式
英文:

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 &quot;java.lang.IllegalStateException: Multiple representations of the same entity&quot; even though I have the @Id set as true and I&#39;m using a one to many relation on my variable. 

Here are the classes which I&#39;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&lt;&gt;();
}

}

@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 
&gt;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(&quot;example&quot;, &quot;alt example&quot;);
AlternativeAreaName altAreaName2 = new AlternativeAreaName(&quot;example&quot;, &quot;alt example2&quot;);

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

huangapple
  • 本文由 发表于 2020年4月5日 21:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61043380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定