gpt4 book ai didi

java - @ManyToOne 错误 - JPA/Hibernate 分离实体传递到持久化

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

晚上好,

我对使用 Hibernate 比较陌生,并且遇到了以下错误:

"message": "org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist:
com.company.project.data.relational.models.ListsItems; nested exception is org.hibernate.PersistentObjectException:
detached entity passed to persist: com.company.project.data.relational.models.ListsItems",

我有一个从具有嵌套对象的前端发送的 JSON 对象。我试图获取 MySQL 中单独表中的嵌套项目,并使用原始对象 ID 建立关系。

这是 JSON 的示例:

{
"name":"Test",
"type":"App Id List",
"listItems":
[
{
"id":1,
"name":"Test",
"value":" 1"
},
{
"id":2,
"name":"NEW TEST",
"value":" 2"
}
]
}

这是我的列表模型:

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "lists")
public class Lists implements Serializable, OperationalEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column(columnDefinition = "char", nullable = false)
private String guid;
private String name;
private String type;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "listItems", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL, })
private Set<ListsItems> listItems;

private Date created;
private Date updated;

}

这是我的 ListsItems 模型:

@Getter
@Setter
@Entity
@Table(name = "lists_items")
@NoArgsConstructor
public class ListsItems implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;
private String value;

@NaturalId
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "lists_id", referencedColumnName = "id")
private Lists listItems;

}

这是保存函数:

@PostMapping(value = "/add")
@PreAuthorize("hasRole('ADMIN')")
public @ResponseBody WebResponse<W> create(@RequestBody W webModel) {
D dbModel = asDbModel(webModel);
dbModel.setGuid(UUID.randomUUID().toString());
return WebResponse.success(createWebModelFromDbModel(getDatabaseEntityRepository().save(dbModel)));
}

关于可能导致此错误的原因有什么想法吗?我进行了一些搜索,但我尝试过的任何其他解决方案都没有成功。

提前致谢!
- 特拉维斯·W.

最佳答案

答案是对 ListItems 进行以下更改:

    @JsonIgnore // this import will be from jackson
@NaturalId
@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "lists_id", referencedColumnName = "id")
private Lists list;

以及以下列表:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "list", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL, })
private Set<ListsItems> listItems;

我还需要迭代结果:

@Override
protected Lists asDbModel(WebLists webModel) {
Lists dbModel = new Lists();
dbModel.setId(webModel.getId());
dbModel.setName(webModel.getName());
dbModel.setType(webModel.getType());
dbModel.setListItems(webModel.getListItems());
for(ListsItems item : webModel.getListItems()) {
item.setList(dbModel);
}
return dbModel;

}

关于java - @ManyToOne 错误 - JPA/Hibernate 分离实体传递到持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60610583/

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