gpt4 book ai didi

java - JPA hibernate : An entity copy [xxx] was already assigned to a different entity [yyyyy]

转载 作者:太空宇宙 更新时间:2023-11-04 14:58:46 25 4
gpt4 key购买 nike

最近,当我在 Play 2.2.1 上使用 JPA(1.3.0.final)Hibernate(4.2.6.final) 时> 框架,我收到如下错误:

java.lang.IllegalStateException:存储实体 [xxx] 时发生错误实体副本 [xxx] 已分配给不同的实体 [yyy]

互联网上现有的解决方案都无法解决我的问题。

现在,让我解释一下这个故事。

我有三个实体,所有者盒子元素

假设:

  1. 元素或盒子必须有一个所有者;
  2. 一个 Box 可以包含零个或多个项目,而特定项目可能不属于任何 Box。
  3. 当元素放入盒子中时,它们可能有相同的所有者,也可能不同。

实体类:

/* 为了简单起见,省略了其他字段、getter 和 setter*/

简化的 Owner 类定义

    @Entity
@Table(name = "OWNER")
public class Owner implements Serializable{
@Id
@Column(name="ID")
public String uuid = UUID.randomUUID().toString();

@Override
public int hashCode() {
int result = 1;
char[] charArray = this.uuid.toCharArray();
int n = charArray.length;
int powbase = (int) Math.pow(31, n - 1);
for(int i = 0; i < n; i++){
if ( i != 0){
powbase = powbase / 31;
}
result += (int)charArray[i] * powbase;
}
return result;
}

@Override
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Item)){
return false;
}
Item other = (Item)obj;
//Box other = (Box)obj;
//Owner other = (Owner)obj;
if(!other.uuid.equals(this.uuid)){
return false;
}
return true;
}
}

Item和Box的hashCode()和equals()实现与Owner类似。为了便于讨论,我们忽略这些。

简化的 Item 类定义

@Entity
@Table(name = "ITEM")
public class Item implements Serializable{

@Id
@Column(name="ID")
public String uuid = UUID.randomUUID().toString();

/* single-directional mapping*/
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "O_ID")
public Owner owner;

/* bi-directional mapping*/
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
@JoinColumn(name = "B_ID")
public Box box;
}

简化的 Box 类定义。

@Entity
@Table(name = "BOX")
public class Box implements Serializable {

@Id
@Column(name="ID")
public String uuid = UUID.randomUUID().toString();

/* single-directional mapping*/
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false)
@JoinColumn(name = "O_ID")
public Owner owner;

/* bi-directional mapping*/
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.REFRESH,
CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.EAGER, mappedBy = "box")
public Set<Item> items = new HashSet<Item>();
}

业务逻辑如下:

public class Test {

public static void main(String[] args) {
Owner owner = new Owner();
JPA.em().persist(owner);

Box box = new Box();
box.owner = owner;
JPA.em().persist(box);

Item boxItem = new Item();
boxItem.owner = owner;
boxItem.box = box;
box.items.add(boxItem);
JPA.em().persist(boxItem);

Item nonBoxItem = new Item();
nonBoxItem.owner = owner;
JPA.em().persist(nonBoxItem);

/* in the future, put the 'nonBoxItem' into a box*/
Item itemToUpdate = JPA.em().find(Item.class, nonBoxItem.uuid);
itemToUpdate.box = box;
box.items.add(itemToUpdate);

JPA.em().merge(itemToUpdate); /* error occurred here*/
}

}

有人有办法解决这个问题吗?

谢谢。

最佳答案

我也遇到了同样的问题。通过从此链接添加依赖项解决了 https://stackoverflow.com/questions/17634230/hibernate-an-entity-copy-was-already-assigned-to-a-different-entity这对我有用

<dependency>
<groupId>com.intersult</groupId>
<artifactId>jpa-fix</artifactId>
<version>1.1</version>

干杯!

关于java - JPA hibernate : An entity copy [xxx] was already assigned to a different entity [yyyyy],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com