0

I got a table with a string field and try to update this field and the merge the update to the database via EntityManager. This is done and without exceptio but I see no effect on the database and ran out of ideas what's up there. Hope somebody has an idea. In the below exampl match != null and with both print statements I get the expected value written to my log. But the merge within the transaction has no impact on the database even though the commit terminated.

@Named
@SessionScoped
@ManagedBean
public class LoginController implements Serializable {

    @PersistenceUnit
    private EntityManagerFactory emf;

    public String mymethod(){
        ...
        match.setPwdResetId(rs);
        System.out.println("reset it is now "+match.getPwdResetId());
        try{
            ut.begin();
            emf.createEntityManager().merge(match);
            ut.commit();
        }
        catch(Exception e){
            FacesMessage m2 = new FacesMessage("Values could not be saved. ");
            FacesContext.getCurrentInstance().addMessage("ResetForm", m2);
            System.out.println("exceptio persisting "+e);
            return "message.jsf";
        }  
        System.out.println("reset it is now2 "+match.getPwdResetId());
TomFree
  • 1,091
  • 3
  • 17
  • 31
  • can you detach the entity and then merge it? – Apostolos Jul 20 '16 at 19:53
  • Yes! How did u know? After the detach I could even merge and now see it in the database. Would love to know the reasons and your thinking to learn. Thanks a lot – TomFree Jul 20 '16 at 20:36
  • i added an answer to accept it along with the merge part of the jpa specification and a link to the whole specification pdf. have fun reading :) – Apostolos Jul 21 '16 at 06:08

1 Answers1

0

Please detach the entity and try again. You can read more at the jpa 2 specification. I post a sample for the merge operation:

The semantics of the merge operation applied to an entity X are as follows:

  • If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.

  • If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity
    instance X'.

  • If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will
    fail).

  • If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by
    relationships from X if these relationships have been annotated with
    the cascade element value cascade=MERGE or cascade=ALL annotation.

  • For all entities Y referenced by relationships from X having the cascade element value cascade=MERGE or cascade=ALL, Y is merged
    recursively as Y'. For all such Y referenced by X, X' is set to
    reference Y'. (Note that if X is managed then X is the same object as X'.)

  • If X is an entity merged to X', with a reference to another entity Y, where cascade=MERGE or cascade=ALL is not specified, then
    navigation of the same association from X' yields a reference to a
    managed object Y' with the same persistent identity as Y.

The persistence provider must not merge fields marked LAZY that have not been fetched: it must ignore such fields when merging. Any Version columns used by the entity must be checked by the persistence runtime implementation during the merge operation and/or at flush or commit time. In the absence of Version columns there is no additional version checking done by the persistence provider runtime during the merge operation.

you can find the whole pdf here

Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • Thanks. I don't really get that. What's the reason that a managed entity is ignored by merged? Is merge the wrong command for an SQL level update query? Seems odd that the proper way is detaching and then merging or am I not getting the concept? – TomFree Jul 23 '16 at 09:30