Hibernate

Olá.

Estou aprendendo Hibernate mas , mesmo consultando a documentação oficial e os tutoriais ( cada tutorial tem uma visão diferente sobre a implementação do hibernate ) tenho encontrado dificuldades. :frowning:

Tenho recebido a seguinte mensagem ( HibernateException ) com frequência :

net.sf.hibernate.HibernateException: identifier of an instance of negocio.Fabricante altered from 2 to 2

Ela surge após uma query que, aparentemente funciona muito bem :

		session = HibernateUtil.currentSession();
		tx = session.beginTransaction();
		Query q = session.createQuery("from Fabricante as fabricante order by fabricante.nome");
		result = q.list();
		tx.commit();

Alguém pode me ajudar ?? Não coloquei todos os arquivos ( xml ) para não encher demais a mensagem.

Desde já, muito obrigado.

Márcio

Provavelmente você mapeou o campo identificador (identifier) da tabela que é do tipo INTEGER usando LONG, ou vice-versa. Aí ele dá essa mensagem (não muito clara) que o campo mudou de 2 (integer) para 2 (long) e causa a exception.

Valeu !!!

Gastaria um dia inteiro para "tentar"resolver isto. Nunca pensei que fosse a definição do tipo de campo.

Muitíssimo obrigado

Márcio

(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++

GUJ internacional :smiley:

Yeah!
http://www.javahispano.org/news.item.action?id=226604678

:wink:

valeuz…