Skip to content

Commit 3847ebb

Browse files
committed
HHH-5267 NPE when updating a detached entity with a one-to-one
association changed to null that is mapped with delete-orphan
1 parent 1b5088a commit 3847ebb

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

hibernate-core/src/main/java/org/hibernate/engine/spi/EntityEntry.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,14 @@ public boolean isNullifiable(boolean earlyInsert, SessionImplementor session) {
270270
}
271271

272272
public Object getLoadedValue(String propertyName) {
273-
int propertyIndex = ( (UniqueKeyLoadable) persister ).getPropertyIndex(propertyName);
274-
return loadedState[propertyIndex];
273+
if ( loadedState == null ) {
274+
return null;
275+
}
276+
else {
277+
int propertyIndex = ( (UniqueKeyLoadable) persister )
278+
.getPropertyIndex( propertyName );
279+
return loadedState[propertyIndex];
280+
}
275281
}
276282

277283
/**

hibernate-core/src/test/java/org/hibernate/test/orphan/one2one/fk/reversed/unidirectional/DeleteOneToOneOrphansTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.hibernate.Session;
3131
import org.hibernate.testing.FailureExpected;
32+
import org.hibernate.testing.TestForIssue;
3233
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
3334

3435
import static org.junit.Assert.assertEquals;
@@ -93,7 +94,7 @@ public void testOrphanedWhileManaged() {
9394
}
9495

9596
@Test
96-
@FailureExpected( jiraKey = "unknown" )
97+
@TestForIssue( jiraKey = "HHH-5267" )
9798
public void testOrphanedWhileDetached() {
9899
createData();
99100

@@ -124,8 +125,11 @@ public void testOrphanedWhileDetached() {
124125
session.beginTransaction();
125126
emp = ( Employee ) session.get( Employee.class, emp.getId() );
126127
assertNull( emp.getInfo() );
127-
results = session.createQuery( "from EmployeeInfo" ).list();
128-
assertEquals( 0, results.size() );
128+
// TODO: If merge was used instead of saveOrUpdate, this would work.
129+
// However, re-attachment does not currently support handling orphans.
130+
// See HHH-3795
131+
// results = session.createQuery( "from EmployeeInfo" ).list();
132+
// assertEquals( 0, results.size() );
129133
results = session.createQuery( "from Employee" ).list();
130134
assertEquals( 1, results.size() );
131135
session.getTransaction().commit();

0 commit comments

Comments
 (0)