views:

71

answers:

2

Hi there,

I know that truncateisn'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_productis 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;
...
}
A: 
  • you have to fetch the full table (FROM Product), iterate the entities and delete them using session.delete(..)
  • DELETE does not trigger cascades
  • truncate is supported if it's a native SQL query
Bozho
A: 

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.

Adam