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

时间:2013-10-22 16:23:58

标签: java hibernate jpa

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

我想做的事情:

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

但该实体未被删除。

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

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

SystemParameter:

@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;
}

SystemParameterGroup:

@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;
}

1 个答案:

答案 0 :(得分:0)

您还必须从SystemParameter的{​​{1}}列表中删除SystemParameterGroup实体:

SystemParameter

此问题与this one

有关