我有以下三个实体EntityA,EntityB和EntityC:
EntityA:
import lombok.*;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "Entity_A")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "locationA")
@ToString(of = "locationA")
public class EntityA {
@Id
@Column(name = "Name_A", length = 10)
private String nameA;
@Column(name = "Loc_A", length = 10)
private String locationA;
@ManyToMany(cascade = { CascadeType.MERGE })
@JoinTable(
name = "En_A_On_B",
joinColumns = { @JoinColumn(name = "Name_A") },
inverseJoinColumns = { @JoinColumn(name = "B_id") }
)
private Set<EntityB> bs;
}
实体B:
import lombok.*;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "Entity_B")
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "locationB")
@ToString(of = "locationB")
public class EntityB {
@Id
@GeneratedValue
@Column(name = "B_id")
private int id;
@Column(name = "Loc_B", length = 10)
private String locationB;
@ManyToMany(cascade = { CascadeType.MERGE })
@JoinTable(
name = "En_C_on_B",
joinColumns = { @JoinColumn(name = "B_id") },
inverseJoinColumns = { @JoinColumn(name = "C") }
)
private Set<EntityC> cs;
}
EntityC:
import lombok.*;
import javax.persistence.*;
import java.util.Set;
@Entity
@Table(name = "Entity_C")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = "c")
@ToString(of = "c")
public class EntityC {
@Id
@Column(name = "C", length = 20)
private String c;
}
要保存的服务类别:
@Service
@Slf4j
public class ServiceClass {
@Autowired
private EntityARepository entityARepository;
private Set<EntityC> cs1 = new HashSet<>(asList(
EntityC.builder().c("100").build(),
EntityC.builder().c("10").build()
));
private Set<EntityC> cs2 = new HashSet<>(asList(
EntityC.builder().c("100").build(),
EntityC.builder().c("200").build()
));
//METHOD TO SAVE
public void save() {
Map<String, Set<EntityC>> map = new HashMap<>();
map.put("B1", cs1);
map.put("B2", cs2);
List<String> bs = asList("B1", "B2");
EntityA aa = EntityA.builder().nameA("abcd").locationA("mon").build();
EntityA ab = EntityA.builder().nameA("abcde").locationA("money").build();
bs.forEach(b -> {
EntityB entityB = EntityB.builder().locationB("100xxx").build()
entityB.getCs().addAll(map.get(b));
aa.getBs().add(entityB);
ab.getBs().add(entityB);
});
entityARepository.save(aa);
entityARepository.save(ab);
}
}
上述代码的执行会引发以下异常
由于:java.lang.IllegalStateException:同一实体[com.xxx.xxx.xxx.xxx.EntityC#100]的多个表示形式正在被合并。分离:[(c = 100)];分离:[(c = 100)]
注意:我已经在互联网上进行了探索,但没有一个与我的情况相符
任何想法我该如何纠正该问题
答案 0 :(得分:1)
问题就在这里:
select location_id, full_name, locationName, item_id, displayname,
LOCATION_ON_HAND_COUNT, LOCATION_ON_ORDER_COUNT,
LOCATION_QUANTITYBACKORDERED,
LOCATION_AVAILABLE_COUNT, LOCATION_REORDER_POINT
, ir.W_QtyOrdered, ir.WQty_Shipped, ir.WOnHand,
ir.item_no, ir.ItemNo
from dw_item_location
left join westrock.InventoryReport IR on
dw_item_location.znumber = IR.ItemNo
where location_id = 2 or location_id = 3
您试图在一个工作单元中保留同一实体的两个版本。想象一下,您输入:
private Set<EntityC> cs1 = new HashSet<>(asList(
EntityC.builder().c("100").build(), //this entity instance has the same identifier...
EntityC.builder().c("10").build()
));
private Set<EntityC> cs2 = new HashSet<>(asList(
EntityC.builder().c("100").build(), //...as this one
EntityC.builder().c("200").build()
));
在EntityC.builder().c("100").name("A name").build()
中:
cs1
在EntityC.builder().c("100").name("Another name").build()
中代替。由于两个实体具有相同的cs2
(id
),Hibernate应该如何知道哪个版本“获胜”?
尝试将c="100"
中相同的实例放在两组中,问题应该消失了。