Hibernate version: 2.1.4
I know (like docs) that Hibernate performs one DELETE before insert in bidirectional (bag) many-to-many associations, like this:
The generated SQL (show_sql=true):
16:19:45,794 INFO [STDOUT] Hibernate: update Instituicao set bairro=?, cep=?, complemento=?, descricao=?, email=?, estado=?, fax=?, fone=?, localidade=?, logradouro=?, nome=?, numero=?, observacao=?, ramal=?, tipo=?, uf=?, logCriacao=?, logUltimaAtualizacao=?, logUsuario=?, logVersao=? where id=?
16:19:45,872 INFO [STDOUT] Hibernate: delete from AtividadeInstituicao where InstituicaoId=?
16:19:45,904 INFO [STDOUT] Hibernate: insert into AtividadeInstituicao (InstituicaoId, AtividadeId) values (?, ?)
However, this behavior is doubtful because all Atividade's associated with this Instituicao are affected.
Supose that BD have this relations:
Code:
Atividade | Instituicao
----------+------------
2 | 14
2 | 16
3 | 16
So, I try to create a new relation between Atividade 15 and Instituicao 16, like it:
Code:
//Initializes persistency
p = PersistenciaFactory.getInstance(ServiceLocator.SERVIDOR_LOCAL).getPersistenciaRemota(GapeHibernateHome.class);
//Load Atividade 15 and initializes bag of Instituicao (many-to-many)
Atividade a = (Atividade) p.load(Atividade.class, new Long(15), new String[] {Atividade.CHILD_INSTITUICOES_PARCEIRAS});
//Load Instituicao 16 and NOT initializes bag of Atividade (inverse many-to-many) because it's very large
Instituicao i16 = (Instituicao) p.load(Instituicao.class, new Long(16));
//Link Atividade and Instituicao
a.addInstituicao(i16); //make bidirectional links
//Update Instituicao 16
p.update(i16);
This code realy insert a new row:
Code:
Atividade | Instituicao
----------+------------
15 | 16
but removes these rows:
Code:
Atividade | Instituicao
----------+------------
2 | 16
3 | 16
and ***it are wrong!***
So, my question is: It's possible disable this DELETE before insert?
Or, How to execute this operation in other way...
Thank's for all and sorry my english, this text was translated with babelfish...