Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

java - Hibernate delete doesn't cascade

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

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

1 Reply

0 votes
by (71.8m points)

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,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...