子主体在删除父实体时不删除

时间:2017-04-21 07:30:50

标签: java jpa orm

删除父实体时,我还想删除关联的子实体(来自数据库)。我试图在删除时使用cascade,如下所示,但我必须做错误的事情。

当在父实体对象上调用remove时,我收到错误消息:"该实体仍在数据库的其他位置引用"。我可以确认在数据库中其他位置引用实体的唯一位置是在下面的两个表中(如果我手动从数据库中删除子行,则对父项的删除调用工作正常)。在过去的9个小时里,我一直在阅读有关实体对象和尝试不同内容的内容。我做错了什么?

这是我的父表:

@Entity
@Table(name = "TURTLE_LOOKUP")
public class TurtleLookup implements Serializable 
{

@Basic(optional = false)
@Column(name = "TURTLEID")
private int turtleid;

@Basic(optional = false)
@Column(name = "TURTLE")
private String turtle;

@OneToMany(mappedBy = "turtleType", cascade = CascadeType.REMOVE)
List<TurtleReview> turtleReviews;

...
}

这是我的孩子表:

@Entity
@Table(name = "TURTLE_REVIEW")
public class TurtleReview implements Serializable 
{

@Column(name = "TURTLE_REVIEW_ID")
private int turtleReviewId;

@Column(name = "TURTLE_YEAR")
private int turtleYear;

@ManyToOne(cascade = CascadeType.REMOVE, optional = false)
@JoinColumn(name = "TURTLE_ID", referencedColumnName = "TURTLEID")
private TurtleLookup turtleType;

@Column(name = "IS_COMPLETE")
private short isComplete;

...
}

EDIT / UPDATE:

如果我将CascadeType.REMOVE更改为CascadeType.ALL,则在删除父TurtleLookup实体对象时,会从数据库中成功删除TurtleReview实体。但是,当调用以下函数来创建新的TurtleReview实体对象时,JPA会尝试将新的TurtleLookup实体插入到数据库中,这会抛出异常:&#34;条目已驻留在数据库中。交易回滚&#34;。下面是创建新TurtleReview实体时执行的代码。

public void setDatasetReviewComplete(TurtleLookup turtle, Short year, boolean isComplete)
{
TurtleReview turtleReview = getTurtleReview(turtle, year);
if (turtleReview == null)
{
turtleReview = new TurtleReview();
turtleReview.setTurtleYear(year)
turtleReview.setTurtleType(new a.b.entity.TurtleLookup(turtle.getId(), turtle.getValue()));
}
turtleReview.setIsComplete(isComplete ? (short)1 : 0);
entityManager.persist(turtleReview);
}

3 个答案:

答案 0 :(得分:0)

尝试将级联值更改为all或all-delete-orphan

@OneToMany(mappedBy = "turtleType", cascade = CascadeType.REMOVE)
List<TurtleReview> turtleReviews;

...
}

答案 1 :(得分:0)

尝试使用hibernate @Cascade注释:

@Cascade(value = CascadeType.ALL)
@OneToOne(mappedBy = "turtleReview") // mappedBy name of TurtleRewiew object field in TurtleLookup entity class
private TurtleLookup turtleType;

如果你的关系是oneOoOne,你就不能让oneToMany到另一方而你不能拥有List<TurtleReview>。如果您的关系是oneToMany,那么您的实体将是例如:

@Entity
@Table(name = "TURTLE_LOOKUP")
public class TurtleLookup implements Serializable 
{

@Basic(optional = false)
@Column(name = "TURTLEID")
private int turtleid;

@Basic(optional = false)
@Column(name = "TURTLE")
private String turtle;

@OneToMany(mappedBy = "turtleType") // or add cascade = javax.persistence.CascadeType.ALL and remove @Cascade if you are not using hibernate
@Cascade(value = CascadeType.ALL) 
List<TurtleReview> turtleReviews;

...
}


@Entity
@Table(name = "TURTLE_REVIEW")
public class TurtleReview implements Serializable 
{

@Column(name = "TURTLE_REVIEW_ID")
private int turtleReviewId;

@Column(name = "TURTLE_YEAR")
private int turtleYear;

@ManyToOne
@JoinColumn(name = "TURTLE_ID", referencedColumnName = "TURTLEID")
private TurtleLookup turtleType;

@Column(name = "IS_COMPLETE")
private short isComplete;

...
}

答案 2 :(得分:0)

您的域模型可能存在问题,问题中遗漏了一部分。你可能有圆形级联吗?如果你有一个级联圈,其中一些是CascadeType.REMOVE,有些是CascadeType.PERSIST,那么当你调用remove()方法时,Hibernate(不确定其他JPA实现)会做什么....没有错误或异常消息。