gpt4 book ai didi

java - Hibernate:org.hibernate.NonUniqueObjectException:

转载 作者:行者123 更新时间:2023-12-02 02:04:03 25 4
gpt4 key购买 nike

我有 3 个不同的实体类,即 PashminaDescriptionImagePashminaColour。这里,Pashmina 与描述、图像和 PashminaColour 具有一对多关系。我试图同时保存所有这些实体,但出现了一些错误:

(org.hibernate.HibernateException) org.hibernate.HibernateException: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.nepitc.mshandloomfrabics.entity.Description#0]

我使用以下代码来保存

@Override
public void insert(T t) throws HibernateException {
session = sessionFactory.openSession();
trans = session.beginTransaction();

try {
session.save(t);
trans.commit();
} catch(HibernateException ex) {
trans.rollback();
throw new HibernateException(ex);
} finally {
session.close();
}
}

注意:如果我仅使用一张图像、描述或 pashmina 颜色保存 Pashmina 详细信息,它可以让我插入,但如果我使用多个图像保存 Pashmina,pashmina 颜色或描述会显示错误。

这就是我实现 Controller 的方式

@RequestMapping(value = "/add-pashmina", method = RequestMethod.POST)
public @Async ResponseEntity<String> insertPashmina(@RequestBody Pashmina pashmina) {
if (pashmina != null) {
try {
pashminaService.insert(pashmina);

pashminaId = pashmina.getPashminaId();

for (PashminaColour pash : pashmina.getPashminaColor()) {
pashminaColorService.insert(new PashminaColour(pash.getColor(), new Pashmina(pashminaId)));
}

for (Description desc : pashmina.getDescriptions()) {
descriptionService.insert(new Description(desc.getPashminaDescription(), new Pashmina(pashminaId)));
}

return new ResponseEntity<>(HttpStatus.OK);

} catch (HibernateException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

羊绒

public class Pashmina implements Serializable {

private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sq_pashmina_id")
@SequenceGenerator(name = "sq_pashmina_id", sequenceName = "sq_pashmina_id")
@Column(name = "PASHMINA_ID", unique = true, nullable = false)
private int pashminaId;

@Column(name = "PASHMINA_NAME")
private String pashminaName;

@Column(name = "PRICE")
private double price;

@Column(name = "ADDED_AT", insertable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date addedAt;

@Column(name = "CATEGORY")
private String category;

@Column(name = "ENABLED", insertable = false)
private Character enabled;

@OneToMany(mappedBy = "pashmina", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<PashminaColour> pashminaColor;

@OneToMany(mappedBy = "pashmina", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Image> images;

@OneToMany(mappedBy = "pashmina", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Description> descriptions;

图片

public class Image implements Serializable {

private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id

@Column(name = "IMAGE_ID")
private int imageId;

@Column(name = "IMAGE_NAME")
private String imageName;

@JoinColumn(name = "PASHMINA_ID", referencedColumnName = "PASHMINA_ID")
@ManyToOne
private Pashmina pashmina;

@Column(name = "PUBLIC_ID")
private String publicId;

羊绒颜色

public class PashminaColour implements Serializable {

private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id

@Column(name = "COLOUR_ID", insertable = false)
private int colourId;
@Column(name = "COLOR")
private String color;

@JoinColumn(name = "PASHMINA_ID", referencedColumnName = "PASHMINA_ID")
@ManyToOne
private Pashmina pashmina;

描述

public class Description implements Serializable {

private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Id

@Column(name = "DESCRIPTION_ID")
private int descriptionId;

@Column(name = "PASHMINA_DESCRIPTION")
private String pashminaDescription;

@JoinColumn(name = "PASHMINA_ID", referencedColumnName = "PASHMINA_ID")
@ManyToOne
private Pashmina pashmina;

对于每个类,Id 都是使用 Oracle 数据库中的触发器插入的。谢谢!

如果你们不理解我,请告诉我

这就是我将实体发送到 Controller 的方式 enter image description here

最佳答案

我认为问题在于您从 ajax 请求发送的实体类名称不匹配。我看到您正在发送 PashminaModel 实体,但您在 Spring POJO 类中仅使用 Pashmina 。尝试改变你的实体类,即。 PashminaPashminaModelDescriptionDescriptionModelImageImageModelPashminaPashminaModel

希望它能起作用。

关于java - Hibernate:org.hibernate.NonUniqueObjectException:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51076714/

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