HHH-10778 - Add support for non-public AttributeConverter implementations
This commit is contained in:
parent
1a5cee7c0c
commit
ae4652378c
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.cfg;
|
package org.hibernate.cfg;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.lang.reflect.TypeVariable;
|
import java.lang.reflect.TypeVariable;
|
||||||
|
@ -56,7 +57,9 @@ public class AttributeConverterDefinition implements AttributeConverterInfo {
|
||||||
|
|
||||||
private static AttributeConverter instantiateAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass) {
|
private static AttributeConverter instantiateAttributeConverter(Class<? extends AttributeConverter> attributeConverterClass) {
|
||||||
try {
|
try {
|
||||||
return attributeConverterClass.newInstance();
|
Constructor<? extends AttributeConverter> constructor = attributeConverterClass.getDeclaredConstructor();
|
||||||
|
constructor.setAccessible( true );
|
||||||
|
return constructor.newInstance();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new AnnotationException(
|
throw new AnnotationException(
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.resource.beans.internal;
|
package org.hibernate.resource.beans.internal;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
|
||||||
import org.hibernate.InstantiationException;
|
import org.hibernate.InstantiationException;
|
||||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||||
|
|
||||||
|
@ -35,7 +37,9 @@ public class FallbackBeanInstanceProducer implements BeanInstanceProducer {
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
public <B> B produceBeanInstance(Class<B> beanType) {
|
||||||
log.tracef( "Creating ManagedBean(%s) using direct instantiation", beanType.getName() );
|
log.tracef( "Creating ManagedBean(%s) using direct instantiation", beanType.getName() );
|
||||||
try {
|
try {
|
||||||
return beanType.newInstance();
|
Constructor<B> constructor = beanType.getDeclaredConstructor();
|
||||||
|
constructor.setAccessible( true );
|
||||||
|
return constructor.newInstance();
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
throw new InstantiationException( "Could not instantiate managed bean directly", beanType, e );
|
throw new InstantiationException( "Could not instantiate managed bean directly", beanType, e );
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package org.hibernate.boot.model.process.internal;
|
||||||
|
|
||||||
|
import javax.persistence.AttributeConverter;
|
||||||
|
import javax.persistence.Converter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@Converter( autoApply = true )
|
||||||
|
class IntegerToVarcharConverter implements AttributeConverter<Integer,String> {
|
||||||
|
@Override
|
||||||
|
public String convertToDatabaseColumn(Integer attribute) {
|
||||||
|
return attribute == null ? null : attribute.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToEntityAttribute(String dbData) {
|
||||||
|
return dbData == null ? null : Integer.valueOf( dbData );
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import java.net.URL;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.hibernate.boot.AttributeConverterInfo;
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
import org.hibernate.boot.archive.internal.ByteArrayInputStreamAccess;
|
import org.hibernate.boot.archive.internal.ByteArrayInputStreamAccess;
|
||||||
import org.hibernate.boot.archive.scan.internal.ClassDescriptorImpl;
|
import org.hibernate.boot.archive.scan.internal.ClassDescriptorImpl;
|
||||||
|
@ -132,6 +133,45 @@ public class ScanningCoordinatorTest extends BaseUnitTestCase {
|
||||||
assertManagedResourcesAfterCoordinateScanWithScanner( scanner, false );
|
assertManagedResourcesAfterCoordinateScanWithScanner( scanner, false );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-10778")
|
||||||
|
public void testManagedResourcesAfterCoordinateScanWithConverterScanner() {
|
||||||
|
|
||||||
|
when( classLoaderService.classForName( "converter" ) ).thenReturn( (Class) IntegerToVarcharConverter.class );
|
||||||
|
|
||||||
|
final Scanner scanner = (ScanEnvironment environment, ScanOptions options, ScanParameters parameters) -> {
|
||||||
|
final InputStreamAccess dummyInputStreamAccess = new ByteArrayInputStreamAccess( "dummy", new byte[0] );
|
||||||
|
|
||||||
|
return new ScanResultImpl(
|
||||||
|
Collections.singleton( new PackageDescriptorImpl( "dummy", dummyInputStreamAccess ) ),
|
||||||
|
Collections.singleton( new ClassDescriptorImpl(
|
||||||
|
"converter",
|
||||||
|
ClassDescriptor.Categorization.CONVERTER,
|
||||||
|
dummyInputStreamAccess
|
||||||
|
) ),
|
||||||
|
Collections.singleton( new MappingFileDescriptorImpl( "dummy", dummyInputStreamAccess ) )
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
when( bootstrapContext.getScanner() ).thenReturn( scanner );
|
||||||
|
|
||||||
|
final ManagedResourcesImpl managedResources = ManagedResourcesImpl.baseline(
|
||||||
|
new MetadataSources(),
|
||||||
|
bootstrapContext
|
||||||
|
);
|
||||||
|
|
||||||
|
ScanningCoordinator.INSTANCE.coordinateScan( managedResources, bootstrapContext, xmlMappingBinderAccess );
|
||||||
|
|
||||||
|
assertEquals( 1, scanEnvironment.getExplicitlyListedClassNames().size() );
|
||||||
|
assertEquals( "a.b.C", scanEnvironment.getExplicitlyListedClassNames().get( 0 ) );
|
||||||
|
|
||||||
|
assertEquals( 1, managedResources.getAttributeConverterDefinitions().size() );
|
||||||
|
AttributeConverterInfo attributeConverterInfo = managedResources.getAttributeConverterDefinitions()
|
||||||
|
.iterator()
|
||||||
|
.next();
|
||||||
|
assertEquals( IntegerToVarcharConverter.class, attributeConverterInfo.getConverterClass() );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run coordinateScan() with the given Scanner and assert the emptiness
|
* Run coordinateScan() with the given Scanner and assert the emptiness
|
||||||
* of ManagedResources.
|
* of ManagedResources.
|
||||||
|
|
|
@ -45,6 +45,7 @@ import org.hibernate.type.descriptor.java.StringTypeDescriptor;
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.boot.MetadataBuildingContextTestingImpl;
|
import org.hibernate.testing.boot.MetadataBuildingContextTestingImpl;
|
||||||
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
import org.hibernate.testing.junit4.BaseUnitTestCase;
|
||||||
|
import org.hibernate.testing.util.ExceptionUtil;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
||||||
|
@ -67,8 +68,7 @@ public class AttributeConverterTest extends BaseUnitTestCase {
|
||||||
fail( "expecting an exception" );
|
fail( "expecting an exception" );
|
||||||
}
|
}
|
||||||
catch (AnnotationException e) {
|
catch (AnnotationException e) {
|
||||||
assertNotNull( e.getCause() );
|
assertTyping( BlewUpException.class, ExceptionUtil.rootCause( e ) );
|
||||||
assertTyping( BlewUpException.class, e.getCause() );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
/*
|
||||||
|
* 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.test.converter;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.persistence.AttributeConverter;
|
||||||
|
import javax.persistence.Convert;
|
||||||
|
import javax.persistence.Converter;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Tuple;
|
||||||
|
|
||||||
|
import org.hibernate.jpa.boot.spi.PersistenceUnitDescriptor;
|
||||||
|
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||||
|
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@TestForIssue( jiraKey = "HHH-10778" )
|
||||||
|
public class PackagePrivateAttributeConverterEntityManagerFactoryTest extends BaseEntityManagerFunctionalTestCase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class[] getAnnotatedClasses() {
|
||||||
|
return new Class[] { Tester.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||||
|
Tester tester = new Tester();
|
||||||
|
tester.setId( 1L );
|
||||||
|
tester.setCode( 123 );
|
||||||
|
|
||||||
|
entityManager.persist( tester );
|
||||||
|
} );
|
||||||
|
|
||||||
|
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||||
|
Tuple tuple = (Tuple) entityManager.createNativeQuery(
|
||||||
|
"select code " +
|
||||||
|
"from Tester " +
|
||||||
|
"where id = :id", Tuple.class )
|
||||||
|
.setParameter( "id", 1L )
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
assertEquals( "123", tuple.get( "code" ) );
|
||||||
|
|
||||||
|
Tester tester = entityManager.find( Tester.class, 1L );
|
||||||
|
|
||||||
|
assertEquals( 123, (int) tester.getCode() );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entity declarations used in the test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@Entity(name = "Tester")
|
||||||
|
public static class Tester {
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Convert( converter = IntegerToVarcharConverter.class )
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(Integer code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter( autoApply = true )
|
||||||
|
static class IntegerToVarcharConverter implements AttributeConverter<Integer,String> {
|
||||||
|
@Override
|
||||||
|
public String convertToDatabaseColumn(Integer attribute) {
|
||||||
|
return attribute == null ? null : attribute.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToEntityAttribute(String dbData) {
|
||||||
|
return dbData == null ? null : Integer.valueOf( dbData );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
/*
|
||||||
|
* 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.test.converter;
|
||||||
|
|
||||||
|
import javax.persistence.AttributeConverter;
|
||||||
|
import javax.persistence.Convert;
|
||||||
|
import javax.persistence.Converter;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Tuple;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@TestForIssue( jiraKey = "HHH-10778" )
|
||||||
|
public class PackagePrivateAttributeConverterSessionFactoryTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class[] getAnnotatedClasses() {
|
||||||
|
return new Class[] { Tester.class };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
|
Tester tester = new Tester();
|
||||||
|
tester.setId( 1L );
|
||||||
|
tester.setCode( 123 );
|
||||||
|
|
||||||
|
session.persist( tester );
|
||||||
|
} );
|
||||||
|
|
||||||
|
doInHibernate( this::sessionFactory, session -> {
|
||||||
|
Tuple tuple = session.createNativeQuery(
|
||||||
|
"select code " +
|
||||||
|
"from Tester " +
|
||||||
|
"where id = :id", Tuple.class )
|
||||||
|
.setParameter( "id", 1L )
|
||||||
|
.getSingleResult();
|
||||||
|
|
||||||
|
assertEquals( "123", tuple.get( "code" ) );
|
||||||
|
|
||||||
|
Tester tester = session.find( Tester.class, 1L );
|
||||||
|
|
||||||
|
assertEquals( 123, (int) tester.getCode() );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entity declarations used in the test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@Entity(name = "Tester")
|
||||||
|
public static class Tester {
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Convert( converter = IntegerToVarcharConverter.class )
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCode(Integer code) {
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Converter( autoApply = true )
|
||||||
|
static class IntegerToVarcharConverter implements AttributeConverter<Integer,String> {
|
||||||
|
@Override
|
||||||
|
public String convertToDatabaseColumn(Integer attribute) {
|
||||||
|
return attribute == null ? null : attribute.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer convertToEntityAttribute(String dbData) {
|
||||||
|
return dbData == null ? null : Integer.valueOf( dbData );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue