gpt4 book ai didi

java - JPA - 在双向映射中删除子实体的问题

转载 作者:行者123 更新时间:2023-11-29 08:57:25 24 4
gpt4 key购买 nike

我需要根据它的 ID 删除名为“SystemParamater”的实体。

我想做的是:

- Begin Tran
SystemParameter param EntityManager.find(SystemParameter.class,<paremeter.id>)
EntityManager.remove(param)
-Commit tran

但是实体没有被移除。

删除子实体的正确方法是什么?

在下面,您可以找到我的实体:

系统参数:

@Entity
@Table(name = "system_parameters")
@Cacheab le
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SystemParameter extends BasicEntity {

private static final long serialVersionUID = -6416605270912358340L;

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

@Column(name = "paramName", length = 50, nullable = false)
private String paramName;

@Column(name = "paramValue", length = 255, nullable = false)
private String paramValue;

@Column(name = "encrypted", nullable = false)
@Type(type = "yes_no")
private Boolean encrypted = Boolean.FALSE;

@ManyToOne
@JoinColumn(name = "groupId", nullable = false)
private SystemParameterGroup parameterGroup;
}

系统参数组:

@Entity
@Table(name = "system_parameter_groups", uniqueConstraints = { @UniqueConstraint(columnNames = { "searchKey" }) })
@Cacheable
public class SystemParameterGroup extends BasicEntity {

private static final long serialVersionUID = -1762633144642103487L;

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

@Column(name = "name", length = 50, nullable = false)
private String name;
/**
* Description.
*/
@Column(name = "description", length = 255, nullable = false)
private String description;
/**
* Search key.
*/
@Column(name = "searchKey", length = 50, nullable = false)
private String searchKey;
/**
* System parameter list.
*/
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parameterGroup", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("paramName")
private List<SystemParameter> systemParameterList;
}

最佳答案

您还必须从 SystemParameterGroupSystemParameter 列表中删除 SystemParameter 实体:

systemParameterGroup.getSystemParameterList().remove(systemParameter);

此问题与 this one 有关.

关于java - JPA - 在双向映射中删除子实体的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19523295/

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