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)