2010-09-23 5 views
0

Je sais que truncate n'est pas supporté donc je fais un Delete from table - cela fonctionne plutôt bien mais les tables de jointure ne sont pas nettoyées de cette façon. Exemple:JPA tables vides et tables jointes

Delete from Product; 
Delete from Service; 

à la fois vide, le tableau service_product est toujours rempli. Est-il possible de nettoyer mes tables de jointure sans sql brut?

exemple entité

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

duplication possible de [Comment supprimer par lots en utilisant bulkUpdate] (http://stackoverflow.com/questions/735201/how-to-batch-delete-using-bulkupdate) –

Répondre

0

Y Vous devez ajouter l'annotation @OnDelete (action = OnDeleteAction.CASCADE). Donc, si vous utilisez Hibernate essayer:

@OneToMany(targetEntity = ServiceDownload.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY) 
@OnDelete(action=OnDeleteAction.CASCADE) 
private List<ServiceDownload> serviceDownloads; 

Voir http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html quelques exemples et la documentation.

0
  • vous devez chercher la table complète (FROM Product), itérer les entités et les supprimer à l'aide session.delete(..)
  • DELETE ne déclenche pas des cascades
  • truncate est pris en charge si elle est une requête SQL native
Questions connexes