(Sorry, I can understand portuguese but i can’t speak it)
I did have the same problem and resolved it. It appears that you get the same object twice from the database. At the flushing time (or perhaps at the select time), Hibernate posesses the same object twice in memory, and althought they have the same identifier, Hibernate can’t see it and don’t permit to flush.
The way you fix it is the folowwing :
- detect the moment where you don’t need the first instance of the model object.
- add this line :
(Session hibernateSession;)
hibernateSession.evict(myModelObject);
Here is the sample of code I have in my application :
[code]
if (request.getGroupeEtablissement() != null) {
//Récupération de la liste des id des établissements qui appartiennent à ce groupe
cEtablissements = hibernateSession.createCriteria(EtabGroupeEtab.class);
//critere groupeEtabId
cEtablissements.add(Expression.eq("groupeEtab.groupeEtabId", request.getGroupeEtablissement()));
//liste des établissement du groupe
listEtablissementsId = cEtablissements.list();
alEtablissementsId = new ArrayList();
if ( (null != listEtablissementsId) && (0 < (nbEtablissements = listEtablissementsId.size()))) {
for (int i = 0; i < nbEtablissements; ++i) {
//Récupération de lo'bjet du modèle
[color=red]==> Here I get my first instance of the object of type “EtabGroupeEtab”.[/color]
//Ajout de l'id à la liste
etabGroupeEtab = (EtabGroupeEtab) listEtablissementsId.get(i);
alEtablissementsId.add(etabGroupeEtab.getComp_id().getEtablissement().getEtablissementId());
[color=red] ==> and here I delete my instance from Hibernate cache.[/color]
hibernateSession.evict(etabGroupeEtab);
}
cPoste.add(Expression.in("etablissement.etablissementId", alEtablissementsId));
[color=red]==> This criteria will need to get again my “ETabGroupeEtab” objects from the database. By having put the “evict”, I’m sure this object are not in the database anymore at this time[color]
} else {
//Pas d'établissements dans ce groupe, la recherche ne doit pas renvoyer de résultat
return null;
}
}[/code]
smeet++