-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AutoFlushTest.java
221 lines (183 loc) · 6.15 KB
/
AutoFlushTest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.userguide.flush;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.userguide.util.TransactionUtil.doInJPA;
import static org.junit.Assert.assertTrue;
/**
* @author Vlad Mihalcea
*/
public class AutoFlushTest extends BaseEntityManagerFunctionalTestCase {
private static final Logger log = Logger.getLogger( AutoFlushTest.class );
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Advertisement.class
};
}
@Test
public void testFlushAutoCommit() {
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
//tag::flushing-auto-flush-commit-example[]
entityManager = entityManagerFactory().createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
Person person = new Person( "John Doe" );
entityManager.persist( person );
log.info( "Entity is in persisted state" );
txn.commit();
//end::flushing-auto-flush-commit-example[]
} catch (RuntimeException e) {
if ( txn != null && txn.isActive()) txn.rollback();
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
}
@Test
public void testFlushAutoJPQL() {
doInJPA( this::entityManagerFactory, entityManager -> {
log.info( "testFlushAutoJPQL" );
//tag::flushing-auto-flush-jpql-example[]
Person person = new Person( "John Doe" );
entityManager.persist( person );
entityManager.createQuery( "select p from Advertisement p" ).getResultList();
entityManager.createQuery( "select p from Person p" ).getResultList();
//end::flushing-auto-flush-jpql-example[]
} );
}
@Test
public void testFlushAutoJPQLOverlap() {
doInJPA( this::entityManagerFactory, entityManager -> {
log.info( "testFlushAutoJPQLOverlap" );
//tag::flushing-auto-flush-jpql-overlap-example[]
Person person = new Person( "John Doe" );
entityManager.persist( person );
entityManager.createQuery( "select p from Person p" ).getResultList();
//end::flushing-auto-flush-jpql-overlap-example[]
} );
}
@Test
public void testFlushAutoSQL() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();;
} );
doInJPA( this::entityManagerFactory, entityManager -> {
log.info( "testFlushAutoSQL" );
//tag::flushing-auto-flush-sql-example[]
assertTrue(((Number) entityManager
.createNativeQuery( "select count(*) from Person")
.getSingleResult()).intValue() == 0 );
Person person = new Person( "John Doe" );
entityManager.persist( person );
assertTrue(((Number) entityManager
.createNativeQuery( "select count(*) from Person")
.getSingleResult()).intValue() == 1 );
//end::flushing-auto-flush-sql-example[]
} );
}
@Test
public void testFlushAutoSQLNativeSession() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();;
} );
doInJPA( this::entityManagerFactory, entityManager -> {
log.info( "testFlushAutoSQLNativeSession" );
//tag::flushing-auto-flush-sql-native-example[]
assertTrue(((Number) entityManager
.createNativeQuery( "select count(*) from Person")
.getSingleResult()).intValue() == 0 );
Person person = new Person( "John Doe" );
entityManager.persist( person );
Session session = entityManager.unwrap(Session.class);
// for this to work, the Session/EntityManager must be put into COMMIT FlushMode
// - this is a change since 5.2 to account for merging EntityManager functionality
// directly into Session. Flushing would be the JPA-spec compliant behavior,
// so we know do that by default.
session.setFlushMode( FlushModeType.COMMIT );
// or using Hibernate's FlushMode enum
//session.setHibernateFlushMode( FlushMode.COMMIT );
assertTrue(((Number) session
.createSQLQuery( "select count(*) from Person")
.uniqueResult()).intValue() == 0 );
//end::flushing-auto-flush-sql-native-example[\]
} );
}
@Test
public void testFlushAutoSQLSynchronization() {
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();;
} );
doInJPA( this::entityManagerFactory, entityManager -> {
log.info( "testFlushAutoSQLSynchronization" );
//tag::flushing-auto-flush-sql-synchronization-example[]
assertTrue(((Number) entityManager
.createNativeQuery( "select count(*) from Person")
.getSingleResult()).intValue() == 0 );
Person person = new Person( "John Doe" );
entityManager.persist( person );
Session session = entityManager.unwrap( Session.class );
assertTrue(((Number) session
.createSQLQuery( "select count(*) from Person")
.addSynchronizedEntityClass( Person.class )
.uniqueResult()).intValue() == 1 );
//end::flushing-auto-flush-sql-synchronization-example[]
} );
}
//tag::flushing-auto-flush-jpql-entity-example[]
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
private String name;
public Person() {}
public Person(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
}
@Entity(name = "Advertisement")
public static class Advertisement {
@Id
@GeneratedValue
private Long id;
private String title;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
//end::flushing-auto-flush-jpql-entity-example[]
}