HHH-14821 - Test and fix for issue

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2024-01-03 23:26:38 +01:00 committed by Christian Beikov
parent 718f26022e
commit f72bea49cb
2 changed files with 36 additions and 0 deletions

View File

@ -92,6 +92,10 @@ public class DefaultRefreshEventListener implements RefreshEventListener {
//refresh() does not pass an entityName
persister = source.getEntityPersister( event.getEntityName(), object );
id = persister.getIdentifier( object, event.getSession() );
if ( id == null ) {
throw new HibernateException( "attempted to refresh an instance that is not part of the persistence context yet: "
+ infoString( persister, id, source.getFactory() ));
}
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
"Refreshing transient {0}",

View File

@ -6,9 +6,12 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType;
@ -27,6 +30,7 @@ import jakarta.persistence.Table;
RefreshTest.RealmEntity.class,
RefreshTest.RealmAttributeEntity.class,
RefreshTest.ComponentEntity.class,
RefreshTest.SimpleEntity.class
}
)
@SessionFactory
@ -74,6 +78,34 @@ public class RefreshTest {
);
}
@Test
public void testRefreshWithNullId(SessionFactoryScope scope) {
Assertions.assertThrows(
HibernateException.class,
() -> {
scope.inTransaction(
session -> {
SimpleEntity se = new SimpleEntity();
se.setName( "a" );
session.refresh( se );
}
);
},
"attempted to refresh an instance that is not part of the persistence context yet: [org.hibernate.orm.test.refresh.RefreshTest$SimpleEntity#<null>]"
);
}
@Entity(name= "SimpleEntity" )
public static class SimpleEntity {
@Id
Long id;
String name;
public void setName(String name) {
this.name = name;
}
}
@Table(name="REALM")
@Entity(name = "RealmEntity")
public static class RealmEntity {