Transactions

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.

@Entity
@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:

void method() {

    EntityManager em = ...;

    try {
        em.getTransaction().begin();
        jpaStreamer.stream(Film.class) (1)
            .filter(Film$.rating.equal("R")) (2)
            .forEach(f -> {
                f.setRentalRate(f.getRentalRate() + 1)); (3)
                em.merge(f); (4)
            }
        em.getTransaction().commit();
    } catch(Exception e) {
        em.getTransaction().rollback();
    }

}
1 Selects the entries from the Film-table
2 Filters out only the R-rated films
3 Increases the current rental rate with 1 dollar
4 Notifies the transaction of the update
The source code for this example can be found here.