Transactions are an essential part of JPA that determines when modifications to the data is synchronised with the underlying database. JPAstreamer can assist with the modifications while the actual transactions are handled using standard JPA. Below is an example of a JPA transaction that updates a selection of entries in a table.

The entity to operate on is called Film that, among others, declare the fields rating and rentalRate.

@Table(name = "film", schema = "sakila")
public class Film {


    @Column(name = "rating", columnDefinition = "enum('G','PG','PG-13','R','NC-17')")
    private String rating;

    @Column(name = "rental_rate", columnDefinition = "decimal(4,2)")
    private Float rentalRate;



The transaction below performs a one dollar increase of the rental rate of every R-rated film (for adults only) in a film-table:

final EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila"); (1)
final EntityManager em = emf.createEntityManager();
final JPAStreamer jpaStreamer = JPAStreamer.of(emf); (2)

    try {
        em.getTransaction().begin(); (3)
            .filter(Film$.rating.equal("R")) (4)
            .forEach(f -> {
                f.setRentalRate(f.getRentalRate() + 1)); (5)
                em.merge(f); (6)
    } catch(Exception e) {
1 "sakila" refers to the name of the persistence unit
2 The EntityManagerFactory can be provided to the JPAStreamer builder
3 Selects the entries from the Film-table
4 Filters out only the R-rated films
5 Increases the current rental rate with 1 dollar
6 Notifies the transaction of the update
The source code for this example can be found here.