0

I am very new to JPA - I have this look up that will find the user on the login table.. now if I recieve new information like the user has changed their password, how do I update the entry?


so I find the user - lets say via their email address.

TblLogin acc = tblLoginRepository.findByEmail(email);

I've seen methods invoking "getTransaction()"

http://www.objectdb.com/java/jpa/persistence/update

so something like this?

  tblLoginRepository.getTransaction().begin();
  acc.setPassword("test2");
  tblLoginRepository.getTransaction().commit();

but then do I just do something like this - and that is it?

   TblLogin acc = tblLoginRepository.findByEmail(email);
   acc.setPassword("newpassword");

^ and that is that - nothing else - entry is updated?

for when the user registers -- I do a saveAndFlush? I don't have to do anything else for the editing of an entry?

        TblLogin newAcc = tblLoginRepository.saveAndFlush(new TblLogin(
                    email,
                    password,
                    pin
                ));
The Old County
  • 89
  • 13
  • 59
  • 129

1 Answers1

0

There is heavy usage of terminology when using JPA. You should have a basic understanding of the basic parts like Entity, EntityManager, Session, Transaction etc. The getting-started-with-spring-data-jpa would help you. Using for example the@Transactional makes sure that a decorated method with this annotation is running in a transaction.

You should also check out spring-boot-hibernate-session-question. Using the spring-boot's repository, you could have something like:

TblLogin acc = tblLoginRepository.findByEmail(email);
acc.setPassword("newpassword");
tblLoginRepository.save(acc);

The implementation of the above mentioned save() looks like:

public <S extends T> S save(S entity) {

    if (entityInformation.isNew(entity)) {
        em.persist(entity);
        return entity;
    } else {
        return em.merge(entity);
    }
}

Next step would be to learn the difference between em.persist() and em.flush() as noted here.

I hope that this answer would help you!

Eirini Graonidou
  • 1,506
  • 16
  • 24