1818
1919import javax .persistence .PersistenceException ;
2020
21+ import org .hibernate .Session ;
2122import org .hibernate .SessionFactory ;
2223import org .hibernate .exception .ConstraintViolationException ;
2324import org .junit .Before ;
2930import org .springframework .test .context .junit4 .orm .domain .DriversLicense ;
3031import org .springframework .test .context .junit4 .orm .domain .Person ;
3132import org .springframework .test .context .junit4 .orm .service .PersonService ;
33+ import org .springframework .transaction .annotation .Transactional ;
3234
3335import static org .junit .Assert .*;
3436import static org .springframework .test .transaction .TransactionTestUtils .*;
3840 * Hibernate.
3941 *
4042 * @author Sam Brannen
43+ * @author Juergen Hoeller
44+ * @author Vlad Mihalcea
4145 * @since 3.0
4246 */
4347@ ContextConfiguration
@@ -53,21 +57,14 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
5357 private SessionFactory sessionFactory ;
5458
5559
56- protected int countRowsInPersonTable () {
57- return countRowsInTable ("person" );
58- }
59-
60- protected void assertPersonCount (int expectedCount ) {
61- assertEquals ("Verifying number of rows in the 'person' table." , expectedCount , countRowsInPersonTable ());
62- }
63-
6460 @ Before
65- public void setUp () {
61+ public void setup () {
6662 assertInTransaction (true );
6763 assertNotNull ("PersonService should have been autowired." , personService );
6864 assertNotNull ("SessionFactory should have been autowired." , sessionFactory );
6965 }
7066
67+
7168 @ Test
7269 public void findSam () {
7370 Person sam = personService .findByName (SAM );
@@ -77,13 +74,25 @@ public void findSam() {
7774 assertEquals ("Verifying Sam's driver's license number" , Long .valueOf (1234 ), driversLicense .getNumber ());
7875 }
7976
77+ @ Test // SPR-16956
78+ @ Transactional (readOnly = true )
79+ public void findSamWithReadOnlySession () {
80+ Person sam = personService .findByName (SAM );
81+ sam .setName ("Vlad" );
82+ // By setting setDefaultReadOnly(true), the user can no longer modify any entity...
83+ Session session = sessionFactory .getCurrentSession ();
84+ session .flush ();
85+ session .refresh (sam );
86+ assertEquals ("Sam" , sam .getName ());
87+ }
88+
8089 @ Test
8190 public void saveJuergenWithDriversLicense () {
8291 DriversLicense driversLicense = new DriversLicense (2L , 2222L );
8392 Person juergen = new Person (JUERGEN , driversLicense );
84- int numRows = countRowsInPersonTable ( );
93+ int numRows = countRowsInTable ( "person" );
8594 personService .save (juergen );
86- assertPersonCount ( numRows + 1 );
95+ assertEquals ( "Verifying number of rows in the 'person' table." , numRows + 1 , countRowsInTable ( "person" ) );
8796 assertNotNull ("Should be able to save and retrieve Juergen" , personService .findByName (JUERGEN ));
8897 assertNotNull ("Juergen's ID should have been set" , juergen .getId ());
8998 }
@@ -93,13 +102,6 @@ public void saveJuergenWithNullDriversLicense() {
93102 personService .save (new Person (JUERGEN ));
94103 }
95104
96- private void updateSamWithNullDriversLicense () {
97- Person sam = personService .findByName (SAM );
98- assertNotNull ("Should be able to find Sam" , sam );
99- sam .setDriversLicense (null );
100- personService .save (sam );
101- }
102-
103105 @ Test
104106 // no expected exception!
105107 public void updateSamWithNullDriversLicenseWithoutSessionFlush () {
@@ -121,4 +123,11 @@ public void updateSamWithNullDriversLicenseWithSessionFlush() throws Throwable {
121123 }
122124 }
123125
126+ private void updateSamWithNullDriversLicense () {
127+ Person sam = personService .findByName (SAM );
128+ assertNotNull ("Should be able to find Sam" , sam );
129+ sam .setDriversLicense (null );
130+ personService .save (sam );
131+ }
132+
124133}
0 commit comments