[resolvido] JPA: Recuperando o valor de uma chave primária com @GeneratedValue

1 resposta
FkJ

Tenho uma entidade com generator. Após persistir uma nova instância e fazer commit, o valor da chava primária continua com null (o entity manager continua aberto após a transação).

Fazendo um merge() consigo obter o valor da chave, porém esta é a forma mais indicada?

Obrigado,
Felipe

1 Resposta

FkJ

Depois de pesquisar bastante acabei achando na documentação do Hibernate como fazer o que preciso

You can reload an entity instance and it’s collections at any time using the em.refresh() operation. This is useful when database triggers are used to initialize some of the properties of the entity. Note that only the entity instance and its collections are refreshed unless you specify REFRESH as a cascade style of any associations:

em.persist(cat);
em.flush(); // force the SQL insert and triggers to run
em.refresh(cat); //re-read the state (after the trigger executes)

Criado 18 de abril de 2008
Ultima resposta 25 de abr. de 2008
Respostas 1
Participantes 1