Sobes.tech
Junior — Middle

What data will be stored in the database if you call persist or save methods for a Person object and then update the age within a transaction?

sobes.tech AI

Answer from AI

If you call the persist or save method for a Person object within a transaction, the object becomes managed (attached) in the persistence context. The object's data will be saved to the database upon committing the transaction.

If after calling persist or save within the same transaction you change the age field (for example, person.setAge(newAge)), then upon committing the transaction, the updated age value will be written to the database. This happens because ORM (e.g., Hibernate) tracks changes to managed entities and automatically synchronizes them with the database.

Example in Hibernate:

entityManager.getTransaction().begin();
entityManager.persist(person); // object becomes managed
person.setAge(30); // change the field
entityManager.getTransaction().commit(); // changes are saved to the database

In summary: the database will store the state of the Person object with the updated age.

What data will be stored in the database if you call… - sobes.tech