Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a few entities linked as follows:

@Entity
@Table(name = "distribution_activity")
public class DistributionActivity extends AbstractActivity {

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "activity", orphanRemoval = true)
    protected Set<DistributionTask> tasks = new TreeSet<>();

    ...
}

and

@Entity
@Table(name = "distribution_task")
public class DistributionTask extends AbstractTask {

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "activity_id")
    protected DistributionActivity activity;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "store_id")
    protected Store store;

    ...
}

and

@Entity
@Table(name = "store")
public class Store extends AbstractAuditableEntity {

    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "store", orphanRemoval = true)
    protected Set<DistributionTask> distributionTasks = new TreeSet<>();

    ...
}

The repositories are as follows:

@Repository
public interface DistributionActivityRepository extends PagingAndSortingRepository<DistributionActivity, Long> {
}

and

@Repository
public interface StoreRepository extends PagingAndSortingRepository<Store, Long> {
}

I'm using MySQL and the tables are generated WITHOUT any cascade options on the foreign keys. When I delete a DistributionActivity everything works fine and Hibernate actually issues delete statements for each of the linked tasks.

hibernate.SQL:109 - delete from distribution_task where id=? and version=?
hibernate.SQL:109 - delete from distribution_activity where id=? and version=?

When I delete a Store however, no delete statements are generated for the linked tasks and a MySQLIntegrityConstraintViolationException exception is thrown referring to a foreign key violation.

hibernate.SQL:109 - delete from store where id=? and version=?

Any clues?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

Can you post the snippet of code around session.delete() where the deletion happens?

I cannot say it for sure but I have ran in similar issues before related to bidirectional relationships. Briefly speaking, I needed to make sure the objects are in sync when using bidirectional relationships.

It looks to me that you want to delete a Store that still have a reference in DistributionTask.

Example, if you have something like this:

session.delete(store);

Then, try changing for something like this:

distributionTask.setStore(null);
session.save(distributionTask);
session.delete(store);

So; you need control the consistency of your objects manually when dealing with bidirectional relationship.

I hope it helps. Cheers,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...