/
TransactionDemo.java
69 lines (53 loc) · 2.39 KB
/
TransactionDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.speedment.jpastreamer.demo.hibernate;
import com.speedment.jpastreamer.application.JPAStreamer;
import com.speedment.jpastreamer.demo.hibernate.model.Film;
import com.speedment.jpastreamer.demo.hibernate.model.Film$;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
/**
* 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.
*
* This example demonstrates a JPA transaction that updates the rental rate of a selection of films.
*/
public class TransactionDemo {
public static void main(String[] args) {
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila");
JPAStreamer jpaStreamer = JPAStreamer.of(emf);
System.out.println("Rental rates before raise:");
jpaStreamer.stream(Film.class)
.filter(Film$.rating.equal("R"))
.sorted()
.limit(5)
.forEach(f -> System.out.format("The rental rate for %s is $%f.\n", f.getTitle(), f.getRentalRate()));
final EntityManager em = emf.createEntityManager();
updateRentalRates(jpaStreamer, em);
System.out.println("Rental rates after the raise:");
jpaStreamer.stream(Film.class)
.filter(Film$.rating.equal("R"))
.sorted()
.limit(5)
.forEach(f -> System.out.format("The rental rate for %s is $%f.\n", f.getTitle(), f.getRentalRate()));
jpaStreamer.close();
em.close();
emf.close();
}
/* Performs a transaction that increases the rental rate of every R-rated film (for adults only) with one dollar */
private static void updateRentalRates(final JPAStreamer jpaStreamer, final EntityManager em) {
try {
em.getTransaction().begin();
jpaStreamer.stream(Film.class)
.filter(Film$.rating.equal("R"))
.forEach(f -> {
f.setRentalRate(f.getRentalRate() + 1);
em.merge(f);
}
);
em.getTransaction().commit();
} catch(Exception e) {
e.printStackTrace();
em.getTransaction().rollback();
}
}
}