Skip to content

Commit c05af4c

Browse files
committed
Upgrade to Hibernate 7.0. This breaks compatibility with save() and saveOrUpdate() methods, which must be replaced with persist (for async persist) or merge() (if id must be returned - returns the persisted object which must be swapped out with the unpersisted version)
1 parent b4029cc commit c05af4c

21 files changed

Lines changed: 147 additions & 148 deletions

File tree

guice/hibernate-testing/src/test/java/com/peterphi/std/guice/hibernatetest/DbunitModuleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public void test() throws Exception
4444
// Add some db contents
4545
try (HibernateTransaction tx = txutils.start().withAutoCommit())
4646
{
47-
dao.save(new SimpleEntity(1, "alice", new GroupEntity(1), new GroupEntity(2)));
48-
dao.save(new SimpleEntity(2, "bob"));
49-
dao.save(new SimpleEntity(3, "carol"));
50-
dao.save(new SimpleEntity(4, "dave"));
47+
dao.persist(new SimpleEntity(1, "alice", new GroupEntity(1), new GroupEntity(2)));
48+
dao.persist(new SimpleEntity(2, "bob"));
49+
dao.persist(new SimpleEntity(3, "carol"));
50+
dao.persist(new SimpleEntity(4, "dave"));
5151
}
5252

5353
// DB should now have 4 rows

guice/hibernate/src/main/java/com/peterphi/std/guice/database/dao/Dao.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,48 +143,37 @@ public interface Dao<T, ID extends Serializable>
143143
public void delete(T obj);
144144

145145
/**
146-
* Create or Update an item in the database
146+
* Update an existing item in the database
147147
*
148148
* @param obj
149149
* the entity
150150
*
151-
* @see org.hibernate.Session#saveOrUpdate(Object)
151+
* @see org.hibernate.Session#update(Object)
152152
*/
153-
public void saveOrUpdate(T obj);
153+
public void update(T obj);
154154

155155
/**
156-
* Save a new item in the database, returning its primary key
156+
* Merge an unmanaged entity into a (new or existing) managed entity
157157
*
158158
* @param obj
159159
* the entity
160160
*
161-
* @return the primary key of the newly saved object
161+
* @return a Hibernate entity version of obj
162162
*
163-
* @see org.hibernate.Session#save(Object)
163+
* @see org.hibernate.Session#merge(Object)
164164
*/
165-
public ID save(T obj);
165+
public T merge(T obj);
166166

167-
/**
168-
* Update an existing item in the database
169-
*
170-
* @param obj
171-
* the entity
172-
*
173-
* @see org.hibernate.Session#update(Object)
174-
*/
175-
public void update(T obj);
176167

177168
/**
178-
* Merge an unmanaged entity into a (new or existing) managed entity
169+
* Schedule asynchronous persistence of an unmanaged entity into a new managed entity
179170
*
180171
* @param obj
181172
* the entity
182173
*
183-
* @return a Hibernate entity version of obj
184-
*
185-
* @see org.hibernate.Session#merge(Object)
174+
* @see org.hibernate.Session#persist(Object)
186175
*/
187-
public T merge(T obj);
176+
void persist(T obj);
188177

189178

