Persisting an object using JPA
Up to this point, we configured and used the persistence layer to connect and retrieve information from the database but have not tried to store data into it. This is a pretty straightforward procedure that involves the Entity Manager component—the same one we used in Chapter 3, Java EE Basics – Persistence, Query, and Presentation,—to read data from MySQL, and also a transaction, which is something we haven't seen yet.
The concept is pretty widespread nowadays, so there's no need to have painstaking explanations here but just a quick refresher. We use transactions to coordinate efforts on disparate resources—which obviously must support it—so we have a consistent unit of work. The ACID concept (atomicity, consistency, isolation, and durability) states the primary attributes that must be observed when a transaction is used:
All or none of the participating resources are committed
If an error happens, no resource is updated
The changes being made inside a transaction...