11/*
2- * Copyright (C) 2011, 2012 XStream Committers.
2+ * Copyright (C) 2011, 2012, 2025 XStream Committers.
33 * All rights reserved.
44 *
55 * The software in this package is published under the terms of the BSD
1717import acceptance .hibernate .reference .Person ;
1818import acceptance .hibernate .reference .Site ;
1919
20+ import java .lang .reflect .Method ;
21+
2022import org .hibernate .Session ;
2123import org .hibernate .proxy .HibernateProxy ;
2224
2729 */
2830public class HibernateReferenceTest extends AbstractHibernateAcceptanceTest {
2931
32+ private final static Method persist ;
33+ private final static Method remove ;
34+ static {
35+ Method method ;
36+ try {
37+ method = Session .class .getMethod ("save" , new Class []{Object .class });
38+ } catch (Exception e1 ) {
39+ try {
40+ // Available since Hibernate 6.0
41+ method = Session .class .getMethod ("persist" , new Class []{Object .class });
42+ } catch (Exception e2 ) {
43+ throw new ExceptionInInitializerError ("Cannot find method in "
44+ + Session .class .getName ()
45+ + " to persist/save objects" );
46+ }
47+ }
48+ persist = method ;
49+ try {
50+ method = Session .class .getMethod ("delete" , new Class []{Object .class });
51+ } catch (Exception e1 ) {
52+ try {
53+ // Available since Hibernate 6.0
54+ method = Session .class .getMethod ("remove" , new Class []{Object .class });
55+ } catch (Exception e2 ) {
56+ throw new ExceptionInInitializerError ("Cannot find method in "
57+ + Session .class .getName ()
58+ + " to remove/delete objects" );
59+ }
60+ }
61+ remove = method ;
62+ }
63+
3064 protected void setUp () throws Exception {
3165 super .setUp ();
3266 // don't write the primary keys in this test
@@ -42,14 +76,14 @@ protected void tearDown() {
4276 final Session session = getSessionFactory ().getCurrentSession ();
4377 session .beginTransaction ();
4478 final Division div = (Division )session .createQuery ("from Division" ).uniqueResult ();
45- session . delete ( div );
79+ remove . invoke ( session , new Object []{ div } );
4680 session .getTransaction ().commit ();
47- } catch (RuntimeException e ) {
81+ } catch (Exception e ) {
4882 e .printStackTrace ();
4983 }
5084 }
5185
52- public void testObjectGraphWithReferences () {
86+ public void testObjectGraphWithReferences () throws Exception {
5387 final Division memory = setupNonpersistentDivision ();
5488 final Division persisted = setupPersistentDivision ();
5589
@@ -66,7 +100,7 @@ public void testObjectGraphWithReferences() {
66100 assertEquals (expectedXml , loadedXml );
67101 }
68102
69- public void testLazyProxyWithReferences () {
103+ public void testLazyProxyWithReferences () throws Exception {
70104 setupPersistentDivision ();
71105
72106 final Session session = getSessionFactory ().getCurrentSession ();
@@ -108,21 +142,18 @@ public void testLazyProxyWithReferences() {
108142 /**
109143 * Create the object within a Hibernate session and persist it.
110144 */
111- private Division setupPersistentDivision () {
145+ private Division setupPersistentDivision () throws Exception {
112146 final Session session = getSessionFactory ().getCurrentSession ();
113147 session .beginTransaction ();
114148 final Division div = new Division ("Div1" );
115149 final Department dep = new Department ("Dep1" , div );
116150 final Site site = new Site ("Site1" );
117- /*
118- * This save is necessitated by the fact that Hibernate's transitive persistence is
119- * depth-first and does not do a full graph analysis. Therefore it would be possible for
120- * Hibernate to try to save the person record before the site record, which would throw
121- * an error if the person.site FK is non-nullable.
122- */
123- session .save (site );
151+ /* This save is necessitated by the fact that Hibernate's transitive persistence is depth-first and does not do
152+ * a full graph analysis. Therefore it would be possible for Hibernate to try to save the person record before
153+ * the site record, which would throw an error if the person.site FK is non-nullable. */
154+ persist .invoke (session , new Object []{site });
124155 new Person ("Tom" , dep , site );
125- session . save ( div );
156+ persist . invoke ( session , new Object []{ div } );
126157 session .flush ();
127158 session .getTransaction ().commit ();
128159 return div ;
0 commit comments