HHH-9856 - EntityManager.find() and getReference() throw incorrect exception for non-entity
This commit is contained in:
parent
2b89553db5
commit
0ea03fd9a4
|
@ -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 );
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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[]{
|
||||
|
|
|
@ -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[] {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue