Skip to content

Commit

Permalink
HHH-14821 - Test and fix for issue
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat authored and beikov committed Jan 4, 2024
1 parent 718f260 commit f72bea4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ private static void refresh(RefreshEvent event, RefreshContext refreshedAlready,
//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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
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;
Expand All @@ -27,6 +30,7 @@
RefreshTest.RealmEntity.class,
RefreshTest.RealmAttributeEntity.class,
RefreshTest.ComponentEntity.class,
RefreshTest.SimpleEntity.class
}
)
@SessionFactory
Expand Down Expand Up @@ -74,6 +78,34 @@ public void testIt(SessionFactoryScope scope) {
);
}

@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 {
Expand Down

0 comments on commit f72bea4

Please sign in to comment.