190179
/**

guice/hibernate/src/main/java/com/peterphi/std/guice/hibernate/dao/HibernateDao.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,40 +205,30 @@ public void deleteById(ID id)
205205
@Transactional
206206
public void delete(T obj)
207207
{
208-
getWriteSession().delete(obj);
208+
getWriteSession().remove(obj);
209209
}
210210

211211

212212
@Override
213213
@Transactional
214-
public void saveOrUpdate(T obj)
215-
{
216-
getWriteSession().saveOrUpdate(obj);
217-
}
218-
219-
220-
@SuppressWarnings("unchecked")
221-
@Override
222-
@Transactional
223-
public ID save(T obj)
214+
public void update(T obj)
224215
{
225-
return (ID) getWriteSession().save(obj);
216+
merge(obj);
226217
}
227218

228219

229220
@Override
230221
@Transactional
231-
public void update(T obj)
222+
public T merge(T obj)
232223
{
233-
getWriteSession().update(obj);
224+
return getWriteSession().merge(obj);
234225
}
235226

236-
237227
@Override
238228
@Transactional
239-
public T merge(T obj)
229+
public void persist(T obj)
240230
{
241-
return (T) getWriteSession().merge(obj);
231+
getWriteSession().persist(obj);
242232
}
243233

244234

guice/hibernate/src/main/java/com/peterphi/std/guice/hibernate/module/HibernateModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ public Configuration getHibernateConfiguration(GuiceConfig guiceConfig,
9292
validateHibernateProperties(guiceConfig, properties);
9393

9494
// Set up the hibernate Configuration
95-
Configuration config = new Configuration();
95+
Configuration config;
96+
try
97+
{
98+
config = new Configuration();
99+
}
100+
catch (Throwable t)
101+
{
102+
throw new RuntimeException("Unable to construct empty hibernate Configuration type! " + t.getMessage(), t);
103+
}
96104

97105
// Set up the interceptor
98106
properties.setProperty("hibernate.session_factory.statement_inspector",

guice/hibernate/src/test/java/com/peterphi/std/guice/hibernate/entitycollection/EntityCollectionTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -349,22 +349,22 @@ public List<Long> load()
349349
{
350350
ParentEntity p1 = new ParentEntity();
351351
p1.setCapacity(2);
352-
p1.setId(dao.save(p1));
352+
p1 = dao.merge(p1);
353353

354354
ChildEntity c1 = new ChildEntity();
355355
c1.setParent(p1);
356356
c1.setFlag(true);
357-
c1.setId(rDao.save(c1));
357+
c1 = rDao.merge(c1);
358358

359359
ChildEntity c2 = new ChildEntity();
360360
c2.setParent(p1);
361361
c2.setFlag(true);
362-
c2.setId(rDao.save(c2));
362+
c2 = rDao.merge(c2);
363363

364364
ChildEntity c3 = new ChildEntity();
365365
c3.setParent(p1);
366366
c3.setFlag(false);
367-
c3.setId(rDao.save(c3));
367+
c3 = rDao.merge(c3);
368368

369369
parentIds.add(p1.getId());
370370
}
@@ -373,22 +373,22 @@ public List<Long> load()
373373
{
374374
ParentEntity p2 = new ParentEntity();
375375
p2.setCapacity(2);
376-
p2.setId(dao.save(p2));
376+
p2 = dao.merge(p2);
377377

378378
ChildEntity c1 = new ChildEntity();
379379
c1.setParent(p2);
380380
c1.setFlag(true);
381-
c1.setId(rDao.save(c1));
381+
c1 = rDao.merge(c1);
382382

383383
ChildEntity c2 = new ChildEntity();
384384
c2.setParent(p2);
385385
c2.setFlag(true);
386-
c2.setId(rDao.save(c2));
386+
c2 = rDao.merge(c2);
387387

388388
ChildEntity c3 = new ChildEntity();
389389
c3.setParent(p2);
390390
c3.setFlag(false);
391-
c3.setId(rDao.save(c3));
391+
c3 = rDao.merge(c3);
392392

393393
parentIds.add(p2.getId());
394394
}
@@ -442,7 +442,7 @@ public void testFilledDb() throws Exception
442442
{
443443
p1 = new ParentEntity();
444444
p1.setCapacity(2);
445-
p1 = dao.getById(dao.save(p1));
445+
p1 = dao.merge(p1);
446446
}
447447

448448
List<Long> ids;
@@ -455,7 +455,7 @@ public void testFilledDb() throws Exception
455455
r.setParent(p1);
456456
r.setFlag(true);
457457

458-
rDao.save(r);
458+
rDao.persist(r);
459459
}
460460

461461
ids = dao.getIdsByQuery(query);
@@ -466,7 +466,7 @@ public void testFilledDb() throws Exception
466466
r.setParent(p1);
467467
r.setFlag(false);
468468

469-
rDao.save(r);
469+
rDao.persist(r);
470470
}
471471

472472
// One Q should match now
@@ -479,7 +479,7 @@ public void testFilledDb() throws Exception
479479
r.setParent(p1);
480480
r.setFlag(true);
481481

482-
rDao.save(r);
482+
rDao.persist(r);
483483
}
484484

485485
// Now none should match again

guice/hibernate/src/test/java/com/peterphi/std/guice/hibernate/largetable/LargeTableQueryTest.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public class LargeTableQueryTest
3535
@Transactional
3636
public void testSearchForPrimaryKeyWorks()
3737
{
38-
dao.save(new LargeTableSimplePKEntity("Alice"));
39-
dao.save(new LargeTableSimplePKEntity("Bob"));
40-
dao.save(new LargeTableSimplePKEntity("Carol"));
41-
dao.save(new LargeTableSimplePKEntity("Dave"));
42-
dao.save(new LargeTableSimplePKEntity("Eve"));
38+
dao.persist(new LargeTableSimplePKEntity("Alice"));
39+
dao.persist(new LargeTableSimplePKEntity("Bob"));
40+
dao.persist(new LargeTableSimplePKEntity("Carol"));
41+
dao.persist(new LargeTableSimplePKEntity("Dave"));
42+
dao.persist(new LargeTableSimplePKEntity("Eve"));
4343

4444
// Try a regular query
4545
assertEquals("custom-coded count should return 2", 2, dao.countWithAInName());
@@ -61,11 +61,11 @@ public void testSearchForPrimaryKeyWorks()
6161
@Transactional
6262
public void testSearchWorks()
6363
{
64-
dao.save(new LargeTableSimplePKEntity("Alice"));
65-
dao.save(new LargeTableSimplePKEntity("Bob"));
66-
dao.save(new LargeTableSimplePKEntity("Carol"));
67-
dao.save(new LargeTableSimplePKEntity("Dave"));
68-
dao.save(new LargeTableSimplePKEntity("Eve"));
64+
dao.persist(new LargeTableSimplePKEntity("Alice"));
65+
dao.persist(new LargeTableSimplePKEntity("Bob"));
66+
dao.persist(new LargeTableSimplePKEntity("Carol"));
67+
dao.persist(new LargeTableSimplePKEntity("Dave"));
68+
dao.persist(new LargeTableSimplePKEntity("Eve"));
6969

7070
// Try a regular query
7171
assertEquals(2, dao.countWithAInName());
@@ -82,11 +82,11 @@ public void testSearchWorks()
8282
@Transactional
8383
public void testSearchWorksWithOrder()
8484
{
85-
dao.save(new LargeTableSimplePKEntity("Alice"));
86-
dao.save(new LargeTableSimplePKEntity("Bob"));
87-
dao.save(new LargeTableSimplePKEntity("Carol"));
88-
dao.save(new LargeTableSimplePKEntity("Dave"));
89-
dao.save(new LargeTableSimplePKEntity("Eve"));
85+
dao.persist(new LargeTableSimplePKEntity("Alice"));
86+
dao.persist(new LargeTableSimplePKEntity("Bob"));
87+
dao.persist(new LargeTableSimplePKEntity("Carol"));
88+
dao.persist(new LargeTableSimplePKEntity("Dave"));
89+
dao.persist(new LargeTableSimplePKEntity("Eve"));
9090

9191
// Now try a web query
9292

@@ -108,11 +108,11 @@ public void testSearchWorksWithOrder()
108108
@Transactional
109109
public void testSearchComplexPK()
110110
{
111-
complexPKdao.save(new LargeTableComplexPKEntity("Alice"));
112-
complexPKdao.save(new LargeTableComplexPKEntity("Bob"));
113-
complexPKdao.save(new LargeTableComplexPKEntity("Carol"));
114-
complexPKdao.save(new LargeTableComplexPKEntity("Dave"));
115-
complexPKdao.save(new LargeTableComplexPKEntity("Eve"));
111+
complexPKdao.persist(new LargeTableComplexPKEntity("Alice"));
112+
complexPKdao.persist(new LargeTableComplexPKEntity("Bob"));
113+
complexPKdao.persist(new LargeTableComplexPKEntity("Carol"));
114+
complexPKdao.persist(new LargeTableComplexPKEntity("Dave"));
115+
complexPKdao.persist(new LargeTableComplexPKEntity("Eve"));
116116

117117
assertEquals(2, complexPKdao.findByUriQuery(new WebQuery().contains("name", "a")).getList().size());
118118
}
@@ -125,11 +125,11 @@ public void testSearchComplexPK()
125125
@Transactional
126126
public void testSearchComplexPKIDs()
127127
{
128-
complexPKdao.save(new LargeTableComplexPKEntity("Alice"));
129-
complexPKdao.save(new LargeTableComplexPKEntity("Bob"));
130-
complexPKdao.save(new LargeTableComplexPKEntity("Carol"));
131-
complexPKdao.save(new LargeTableComplexPKEntity("Dave"));
132-
complexPKdao.save(new LargeTableComplexPKEntity("Eve"));
128+
complexPKdao.persist(new LargeTableComplexPKEntity("Alice"));
129+
complexPKdao.persist(new LargeTableComplexPKEntity("Bob"));
130+
complexPKdao.persist(new LargeTableComplexPKEntity("Carol"));
131+
complexPKdao.persist(new LargeTableComplexPKEntity("Dave"));
132+
complexPKdao.persist(new LargeTableComplexPKEntity("Eve"));
133133

134134
final List<SomePrimaryKey> results = complexPKdao.findIdsByUriQuery(new WebQuery().contains("name", "a")).getList();
135135

guice/hibernate/src/test/java/com/peterphi/std/guice/hibernate/usertype/datetime/DateTimeUserTypeVersionFieldTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.junit.runner.RunWith;
1010

1111
import static org.junit.Assert.assertEquals;
12-
import static org.junit.Assert.assertNotSame;
12+
import static org.junit.Assert.assertNotEquals;
1313
import static org.junit.Assert.assertTrue;
1414

1515
/**
@@ -31,17 +31,18 @@ public void testThatDateTimeVersionGetsUpdatedOnStore() throws Exception
3131
ObjWithDateTimeVersionField obj = new ObjWithDateTimeVersionField();
3232
final DateTime initialVersion = obj.getLastUpdated();
3333

34-
final Long id = dao.save(obj);
35-
36-
obj = dao.getById(id);
34+
obj = dao.merge(obj);
3735

3836
assertEquals("Version should be unmodified for initial store", initialVersion, obj.getLastUpdated());
3937

38+
obj.setSomeString("something else");
4039
dao.update(obj);
4140

41+
obj = dao.getById(obj.getId());
42+
4243
final DateTime nextVersion = obj.getLastUpdated();
4344

44-
assertNotSame("Version should be changed after update", initialVersion, nextVersion);
45+
assertNotEquals("Version should be changed after update", initialVersion, nextVersion);
4546
assertTrue("Version date for later update should be later than original time", initialVersion.isBefore(nextVersion));
4647
}
4748
}

guice/hibernate/src/test/java/com/peterphi/std/guice/hibernate/usertype/datetime/ObjWithDateTimeVersionField.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class ObjWithDateTimeVersionField
2020
@Column(name = "updated_ts", nullable = false)
2121
private DateTime lastUpdated = DateTime.now();
2222

23+
@Column(name="some_string",nullable=true)
24+
private String someString;
25+
2326

2427
public Long getId()
2528
{
@@ -43,4 +46,16 @@ public void setLastUpdated(final DateTime lastUpdated)
4346
{
4447
this.lastUpdated = lastUpdated;
4548
}
49+
50+
51+
public String getSomeString()
52+
{
53+
return someString;
54+
}
55+
56+
57+
public void setSomeString(final String someString)
58+
{
59+
this.someString = someString;
60+
}
4661
}

guice/hibernate/src/test/java/com/peterphi/std/guice/hibernate/usertype/localdate/LocalDateUserTypeTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public void testThatDateTimeVersionGetsUpdatedOnStore() throws Exception
2525
LocalDateEntity obj = new LocalDateEntity();
2626
obj.someDate = new LocalDate("2001-01-01");
2727

28-
final Long id = dao.save(obj);
29-
30-
obj = dao.getById(id);
28+
obj = dao.merge(obj);
3129

3230
assertEquals(new LocalDate("2001-01-01"), obj.someDate);
3331
assertNull(obj.someNullDate);

0 commit comments

Comments
 (0)