Attempt to update registry ID not allowed with JPA and EclipseLink

4

How do I allow a record ID to be updated with JPA and EclipseLink?

The following exception is thrown when I try to update the ID:

  

Caused by: Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException   Exception Description: The attribute [idnotificacaoTipoEnvio] of class [br.com.ko.NotificacaoTipoEnvio] is mapped to a primary key column in the database. Updates are not allowed.

The attribute is mapped as follows:

@Id
@Basic(optional = false)
@NotNull
@Column(name = "idnotificacao_tipo_envio")
private Integer idnotificacaoTipoEnvio;

The method that does the update is as follows:

public void edit(T entity) {
    getEntityManager().getTransaction().begin();
    getEntityManager().merge(entity);
    getEntityManager().getTransaction().commit();
}
    
asked by anonymous 29.05.2014 / 19:07

1 answer

5

There are two alternatives (which I know) for this situation.

Method XGH

  • Retrieve the connection object ( Connection )
  • Give an update via JDBC
  • Run find() to retrieve the new object
  • Method POG

  • Execute a remove() to exclude the entity from EntityManager
  • Change the ID
  • Execute a persist() to insert the entity into the database as if it were a new one
  • Important Considerations

    Obviously the above solutions are not ideas. If changing this field is something recurring, then it should not be the PK of the table.

    Another alternative would be to reshape the bank with a sequential ID (auto increment) and that field that varies a normal field with a unique constraint.

    It makes more sense, right?

        
    29.05.2014 / 20:52