Question

I am developing a website and it has quite a lot many to many relationships. This means that usually when I delete something it might be connected to tons of other things which will result in deleting way too many things. In that respect I would like to ask you few things:

  1. What are the cascade options and how to disable cascade delete.
  2. Let's say we've got the following case:

CLASS TASK:

@Entity
public class Task extends Model {

    @Id
    public Long id;

    @Required
    public String label; 


    @ManyToMany
    public  List<User> users = new ArrayList();

    public void addUser(User user){

            users.add(user);

    }

    public static List<Task> all(){

        return find.all();
    }

    public static Finder<Long,Task> find = new Finder(

        Long.class, Task.class

    );


    public static void create (Task task){

        task.save();
    }

    public static void delete (Long id){

        find.ref(id).delete();

    }   

}

CLASS USER:

@Entity
@Table(name="Users")

public class User extends Model {



    @Id
    public Long id;

    @Required
    public String Name; 



    @ManyToMany 
    public  List<Task> tasks = new ArrayList();


    public static List<User> all(){

        return find.all();
    }

    public static Finder<Long,User> find = new Finder(

        Long.class, User.class

    );


    public static void create (User user){

        user.save();
    }

    public static void delete (Long id){

        find.ref(id).delete();

    }

    public void addTask(Task task){

        tasks.add(task);

    }
}

As you can see tasks have many users and users have many tasks. When I delete an object I want to delete not only the object itself but also the references of other objects to this object. Say for example:

User John has three tasks to do: A, B and C Tasks A,B and C are assigned to other users as well. I want to delete John and to delete the database references of John to A, B and C. I don't want to delete A, B and C tasks as they are still being used.

What cascade options do I use for this case?

Was it helpful?

Solution

Found the answer! It seems that cascade delete in database terms and in the framework are not the same thing. cascade = CascadeType.ALL deletes the object and the references of that object to others. However, it does not delete other objects.

Thank you my brilliant self.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top