JPA空表和连接表

时间:2010-09-23 12:23:29

标签: java hibernate jpa

我知道truncate不受支持,因此我执行了Delete from table - 这项工作非常好但是连接表不会以这种方式清理。例如:

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

2 个答案:

答案 0 :(得分:0)

  • 您必须获取完整表格(FROM Product),迭代实体并使用session.delete(..)
  • 删除它们
  • DELETE不会触发级联
  • 如果它是本机SQL查询,则支持
  • truncate

答案 1 :(得分:0)

您应该添加@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