HHH-9856 - EntityManager.find() and getReference() throw incorrect exception for non-entity

This commit is contained in:
Steve Ebersole 2015-06-08 18:04:56 -05:00
parent 2b89553db5
commit 0ea03fd9a4
6 changed files with 121 additions and 4 deletions

View File

@ -0,0 +1,26 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate;
/**
* Indicates an attempt was made to refer to an unknown entity name/class.
* <p/>
* NOTE : extends MappingException because that's what core used to do and that's how
* HEM expectes it. Longer term I think it makes more sense to have a different
* hierarchy for runtime-"mapping" exceptions.
*
* @author Steve Ebersole
*/
public class UnknownEntityTypeException extends MappingException {
public UnknownEntityTypeException(String message, Throwable cause) {
super( message, cause );
}
public UnknownEntityTypeException(String message) {
super( message );
}
}

View File

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

View File

@ -49,6 +49,7 @@ import org.hibernate.StatelessSession;
import org.hibernate.StatelessSessionBuilder;
import org.hibernate.Transaction;
import org.hibernate.TypeHelper;
import org.hibernate.UnknownEntityTypeException;
import org.hibernate.boot.cfgxml.spi.CfgXmlAccessService;
import org.hibernate.boot.cfgxml.spi.LoadedConfig;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
@ -781,7 +782,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
}
if ( entityPersister == null ) {
throw new HibernateException( "Unable to locate persister: " + byClass.getName() );
throw new UnknownEntityTypeException( "Unable to locate persister: " + byClass.getName() );
}
return entityPersister;
@ -791,7 +792,7 @@ public final class SessionFactoryImpl implements SessionFactoryImplementor {
public EntityPersister locateEntityPersister(String byName) {
final EntityPersister entityPersister = entityPersisters.get( byName );
if ( entityPersister == null ) {
throw new HibernateException( "Unable to locate persister: " + byName );
throw new UnknownEntityTypeException( "Unable to locate persister: " + byName );
}
return entityPersister;
}

View File

@ -8,13 +8,16 @@ package org.hibernate.test.annotations.entitynonentity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.UnknownEntityTypeException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Emmanuel Bernard
@ -45,6 +48,52 @@ public class EntityNonEntityTest extends BaseCoreFunctionalTestCase {
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9856" )
public void testGetAndFindNonEntityThrowsIllegalArgumentException() {
try {
sessionFactory().locateEntityPersister( Cellular.class );
}
catch (UnknownEntityTypeException ignore) {
// expected
}
try {
sessionFactory().locateEntityPersister( Cellular.class.getName() );
}
catch (UnknownEntityTypeException ignore) {
// expected
}
Session s = openSession();
s.beginTransaction();
try {
s.get( Cellular.class, 1 );
fail( "Expecting a failure" );
}
catch (UnknownEntityTypeException ignore) {
// expected
}
finally {
s.getTransaction().commit();
s.close();
}
s = openSession();
s.beginTransaction();
try {
s.get( Cellular.class.getName(), 1 );
fail( "Expecting a failure" );
}
catch (UnknownEntityTypeException ignore) {
// expected
}
finally {
s.getTransaction().commit();
s.close();
}
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{

View File

@ -10,9 +10,12 @@ import javax.persistence.EntityManager;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.fail;
/**
* @author Emmanuel Bernard
*/
@ -31,6 +34,24 @@ public class FindTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9856" )
public void testNonEntity() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.find( String.class, 1 );
fail( "Expecting a failure" );
}
catch (IllegalArgumentException ignore) {
// expected
}
finally {
em.getTransaction().rollback();
em.close();
}
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[] {

View File

@ -15,12 +15,14 @@ import org.hibernate.cfg.Environment;
import org.hibernate.jpa.internal.EntityManagerFactoryImpl;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Gavin King
@ -106,6 +108,24 @@ public class GetLoadTest extends BaseEntityManagerFunctionalTestCase {
assertEquals( count, fetches );
}
@Test
@TestForIssue( jiraKey = "HHH-9856" )
public void testNonEntity() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.getReference( String.class, 1 );
fail( "Expecting a failure" );
}
catch (IllegalArgumentException ignore) {
// expected
}
finally {
em.getTransaction().rollback();
em.close();
}
}
@Override
@SuppressWarnings( {"unchecked"})
protected void addConfigOptions(Map options) {