HHH-14821 - Test and fix for issue
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
parent
0721629a19
commit
363d896f72
|
@ -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}",
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue