views:

39

answers:

0

I have following mapping for my Category objects:

@Entity
public class Category extends CrawlableEntity implements Identifiable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Expose
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)    
    private Category parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private List<Category> categories = new ArrayList<Category>();

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
    @Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private List<Product> products = new ArrayList<Product>();
...
}

This just defines category tree and allow to easily navigate through it. Each cateory also contains collection of products.

The problem occurs when I want to delete on of root cateories.

getHibernateTemplate().delete(category)

Deletion of which should lead to deletion of all its children(product and categories). I'm just getting OutOfMemory exception. If I'm deleting cateogory which doesn't have many child products and cateories everything is ok. So is there is way how to delete bug objects tree without consuming lot of memory?