JPA空表和连接表
问题描述:
我知道truncate
不被支持,所以我做了一个Delete from table
- 这个工作非常好,但连接表没有以这种方式清理。例如:JPA空表和连接表
Delete from Product;
Delete from Service;
两者都为空,表格service_product
仍填满。有没有机会清理我的连接表没有原始的SQL?
例如实体
public class Service implements Serializable {
private static final long serialVersionUID = 4520872456865907866L;
// seam-gen attributes (you should probably edit these)
@EmbeddedId
private ServiceId id;
@Length(max = 255)
private String servicename;
@Column(columnDefinition = "text")
private String highlightsText;
@Column(columnDefinition = "text")
private String detailsText;
@Column(columnDefinition = "text")
private String productText;
@Column(columnDefinition = "text")
private String dataText;
@ManyToMany(mappedBy = "services")
private Set<Machine> machines;
@OneToMany(targetEntity = ServiceDownload.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private List<ServiceDownload> serviceDownloads;
@OneToMany(targetEntity = ProductSpecial.class, cascade = { CascadeType.ALL })
private List<ProductSpecial> productSpecials;
@OneToOne(cascade = { CascadeType.ALL })
private ServicePicture servicePicture;
...
}
答
Y你应该添加@OnDelete(action = OnDeleteAction.CASCADE)注解。所以,如果你正在使用Hibernate的尝试:
@OneToMany(targetEntity = ServiceDownload.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
@OnDelete(action=OnDeleteAction.CASCADE)
private List<ServiceDownload> serviceDownloads;
见http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html的一些例子和文档。
答
- 你要获取全表(
FROM Product
),迭代的实体和使用session.delete(..)
-
DELETE
不会触发级联 -
truncate
删除他们,如果它的支持原生SQL查询
可能的重复[如何批量删除使用批量更新](http://stackoverflow.com/questions/735201/how-to-batch-delete-using-bulkupdate) – 2010-09-23 14:08:31