Hibernate lançando update sozinho

5 respostas Resolvido
hibernate
savaegt

Olá, tenho um objeto que está gerenciado pelo hibernate. Em tela, um atributo desse objeto é atualizado o valor e o Hibernate imediatamente dispara um update no banco de dados, sem nem mesmo passar por métodos no controller ou da camada de persistência de dados.

Alguém consegue me ajudar com isso?

5 Respostas

peczenyj

ué… se o objeto é gerenciado pelo hibernate, pq ele deveria passar pelo controller?

savaegt

Pq eu só quero salvar o dado depois que o usuário clicar em salvar. Daí ele vai pra o controller faz a validação e depois faz o Merge.
Se ele não passar pelo controller, o usuário entra na tela, altera um valor e sai da tela sem salvar, e ainda assim ele realiza o update no banco. :confused:

igomes

debugggggg, porque pelas forças das trevas com certeza não é.

peczenyj
Solucao aceita

http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html

Hibernate defines and supports the following object states:

Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

Persistent - a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements, or DELETE statements when an object should be made transient.

Detached - a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

agora que vc sabe que objetos gerenciados pelo Hibernate podem se salvar sozinhos, use patterns adequados para gerenciar as mudanças.

por exemplo

getSession().setFlushMode(FlushMode.MANUAL);

vai previnir que os objetos ‘se salvem’ e

getSession().clear();

vai descartar objetos na sessão

eu acho que vc deveria investir mais tempo em estudar como o Hibernate funciona.

javaflex

Hibernate tem vida própria mesmo, tem que domar ele. Segue o que @peczenyj falou. Essa ferramenta é extremamente complexa internamente, se precisa mesmo usá-la vai ter que dominar essa complexidade pra não se deparar com surpresas como esta que enfrenta.

Criado 19 de outubro de 2016
Ultima resposta 20 de out. de 2016
Respostas 5
Participantes 4