我正在开发一个spring mvc app,其中我有2个型号。 Tripstop和物品交易。以下是我的tripstop模型:
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="TripStop")
public class TripStopModel {
@Id
@Column(name="tripstopid")
@GeneratedValue
private Integer tripStopId;
@Column(name="datetime")
private String tripStopDateTime;
@Column(name="createts")
private Date tripStopCreateTime;
@Column(name="latitude")
private double latitude;
@Column(name="longitude")
private double longitude;
@ManyToOne(optional=true)
@JoinColumn(name="locationid")
private LocationModel locationModel;
@ManyToOne(optional=true)
@JoinColumn(name="userid", referencedColumnName="SYSTEMUSERID")
private UserModel userModel;
@ManyToOne(optional=true)
@JoinColumn(name="tripid")
private TripModel tripModel;
@OneToMany(mappedBy = "tripStopModel", cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<ItemTransactionModel> itemTransactionModelList;
@ManyToOne(optional = true)
@JoinColumn(name="tid")
private CircleModel circleModel;
}
以下是我的商品交易模型:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="itemTx")
public class ItemTransactionModel {
@Id
@Column(name="itemTxid")
@GeneratedValue
private Integer itemTxId;
@Column(name="itemcode")
private String itemCode;
@Column(name="weight")
private String weight;
@Column(name="bagcount")
private Integer bagCount;
@ManyToOne(optional=true)
@JoinColumn(name="tripstopid")
private TripStopModel tripStopModel;
}
以下是我用来删除tripstop的代码:
Session session = sessionFactory.getCurrentSession();
session.delete(tripStopModel);
我希望当我删除tripstop时,来自itemtx的相关记录也应该被删除。为此,我使用级联类型删除。
但是没有一个被删除,而且会引发以下错误:
org.hibernate.exception.ConstraintViolationException: could not execute statement
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
tripstopid是itemtx表中的外键。