in Education by
I have a problem with Hibernate. In Short: How to configure a ManyToMany association with Hibernate when the relationship has an attribute and we need save, delete and update in cascade? In Large: Imagine the following DataBase: Super Mini M______N | attribute There are 3 tables here: "Mini", "Super" and "Super_Mini". Now imagine Super_Mini has 1 attribute for the relation (and obviously the keys). Ok, now this is translating to Hibernate by the following: Super: // The relation is Many to Many, but considering that it has an attribute, this is OneToMany with the ManyMany RelationShip @OneToMany(mappedBy="mini", targetEntity=Mini.class) @Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE}) @LazyCollection(LazyCollectionOption.TRUE) private Set superMini = new HashSet(); SuperMini: @Id @ManyToOne(targetEntity=Super.class,fetch=FetchType.LAZY) @Cascade({CascadeType.LOCK}) @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") }) private Super super; @Id @ManyToOne(targetEntity=Mini.class,fetch=FetchType.LAZY) @Cascade({CascadeType.LOCK}) @JoinColumns({ @JoinColumn(name="...", referencedColumnName="...") }) private Mini mini; So, I think the configuration is correct, and the save, independently if the object has Mini childrens save all of them. The problem is when I try to delete the object: Super data = getHibernateTemplate().load(Super.class, idSuper); getHibernateTemplate().getSessionFactory().getCurrentSession().clear(); data.setMini( new HashSet() ); getHibernateTemplate().delete( data ); getHibernateTemplate().getSessionFactory().getCurrentSession().flush(); Hibernate don´t delete the Mini relation... What´s the problem? I know how to solve it by HQL, but maybe the configuration is not correct, I don´t know. Thank you in advance, JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your question is not clear. Super does not contain a Set. It contains a Set. So the last code snippet doesn't make much sense. Moreover, the targetEntity attribute on Super.superMini2 is incorrect, and unnecessary. CascadeType.ALL include CascadeType.DELETE, so it's also unnecessary. But to answer your question, I think the problem is that deleting Super cascades to SuperMini2 because the association has a cascade delete, but there is no cascade delete between SuperMini2 and Mini2, so of course, Mini2 instances are not deleted. EDIT: The answer is that the OP, before editing the question, removed all the entities from the collection of SuperMini before deleting the Super entity. So the cascade delete on the collection of Supermini didn't have anything to delete anymore.

Related questions

0 votes
    Here is an example of what I've got going on: CREATE TABLE Parent (id BIGINT NOT NULL, PRIMARY ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
0 votes
    What is the difference between save() and persist() method in Hibernate?...
asked Feb 4, 2021 in Technology by JackTerrance
0 votes
    What is Difference between save(), saveOrUpdate() and persist() in Hibernate Session?...
asked Feb 4, 2021 in Technology by JackTerrance
0 votes
    The ___________ operation, denoted by −, allows us to find tuples that are in one relation but are not ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What is One-to-Many association in Hibernate?...
asked Feb 4, 2021 in Technology by JackTerrance
0 votes
    I created update() method which is using JPA. It looks like this: public boolean update(Programy program) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I am creating a maven project from scratch, first just learn how to use maven. The main project contains ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I am creating a maven project from scratch, first just learn how to use maven. The main project contains ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I have been using php-sdk to connect to facebook for a while and I never had any problems, but ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    f. Use the following formula to output the variable on the left: Cascade the cout statements Use the following variations ... (v) E=mc Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    @Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } I'd like to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    @Entity class A { @ManyToMany private List list; ... } @Entity class B { ... } I'd like to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    What is Many-to-Many association in Hibernate?...
asked Feb 4, 2021 in Technology by JackTerrance
0 votes
    Which are the reasons for endangering the many species of plants and animals? How can we save ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 8, 2021 in Education by JackTerrance
0 votes
    I have the following table relationship in my database: Parent / \ Child1 Child2 \ / GrandChild I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...