EntityManager#find() throws IllegalArgumentException for wrong arguments
This commit is contained in:
parent
0509b10040
commit
6327616c8d
|
@ -33,6 +33,7 @@ import org.hibernate.metamodel.mapping.CompositeIdentifierMapping;
|
|||
import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
|
||||
import org.hibernate.metamodel.mapping.EntityMappingType;
|
||||
import org.hibernate.metamodel.mapping.MappingType;
|
||||
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.hibernate.pretty.MessageHelper;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
|
@ -65,11 +66,9 @@ public class DefaultLoadEventListener implements LoadEventListener {
|
|||
throw new HibernateException( "Unable to locate persister: " + event.getEntityClassName() );
|
||||
}
|
||||
|
||||
final Class<?> idClass = persister.getIdentifierType().getReturnedClass();
|
||||
if ( idClass != null &&
|
||||
!idClass.isInstance( event.getEntityId() ) &&
|
||||
!(event.getEntityId() instanceof DelayedPostInsertIdentifier) ) {
|
||||
checkIdClass( persister, event, loadType, idClass );
|
||||
if ( !persister.getIdentifierMapping().getJavaType().isInstance( event.getEntityId() ) &&
|
||||
!( event.getEntityId() instanceof DelayedPostInsertIdentifier ) ) {
|
||||
checkIdClass( persister, event, loadType, persister.getIdentifierType().getReturnedClass() );
|
||||
}
|
||||
|
||||
doOnLoad( persister, event, loadType );
|
||||
|
@ -155,6 +154,11 @@ public class DefaultLoadEventListener implements LoadEventListener {
|
|||
}
|
||||
}
|
||||
}
|
||||
else if ( idMapping instanceof NonAggregatedIdentifierMapping ) {
|
||||
if ( idClass.isInstance( event.getEntityId() ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new TypeMismatchException(
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* 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.orm.test.jpa.compliance;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
|
||||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||
import org.hibernate.testing.orm.junit.Jpa;
|
||||
import org.hibernate.testing.orm.junit.Setting;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.LockModeType;
|
||||
import jakarta.persistence.Temporal;
|
||||
import jakarta.persistence.TemporalType;
|
||||
import jakarta.persistence.TransactionRequiredException;
|
||||
|
||||
@Jpa(
|
||||
annotatedClasses = {EntityManagerFindTest.TestEntity.class,
|
||||
EntityManagerFindTest.TestEntity2.class},
|
||||
properties = @Setting( name = AvailableSettings.JPA_LOAD_BY_ID_COMPLIANCE, value = "true")
|
||||
)
|
||||
public class EntityManagerFindTest {
|
||||
|
||||
@Test
|
||||
public void testFindWrongTypeAsId(EntityManagerFactoryScope scope) {
|
||||
scope.inEntityManager(
|
||||
entityManager ->
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
entityManager.find( TestEntity.class, "ID" )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindNullId(EntityManagerFactoryScope scope) {
|
||||
scope.inEntityManager(
|
||||
entityManager ->
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
entityManager.find( TestEntity.class, null )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindPessimisticLockNoTransation(EntityManagerFactoryScope scope) {
|
||||
scope.inEntityManager(
|
||||
entityManager ->
|
||||
Assertions.assertThrows(
|
||||
TransactionRequiredException.class,
|
||||
() ->
|
||||
entityManager.find( TestEntity.class, 1, LockModeType.PESSIMISTIC_READ )
|
||||
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindNonExistingEntity(EntityManagerFactoryScope scope) {
|
||||
scope.inEntityManager(
|
||||
entityManager ->
|
||||
Assertions.assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
entityManager.find( NonExistingEntity.class, 1 )
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findJavaUtilDateAsId(EntityManagerFactoryScope scope){
|
||||
scope.inTransaction(
|
||||
entityManager -> {
|
||||
entityManager.find( TestEntity2.class, new Date() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static class NonExistingEntity {
|
||||
|
||||
}
|
||||
|
||||
@Entity(name = "TestEntity")
|
||||
public static class TestEntity {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Entity(name = "TestEntity2")
|
||||
public static class TestEntity2 {
|
||||
@Id
|
||||
@Temporal(TemporalType.DATE)
|
||||
protected java.util.Date id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue