I have a Java/Spring web application that runs on Wildfly 10. I configured JPA and was wondering what a common approach is to do update and delete statements. Say for example an httprequest enters the server to show all the details about a Person
record. I will use the entity manager to find this record
class PersonDao
{
@PersistenceContent
entityManager entityManager
public Person findPerson(int id)
{
assert(id >= 0); //pseudocode
Person p = this.entityManager.find(Person.class,id);
assert(p != null); //pseudocode
return p;
}
}
Then the details of this person are shown on the screen, the request is finished and the thread gone. The attached Person record in the entity manager is no longer accessible in my code.
Some time later, a new request is launched by the user to update the age of the person. Currently in my Dao class I ualways re-find the record to so I'm sure it's present in my persistence context, but it seems to be redundant and tedious. I would like to know a better way to implement this:
public void updatePersonAge(int id, int newAge)
{
assert(newAge >= 0)
Person p = this.findPerson(id);
p.setAge(newAge);
this.entityManager.persist(p);
}