Question

Say i have class called Record, with a many-to-one relationship to a class called Artist.

If i define a cascade option as such:

class Record{

    @ManyToOne(cascade = CascadeType.ALL)
    private Artist artist;

    ...
}

Say i have a number of records managed by my EntityManager and some of these share the same Artist. What happens when i call, update, merge, detach, remove and so on on the Record objects? Will the artist be removed for example? Will it be detached? If so, what happens to the other Record classes that references that Artist?

Was it helpful?

Solution

Since you configured the association with cascade = CascadeType.ALL, all the operations done on a record will also be done on the associated Artist. Removing the record will thus remove the artist. This will fail with a foreign key constraint exception (if they're correctly configured in the database) if another record also references the same artist.

Configuring such a cascade on a ManyToXxx associations doesn't make much sense.

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