I know that truncate
isn’t supported so I do a Delete from table
– this works quite good but the join tables aren’t cleaned this way. Example:
Delete from Product; Delete from Service;
both empty, table service_product
is still filled. Is there a chance to clean my join tables without raw sql?
example entity
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; ... }
Answer
You should add the @OnDelete(action=OnDeleteAction.CASCADE) annotation. So if you’re using Hibernate try:
@OneToMany(targetEntity = ServiceDownload.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY) @OnDelete(action=OnDeleteAction.CASCADE) private List<ServiceDownload> serviceDownloads;
See http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html for some examples and documentation.