6
6
import java .time .LocalDateTime ;
7
7
import java .time .temporal .ChronoUnit ;
8
8
9
- import jakarta .persistence .EntityManagerFactory ;
10
- import jakarta .persistence .Query ;
11
- import jakarta .persistence .TypedQuery ;
12
-
13
9
import org .junit .jupiter .api .BeforeAll ;
14
10
import org .junit .jupiter .api .BeforeEach ;
15
11
import org .junit .jupiter .api .Test ;
16
12
17
13
import info .unterrainer .commons .rdbutils .exceptions .RdbUtilException ;
18
14
import info .unterrainer .commons .rdbutils .jpas .TestJpa ;
15
+ import jakarta .persistence .EntityManagerFactory ;
16
+ import jakarta .persistence .Query ;
17
+ import jakarta .persistence .TypedQuery ;
19
18
import lombok .extern .slf4j .Slf4j ;
20
19
21
20
@ Slf4j
@@ -51,6 +50,21 @@ public void persistingAndReadingEntityWorks() {
51
50
assertThat (jpa .getEditedOn ()).isEqualTo (testJpa .getEditedOn ().truncatedTo (ChronoUnit .MICROS ));
52
51
}
53
52
53
+ @ Test
54
+ public void persistingAndUpdatingAndReadingEntityWorks () {
55
+ persistTestEntity (testJpa );
56
+
57
+ testJpa .setMessage ("new message" );
58
+ testJpa .setCreatedOn (LocalDateTime .now ().plus (1 , ChronoUnit .SECONDS ));
59
+ testJpa .setEditedOn (LocalDateTime .now ().plus (14 , ChronoUnit .SECONDS ));
60
+ updateTestEntity (testJpa );
61
+
62
+ TestJpa jpa = selectFirstTestEntity ();
63
+ assertThat (jpa .getMessage ()).isEqualTo (testJpa .getMessage ());
64
+ assertEquals (jpa .getCreatedOn (), testJpa .getCreatedOn ().truncatedTo (ChronoUnit .MICROS ));
65
+ assertThat (jpa .getEditedOn ()).isEqualTo (testJpa .getEditedOn ().truncatedTo (ChronoUnit .MICROS ));
66
+ }
67
+
54
68
private int deleteTestTable () {
55
69
return Transactions .withNewTransactionReturning (emf , em -> {
56
70
Query q = em .createQuery (String .format ("DELETE FROM %s AS o" , TestJpa .class .getSimpleName ()));
@@ -66,6 +80,12 @@ private void persistTestEntity(final TestJpa jpa) {
66
80
});
67
81
}
68
82
83
+ private void updateTestEntity (final TestJpa jpa ) {
84
+ Transactions .withNewTransaction (emf , em -> {
85
+ em .merge (jpa );
86
+ });
87
+ }
88
+
69
89
private TestJpa selectFirstTestEntity () {
70
90
return Transactions .withNewTransactionReturning (emf , em -> {
71
91
TypedQuery <TestJpa > q = em
0 commit comments