Skip to content

Commit 0c109c1

Browse files
committed
improve the error message when an entity is not annotated @entity or not added to PU
1 parent c1b5516 commit 0c109c1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

hibernate-core/src/main/java/org/hibernate/UnknownEntityTypeException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public UnknownEntityTypeException(String message, Throwable cause) {
1919
}
2020

2121
public UnknownEntityTypeException(String entityName) {
22-
super( "Unknown entity type: " + entityName );
22+
super( "Unknown entity type '" + entityName + "'" );
2323
}
2424

2525
public UnknownEntityTypeException(Class<?> entityClass) {

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import jakarta.persistence.CacheRetrieveMode;
88
import jakarta.persistence.CacheStoreMode;
9+
import jakarta.persistence.Entity;
910
import jakarta.persistence.EntityGraph;
1011
import jakarta.persistence.EntityNotFoundException;
1112
import jakarta.persistence.FindOption;
@@ -1526,21 +1527,30 @@ public EntityPersister getEntityPersister(final String entityName, final Object
15261527
return requireEntityPersister( guessEntityName( object ) );
15271528
}
15281529
else {
1529-
// try block is a hack around fact that currently tuplizers are not
1530-
// given the opportunity to resolve a subclass entity name. this
1531-
// allows the (we assume custom) interceptor the ability to
1530+
// try block is a hack around fact that currently tuplizers are
1531+
// not given the opportunity to resolve a subclass entity name.
1532+
// This allows the (we assume custom) interceptor the ability to
15321533
// influence this decision if we were not able to based on the
15331534
// given entityName
15341535
try {
15351536
return requireEntityPersister( entityName )
15361537
.getSubclassEntityPersister( object, getFactory() );
15371538
}
1538-
catch ( HibernateException e ) {
1539+
catch ( UnknownEntityTypeException uee ) {
15391540
try {
15401541
return getEntityPersister( null, object );
15411542
}
1542-
catch ( HibernateException e2 ) {
1543-
throw e;
1543+
catch ( HibernateException e ) {
1544+
final Class<?> objectClass = object.getClass();
1545+
final String problem =
1546+
objectClass.isAnnotationPresent( Entity.class )
1547+
? "does not belong to this persistence unit"
1548+
: "is not annotated '@Entity'";
1549+
throw new UnknownEntityTypeException(
1550+
uee.getMessage()
1551+
+ " ('" + objectClass.getSimpleName() + "' " + problem + ")",
1552+
e
1553+
);
15441554
}
15451555
}
15461556
}

0 commit comments

Comments
 (0)