Skip to content

Commit 0ea03fd

Browse files
committed
HHH-9856 - EntityManager.find() and getReference() throw incorrect exception for non-entity
1 parent 2b89553 commit 0ea03fd

File tree

6 files changed

+121
-4
lines changed

6 files changed

+121
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate;
8+
9+
/**
10+
* Indicates an attempt was made to refer to an unknown entity name/class.
11+
* <p/>
12+
* NOTE : extends MappingException because that's what core used to do and that's how
13+
* HEM expectes it. Longer term I think it makes more sense to have a different
14+
* hierarchy for runtime-"mapping" exceptions.
15+
*
16+
* @author Steve Ebersole
17+
*/
18+
public class UnknownEntityTypeException extends MappingException {
19+
public UnknownEntityTypeException(String message, Throwable cause) {
20+
super( message, cause );
21+
}
22+
23+
public UnknownEntityTypeException(String message) {
24+
super( message );
25+
}
26+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory {
289289
*
290290
* @return The located EntityPersister, never {@code null}
291291
*
292-
* @throws HibernateException If a matching EntityPersister cannot be located
292+
* @throws org.hibernate.UnknownEntityTypeException If a matching EntityPersister cannot be located
293293
*/
294294
EntityPersister locateEntityPersister(Class byClass);
295295

@@ -300,7 +300,7 @@ public interface SessionFactoryImplementor extends Mapping, SessionFactory {
300300
*
301301
* @return The located EntityPersister, never {@code null}
302302
*
303-
* @throws HibernateException If a matching EntityPersister cannot be located
303+
* @throws org.hibernate.UnknownEntityTypeException If a matching EntityPersister cannot be located
304304
*/
305305
EntityPersister locateEntityPersister(String byName);
306306

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.hibernate.StatelessSessionBuilder;
5050
import org.hibernate.Transaction;
5151
import org.hibernate.TypeHelper;
52+
import org.hibernate.UnknownEntityTypeException;
5253
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
5354
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
5455
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@@ -781,7 +782,7 @@ public EntityPersister locateEntityPersister(Class byClass) {
781782
}
782783

783784
if ( entityPersister == null ) {
784-
throw new HibernateException( "Unable to locate persister: " + byClass.getName() );
785+
throw new UnknownEntityTypeException( "Unable to locate persister: " + byClass.getName() );
785786
}
786787

787788
return entityPersister;
@@ -791,7 +792,7 @@ public EntityPersister locateEntityPersister(Class byClass) {
791792
public EntityPersister locateEntityPersister(String byName) {
792793
final EntityPersister entityPersister = entityPersisters.get( byName );
793794
if ( entityPersister == null ) {
794-
throw new HibernateException( "Unable to locate persister: " + byName );
795+
throw new UnknownEntityTypeException( "Unable to locate persister: " + byName );
795796
}
796797
return entityPersister;
797798
}

hibernate-core/src/test/java/org/hibernate/test/annotations/entitynonentity/EntityNonEntityTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
import org.hibernate.Session;
1010
import org.hibernate.Transaction;
11+
import org.hibernate.UnknownEntityTypeException;
1112

13+
import org.hibernate.testing.TestForIssue;
1214
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
1315
import org.junit.Test;
1416

1517
import static org.junit.Assert.assertEquals;
1618
import static org.junit.Assert.assertNull;
1719
import static org.junit.Assert.assertTrue;
20+
import static org.junit.Assert.fail;
1821

1922
/**
2023
* @author Emmanuel Bernard
@@ -45,6 +48,52 @@ public void testMix() throws Exception {
4548
s.close();
4649
}
4750

51+
@Test
52+
@TestForIssue( jiraKey = "HHH-9856" )
53+
public void testGetAndFindNonEntityThrowsIllegalArgumentException() {
54+
try {
55+
sessionFactory().locateEntityPersister( Cellular.class );
56+
}
57+
catch (UnknownEntityTypeException ignore) {
58+
// expected
59+
}
60+
61+
try {
62+
sessionFactory().locateEntityPersister( Cellular.class.getName() );
63+
}
64+
catch (UnknownEntityTypeException ignore) {
65+
// expected
66+
}
67+
68+
Session s = openSession();
69+
s.beginTransaction();
70+
try {
71+
s.get( Cellular.class, 1 );
72+
fail( "Expecting a failure" );
73+
}
74+
catch (UnknownEntityTypeException ignore) {
75+
// expected
76+
}
77+
finally {
78+
s.getTransaction().commit();
79+
s.close();
80+
}
81+
82+
s = openSession();
83+
s.beginTransaction();
84+
try {
85+
s.get( Cellular.class.getName(), 1 );
86+
fail( "Expecting a failure" );
87+
}
88+
catch (UnknownEntityTypeException ignore) {
89+
// expected
90+
}
91+
finally {
92+
s.getTransaction().commit();
93+
s.close();
94+
}
95+
}
96+
4897
@Override
4998
protected Class[] getAnnotatedClasses() {
5099
return new Class[]{

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ops/FindTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
1212

13+
import org.hibernate.testing.TestForIssue;
1314
import org.junit.Assert;
1415
import org.junit.Test;
1516

17+
import static org.junit.Assert.fail;
18+
1619
/**
1720
* @author Emmanuel Bernard
1821
*/
@@ -31,6 +34,24 @@ public void testSubclassWrongId() throws Exception {
3134
em.close();
3235
}
3336

37+
@Test
38+
@TestForIssue( jiraKey = "HHH-9856" )
39+
public void testNonEntity() {
40+
EntityManager em = getOrCreateEntityManager();
41+
em.getTransaction().begin();
42+
try {
43+
em.find( String.class, 1 );
44+
fail( "Expecting a failure" );
45+
}
46+
catch (IllegalArgumentException ignore) {
47+
// expected
48+
}
49+
finally {
50+
em.getTransaction().rollback();
51+
em.close();
52+
}
53+
}
54+
3455
@Override
3556
public Class[] getAnnotatedClasses() {
3657
return new Class[] {

hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/ops/GetLoadTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
1616
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
1717

18+
import org.hibernate.testing.TestForIssue;
1819
import org.junit.Test;
1920

2021
import static org.junit.Assert.assertEquals;
2122
import static org.junit.Assert.assertFalse;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
25+
import static org.junit.Assert.fail;
2426

2527
/**
2628
* @author Gavin King
@@ -106,6 +108,24 @@ private void assertFetchCount(int count) {
106108
assertEquals( count, fetches );
107109
}
108110

111+
@Test
112+
@TestForIssue( jiraKey = "HHH-9856" )
113+
public void testNonEntity() {
114+
EntityManager em = getOrCreateEntityManager();
115+
em.getTransaction().begin();
116+
try {
117+
em.getReference( String.class, 1 );
118+
fail( "Expecting a failure" );
119+
}
120+
catch (IllegalArgumentException ignore) {
121+
// expected
122+
}
123+
finally {
124+
em.getTransaction().rollback();
125+
em.close();
126+
}
127+
}
128+
109129
@Override
110130
@SuppressWarnings( {"unchecked"})
111131
protected void addConfigOptions(Map options) {

0 commit comments

Comments
 (0)