diff --git a/hibernate-core/hibernate-core.gradle b/hibernate-core/hibernate-core.gradle index aebf142b8b..b1caf744bd 100644 --- a/hibernate-core/hibernate-core.gradle +++ b/hibernate-core/hibernate-core.gradle @@ -36,6 +36,7 @@ dependencies { testCompile( libraries.jandex ) testCompile( libraries.classmate ) testCompile( libraries.mockito ) + testCompile( 'joda-time:joda-time:2.3' ) testCompile( libraries.validator ) { // for test runtime diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractTypeDescriptor.java index 8a0233c646..6c2a8ef312 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/AbstractTypeDescriptor.java @@ -48,8 +48,6 @@ public abstract class AbstractTypeDescriptor implements JavaTypeDescriptor this.comparator = Comparable.class.isAssignableFrom( type ) ? (Comparator) ComparableComparator.INSTANCE : null; - - JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaTypeDescriptor.java index 7b882eb4a3..5f4ddc1034 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/EnumJavaTypeDescriptor.java @@ -17,6 +17,8 @@ public class EnumJavaTypeDescriptor extends AbstractTypeDescript @SuppressWarnings("unchecked") protected EnumJavaTypeDescriptor(Class type) { super( type, ImmutableMutabilityPlan.INSTANCE ); + + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java index 9ad2724bb5..69a3d19e01 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/JavaTypeDescriptorRegistry.java @@ -27,13 +27,63 @@ public class JavaTypeDescriptorRegistry { private ConcurrentHashMap descriptorsByClass = new ConcurrentHashMap(); + public JavaTypeDescriptorRegistry() { + addDescriptorInternal( ByteTypeDescriptor.INSTANCE ); + addDescriptorInternal( BooleanTypeDescriptor.INSTANCE ); + addDescriptorInternal( CharacterTypeDescriptor.INSTANCE ); + addDescriptorInternal( ShortTypeDescriptor.INSTANCE ); + addDescriptorInternal( IntegerTypeDescriptor.INSTANCE ); + addDescriptorInternal( LongTypeDescriptor.INSTANCE ); + addDescriptorInternal( FloatTypeDescriptor.INSTANCE ); + addDescriptorInternal( DoubleTypeDescriptor.INSTANCE ); + addDescriptorInternal( BigDecimalTypeDescriptor.INSTANCE ); + addDescriptorInternal( BigIntegerTypeDescriptor.INSTANCE ); + + addDescriptorInternal( StringTypeDescriptor.INSTANCE ); + + addDescriptorInternal( BlobTypeDescriptor.INSTANCE ); + addDescriptorInternal( ClobTypeDescriptor.INSTANCE ); + addDescriptorInternal( NClobTypeDescriptor.INSTANCE ); + + addDescriptorInternal( ByteArrayTypeDescriptor.INSTANCE ); + addDescriptorInternal( CharacterArrayTypeDescriptor.INSTANCE ); + addDescriptorInternal( PrimitiveByteArrayTypeDescriptor.INSTANCE ); + addDescriptorInternal( PrimitiveCharacterArrayTypeDescriptor.INSTANCE ); + + addDescriptorInternal( CalendarTypeDescriptor.INSTANCE ); + addDescriptorInternal( DateTypeDescriptor.INSTANCE ); + descriptorsByClass.put( java.sql.Date.class, JdbcDateTypeDescriptor.INSTANCE ); + descriptorsByClass.put( java.sql.Time.class, JdbcTimeTypeDescriptor.INSTANCE ); + descriptorsByClass.put( java.sql.Timestamp.class, JdbcTimestampTypeDescriptor.INSTANCE ); + addDescriptorInternal( TimeZoneTypeDescriptor.INSTANCE ); + + addDescriptorInternal( ClassTypeDescriptor.INSTANCE ); + + addDescriptorInternal( CurrencyTypeDescriptor.INSTANCE ); + addDescriptorInternal( LocaleTypeDescriptor.INSTANCE ); + addDescriptorInternal( UrlTypeDescriptor.INSTANCE ); + addDescriptorInternal( UUIDTypeDescriptor.INSTANCE ); + } + + private JavaTypeDescriptor addDescriptorInternal(JavaTypeDescriptor descriptor) { + return descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor ); + } + /** * Adds the given descriptor to this registry * * @param descriptor The descriptor to add. */ public void addDescriptor(JavaTypeDescriptor descriptor) { - descriptorsByClass.put( descriptor.getJavaTypeClass(), descriptor ); + JavaTypeDescriptor old = addDescriptorInternal( descriptor ); + if ( old != null ) { + log.debugf( + "JavaTypeDescriptorRegistry entry replaced : %s -> %s (was %s)", + descriptor.getJavaTypeClass(), + descriptor, + old + ); + } } @SuppressWarnings("unchecked") diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/converter/AndNationalizedTests.java b/hibernate-core/src/test/java/org/hibernate/test/converter/AndNationalizedTests.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/type/converter/AndNationalizedTests.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/AndNationalizedTests.java index a8efc3394c..b69db1bc2a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/converter/AndNationalizedTests.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/AndNationalizedTests.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.converter; +package org.hibernate.test.converter; import java.sql.Types; import javax.persistence.AttributeConverter; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/AttributeConverterTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/AttributeConverterTest.java index 41633958a4..dee6aa3cf9 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/AttributeConverterTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type; +package org.hibernate.test.converter; import java.io.Serializable; import java.sql.Timestamp; @@ -178,7 +178,7 @@ public class AttributeConverterTest extends BaseUnitTestCase { try { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Tester.class ) - .addURL( ConfigHelper.findAsResource( "org/hibernate/test/type/orm.xml" ) ) + .addURL( ConfigHelper.findAsResource( "org/hibernate/test/converter/orm.xml" ) ) .getMetadataBuilder() .build(); @@ -359,13 +359,13 @@ public class AttributeConverterTest extends BaseUnitTestCase { JavaConstantNode javaConstantNode = new JavaConstantNode(); javaConstantNode.setExpectedType( type ); javaConstantNode.setSessionFactory( (SessionFactoryImplementor) sf ); - javaConstantNode.setText( "org.hibernate.test.type.AttributeConverterTest$ConvertibleEnum.VALUE" ); + javaConstantNode.setText( "org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE" ); final String outcome = javaConstantNode.getRenderText( (SessionFactoryImplementor) sf ); assertEquals( "'VALUE'", outcome ); s = sf.openSession(); s.beginTransaction(); - s.createQuery( "FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.type.AttributeConverterTest$ConvertibleEnum.VALUE" ) + s.createQuery( "FROM EntityWithConvertibleField e where e.convertibleEnum = org.hibernate.test.converter.AttributeConverterTest$ConvertibleEnum.VALUE" ) .list(); s.getTransaction().commit(); s.close(); diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/BasicJodaTimeConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/BasicJodaTimeConversionTest.java similarity index 50% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/BasicJodaTimeConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/BasicJodaTimeConversionTest.java index 9f8c5ab3d5..1dcfadb0cc 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/BasicJodaTimeConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/BasicJodaTimeConversionTest.java @@ -4,30 +4,22 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import java.net.MalformedURLException; -import java.util.Arrays; import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.Convert; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import org.joda.time.LocalDate; @@ -39,17 +31,23 @@ import static org.junit.Assert.assertTrue; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8842" ) -public class BasicJodaTimeConversionTest { - static int callsToConverter = 0; +public class BasicJodaTimeConversionTest extends BaseNonConfigCoreFunctionalTestCase { + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } public static class JodaLocalDateConverter implements AttributeConverter { public Date convertToDatabaseColumn(LocalDate localDate) { - callsToConverter++; + convertToDatabaseColumnCalled = true; return localDate.toDate(); } public LocalDate convertToEntityAttribute(Date date) { - callsToConverter++; + convertToEntityAttributeCalled = true; return LocalDate.fromDateFields( date ); } } @@ -70,52 +68,42 @@ public class BasicJodaTimeConversionTest { } } + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TheEntity.class }; + } + @Test public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( TheEntity.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( TheEntity.class.getName() ); + final EntityPersister ep = sessionFactory().getEntityPersister( TheEntity.class.getName() ); final Type theDatePropertyType = ep.getPropertyType( "theDate" ); final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); assertTyping( JodaLocalDateConverter.class, type.getAttributeConverter() ); - int previousCallCount = 0; + resetFlags(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new TheEntity( 1, new LocalDate() ) ); - em.getTransaction().commit(); - em.close(); + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new TheEntity( 1, new LocalDate() ) ); + session.getTransaction().commit(); + session.close(); - assertTrue( previousCallCount < callsToConverter ); - previousCallCount = callsToConverter; + assertTrue( convertToDatabaseColumnCalled ); + resetFlags(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.find( TheEntity.class, 1 ); - em.getTransaction().commit(); - em.close(); + session = openSession(); + session.getTransaction().begin(); + session.get( TheEntity.class, 1 ); + session.getTransaction().commit(); + session.close(); - assertTrue( previousCallCount < callsToConverter ); + assertTrue( convertToEntityAttributeCalled ); + resetFlags(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete TheEntity" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete TheEntity" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/converter/DirtyCheckingTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/DirtyCheckingTest.java similarity index 99% rename from hibernate-core/src/test/java/org/hibernate/test/type/converter/DirtyCheckingTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/DirtyCheckingTest.java index 5124862ebf..7c29212002 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/converter/DirtyCheckingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/DirtyCheckingTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.converter; +package org.hibernate.test.converter; import javax.persistence.AttributeConverter; import javax.persistence.Column; diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitDateConvertersTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitDateConvertersTest.java new file mode 100644 index 0000000000..df8b32f841 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitDateConvertersTest.java @@ -0,0 +1,116 @@ +/* + * 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 . + */ +package org.hibernate.test.converter; + +import java.net.MalformedURLException; +import java.util.Date; +import javax.persistence.AttributeConverter; +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.Session; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.Type; +import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-8807" ) +public class ExplicitDateConvertersTest extends BaseNonConfigCoreFunctionalTestCase { + + // NOTE : initially unable to reproduce the reported problem + + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } + + public static class LongToDateConverter implements AttributeConverter { + @Override + public Long convertToDatabaseColumn(Date attribute) { + convertToDatabaseColumnCalled = true; + return attribute.getTime(); + } + + @Override + public Date convertToEntityAttribute(Long dbData) { + convertToEntityAttributeCalled = true; + return new Date( dbData ); + } + } + + @Entity( name = "Entity1" ) + public static class Entity1 { + @Id + private Integer id; + private String name; + @Convert( converter = LongToDateConverter.class ) + private Date theDate; + + public Entity1() { + } + + public Entity1(Integer id, String name, Date theDate) { + this.id = id; + this.name = name; + this.theDate = theDate; + } + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class }; + } + + @Test + public void testSimpleConvertUsage() throws MalformedURLException { + final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); + final Type theDatePropertyType = ep.getPropertyType( "theDate" ); + final AttributeConverterTypeAdapter type = assertTyping( + AttributeConverterTypeAdapter.class, + theDatePropertyType + ); + assertTyping( LongToDateConverter.class, type.getAttributeConverter() ); + + resetFlags(); + + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "1", new Date() ) ); + session.getTransaction().commit(); + session.close(); + + assertTrue( convertToDatabaseColumnCalled ); + resetFlags(); + + session = openSession(); + session.getTransaction().begin(); + session.get( Entity1.class, 1 ); + session.getTransaction().commit(); + session.close(); + + assertTrue( convertToEntityAttributeCalled ); + + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitEnumConvertersTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitEnumConvertersTest.java new file mode 100644 index 0000000000..00fe8e085f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitEnumConvertersTest.java @@ -0,0 +1,124 @@ +/* + * 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 . + */ +package org.hibernate.test.converter; + +import java.net.MalformedURLException; +import javax.persistence.AttributeConverter; +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.Session; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.Type; +import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; +import static org.junit.Assert.assertTrue; + +/** + * @author Steve Ebersole + */ +@TestForIssue( jiraKey = "HHH-8809" ) +public class ExplicitEnumConvertersTest extends BaseNonConfigCoreFunctionalTestCase { + + // NOTE : initially unable to reproduce the reported problem + + public static enum MediaType { + MUSIC, + VIDEO, + PHOTO, + MUSIC_STREAM, + VIDEO_STREAM + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class }; + } + + + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } + + public static class MediaTypeConverter implements AttributeConverter { + @Override + public String convertToDatabaseColumn(MediaType attribute) { + convertToDatabaseColumnCalled = true; + return attribute.name(); + } + + @Override + public MediaType convertToEntityAttribute(String dbData) { + convertToEntityAttributeCalled = true; + return MediaType.valueOf( dbData ); + } + } + + @Entity( name = "Entity1" ) + public static class Entity1 { + @Id + private Integer id; + private String name; + @Convert( converter = MediaTypeConverter.class ) + private MediaType mediaType; + + public Entity1() { + } + + public Entity1(Integer id, String name, MediaType mediaType) { + this.id = id; + this.name = name; + this.mediaType = mediaType; + } + } + + @Test + public void testSimpleConvertUsage() throws MalformedURLException { + final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); + final Type theDatePropertyType = ep.getPropertyType( "mediaType" ); + final AttributeConverterTypeAdapter type = assertTyping( + AttributeConverterTypeAdapter.class, + theDatePropertyType + ); + assertTyping( MediaTypeConverter.class, type.getAttributeConverter() ); + + resetFlags(); + + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "300", MediaType.VIDEO ) ); + session.getTransaction().commit(); + session.close(); + + assertTrue( convertToDatabaseColumnCalled ); + resetFlags(); + + session = openSession(); + session.getTransaction().begin(); + session.get( Entity1.class, 1 ); + session.getTransaction().commit(); + session.close(); + + assertTrue( convertToEntityAttributeCalled ); + + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); + } +} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitlyNamedConverterClassesTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitlyNamedConverterClassesTest.java similarity index 50% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitlyNamedConverterClassesTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitlyNamedConverterClassesTest.java index 8566ed14af..a4572d18ce 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitlyNamedConverterClassesTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/ExplicitlyNamedConverterClassesTest.java @@ -4,36 +4,24 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import javax.persistence.AttributeConverter; -import javax.persistence.Convert; import javax.persistence.Converter; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; -import org.hibernate.testing.junit4.BaseUnitTestCase; - /** * Tests for asserting correct behavior of applying AttributeConverters explicitly listed in persistence.xml. * * @author Steve Ebersole */ -public class ExplicitlyNamedConverterClassesTest extends BaseUnitTestCase { +public class ExplicitlyNamedConverterClassesTest extends BaseNonConfigCoreFunctionalTestCase { // test handling of explicitly named, but non-auto-applied converter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -65,34 +53,23 @@ public class ExplicitlyNamedConverterClassesTest extends BaseUnitTestCase { } } + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class, NotAutoAppliedConverter.class }; + } + @Test public void testNonAutoAppliedConvertIsNotApplied() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName(), NotAutoAppliedConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "1" ) ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "1" ) ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/converter/NullHandlingTests.java b/hibernate-core/src/test/java/org/hibernate/test/converter/NullHandlingTests.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/type/converter/NullHandlingTests.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/NullHandlingTests.java index c52dd6de77..1f378f1f89 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/converter/NullHandlingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/NullHandlingTests.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.converter; +package org.hibernate.test.converter; import java.sql.Connection; import java.sql.ResultSet; diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/QueryTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/QueryTest.java similarity index 72% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/QueryTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/QueryTest.java index a5764deb4d..35fbe1e07a 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/QueryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/QueryTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import javax.persistence.AttributeConverter; import javax.persistence.Column; @@ -12,12 +12,12 @@ import javax.persistence.Converter; import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.Entity; -import javax.persistence.EntityManager; import javax.persistence.Id; import javax.persistence.Table; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.Session; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -29,18 +29,18 @@ import static junit.framework.Assert.assertNotNull; * * @author Steve Ebersole */ -public class QueryTest extends BaseEntityManagerFunctionalTestCase { +public class QueryTest extends BaseNonConfigCoreFunctionalTestCase { public static final float SALARY = 267.89f; @Test public void testJpqlLiteral() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Employee jDoe = em.createQuery( "from Employee e where e.salary = " + SALARY + "f", Employee.class ).getSingleResult(); + Session session = openSession(); + session.getTransaction().begin(); + Employee jDoe = (Employee) session.createQuery( "from Employee e where e.salary = " + SALARY + "f" ).uniqueResult(); assertNotNull( jDoe ); - em.getTransaction().commit(); - em.close(); + session.getTransaction().commit(); + session.close(); } @Override @@ -50,20 +50,20 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase { @Before public void setUpTestData() { - EntityManager em = entityManagerFactory().createEntityManager(); - em.getTransaction().begin(); - em.persist( new Employee( 1, new Name( "John", "Q.", "Doe" ), SALARY ) ); - em.getTransaction().commit(); - em.close(); + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Employee( 1, new Name( "John", "Q.", "Doe" ), SALARY ) ); + session.getTransaction().commit(); + session.close(); } @After public void cleanUpTestData() { - EntityManager em = entityManagerFactory().createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Employee" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); + Session session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Employee" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Employee" ) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SillyStringConverter.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SillyStringConverter.java similarity index 94% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SillyStringConverter.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/SillyStringConverter.java index a80a2bef0a..1a35b93c5a 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SillyStringConverter.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SillyStringConverter.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import javax.persistence.AttributeConverter; import javax.persistence.Converter; diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertAnnotationTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertAnnotationTest.java similarity index 54% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertAnnotationTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertAnnotationTest.java index 229e533bab..5765566424 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertAnnotationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertAnnotationTest.java @@ -4,94 +4,85 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import java.net.MalformedURLException; import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.Convert; import javax.persistence.Converter; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertTrue; /** + * test handling of an AttributeConverter explicitly named via a @Convert annotation + * * @author Steve Ebersole */ -public class SimpleConvertAnnotationTest extends BaseUnitTestCase { +public class SimpleConvertAnnotationTest extends BaseNonConfigCoreFunctionalTestCase { - // test handling of an AttributeConverter explicitly named via a @Convert annotation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class, UrlConverter.class, AutoUrlConverter.class }; + } @Test public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName(), UrlConverter.class.getName(), AutoUrlConverter.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); + final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); final Type websitePropertyType = ep.getPropertyType( "website" ); - final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, websitePropertyType ); + final AttributeConverterTypeAdapter type = assertTyping( + AttributeConverterTypeAdapter.class, + websitePropertyType + ); assertTyping( UrlConverter.class, type.getAttributeConverter() ); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "1", new URL( "http://hibernate.org" ) ) ); - em.getTransaction().commit(); - em.close(); + resetFlags(); - assertTrue( 0 < callsToConverter ); + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "1", new URL( "http://hibernate.org" ) ) ); + session.getTransaction().commit(); + session.close(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + assertTrue( convertToDatabaseColumnCalled ); + + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); } - static int callsToConverter = 0; + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } @Converter(autoApply = false) public static class UrlConverter implements AttributeConverter { @Override public String convertToDatabaseColumn(URL attribute) { - callsToConverter++; + convertToDatabaseColumnCalled = true; return attribute == null ? null : attribute.toExternalForm(); } @Override public URL convertToEntityAttribute(String dbData) { - callsToConverter++; + convertToEntityAttributeCalled = true; if ( dbData == null ) { return null; } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertsAnnotationTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertsAnnotationTest.java similarity index 53% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertsAnnotationTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertsAnnotationTest.java index 6b98fd072f..fe7050bc94 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleConvertsAnnotationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleConvertsAnnotationTest.java @@ -4,95 +4,86 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import java.net.MalformedURLException; import java.net.URL; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.Convert; import javax.persistence.Converter; import javax.persistence.Converts; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; import static org.junit.Assert.assertTrue; /** + * test handling of an AttributeConverter explicitly named via a @Converts annotation + * * @author Steve Ebersole */ -public class SimpleConvertsAnnotationTest extends BaseUnitTestCase { +public class SimpleConvertsAnnotationTest extends BaseNonConfigCoreFunctionalTestCase { - // test handling of an AttributeConverter explicitly named via a @Convert annotation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - @Test - public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName(), UrlConverter.class.getName(), AutoUrlConverter.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); - final Type websitePropertyType = ep.getPropertyType( "website" ); - final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, websitePropertyType ); - assertTyping( UrlConverter.class, type.getAttributeConverter() ); - - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "1", new URL( "http://hibernate.org" ) ) ); - em.getTransaction().commit(); - em.close(); - - assertTrue( 0 < callsToConverter ); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class, UrlConverter.class, AutoUrlConverter.class }; } - static int callsToConverter = 0; + @Test + public void testSimpleConvertsUsage() throws MalformedURLException { + final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); + final Type websitePropertyType = ep.getPropertyType( "website" ); + final AttributeConverterTypeAdapter type = assertTyping( + AttributeConverterTypeAdapter.class, + websitePropertyType + ); + assertTyping( UrlConverter.class, type.getAttributeConverter() ); + + resetFlags(); + + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "1", new URL( "http://hibernate.org" ) ) ); + session.getTransaction().commit(); + session.close(); + + assertTrue( convertToDatabaseColumnCalled ); + + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); + } + + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } @Converter(autoApply = false) public static class UrlConverter implements AttributeConverter { @Override public String convertToDatabaseColumn(URL attribute) { - callsToConverter++; + convertToDatabaseColumnCalled = true; return attribute == null ? null : attribute.toExternalForm(); } @Override public URL convertToEntityAttribute(String dbData) { - callsToConverter++; + convertToEntityAttributeCalled = true; if ( dbData == null ) { return null; } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleEmbeddableOverriddenConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleEmbeddableOverriddenConverterTest.java similarity index 55% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleEmbeddableOverriddenConverterTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/SimpleEmbeddableOverriddenConverterTest.java index fa1a6dcb61..3199cf3d1c 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleEmbeddableOverriddenConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleEmbeddableOverriddenConverterTest.java @@ -4,33 +4,22 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import javax.persistence.Convert; import javax.persistence.Embeddable; import javax.persistence.Embedded; import javax.persistence.Entity; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; -import javax.persistence.MappedSuperclass; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.CompositeType; import org.hibernate.type.StringType; import org.hibernate.type.Type; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; -import org.hibernate.testing.junit4.BaseUnitTestCase; - import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; /** @@ -38,35 +27,26 @@ import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; * * @author Steve Ebersole */ -public class SimpleEmbeddableOverriddenConverterTest extends BaseUnitTestCase { +public class SimpleEmbeddableOverriddenConverterTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Person.class }; + } + + @Override + protected boolean createSchema() { + return false; + } + /** * Test outcome of annotations exclusively. */ @Test public void testSimpleConvertOverrides() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Person.class.getName() ); - } - }; - - final Map settings = new HashMap(); -// settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - - final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class ); - try { - final EntityPersister ep = sfi.getEntityPersister( Person.class.getName() ); - - CompositeType homeAddressType = assertTyping( CompositeType.class, ep.getPropertyType( "homeAddress" ) ); - Type homeAddressCityType = findCompositeAttributeType( homeAddressType, "city" ); - assertTyping( StringType.class, homeAddressCityType ); - } - finally { - emf.close(); - } + final EntityPersister ep = sessionFactory().getEntityPersister( Person.class.getName() ); + CompositeType homeAddressType = assertTyping( CompositeType.class, ep.getPropertyType( "homeAddress" ) ); + Type homeAddressCityType = findCompositeAttributeType( homeAddressType, "city" ); + assertTyping( StringType.class, homeAddressCityType ); } public Type findCompositeAttributeType(CompositeType compositeType, String attributeName) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleOverriddenConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleOverriddenConverterTest.java new file mode 100644 index 0000000000..ba71f73222 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleOverriddenConverterTest.java @@ -0,0 +1,64 @@ +/* + * 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 . + */ +package org.hibernate.test.converter; + +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.type.StringType; +import org.hibernate.type.Type; + +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; + +/** + * Tests MappedSuperclass/Entity overriding of Convert definitions + * + * @author Steve Ebersole + */ +public class SimpleOverriddenConverterTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Super.class, Sub.class }; + } + + @Override + protected boolean createSchema() { + return false; + } + + /** + * Test outcome of annotations exclusively. + */ + @Test + public void testSimpleConvertOverrides() { + final EntityPersister ep = sessionFactory().getEntityPersister( Sub.class.getName() ); + + Type type = ep.getPropertyType( "it" ); + assertTyping( StringType.class, type ); + } + + @MappedSuperclass + public static class Super { + @Id + public Integer id; + @Convert(converter = SillyStringConverter.class) + public String it; + } + + @Entity(name = "Sub") + @Convert( attributeName = "it", disableConversion = true ) + public static class Sub extends Super { + + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleXmlOverriddenTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleXmlOverriddenTest.java new file mode 100644 index 0000000000..8eb17ef3c3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/SimpleXmlOverriddenTest.java @@ -0,0 +1,111 @@ +/* + * 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 . + */ +package org.hibernate.test.converter; + +import javax.persistence.Convert; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.mapping.PersistentClass; +import org.hibernate.type.StringType; +import org.hibernate.type.Type; +import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; + +import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; + +/** + * Test simple application of Convert annotation via XML. + * + * @author Steve Ebersole + */ +public class SimpleXmlOverriddenTest extends BaseUnitTestCase { + private StandardServiceRegistry ssr; + + @Before + public void before() { + ssr = new StandardServiceRegistryBuilder().build(); + } + + @After + public void after() { + if ( ssr != null ) { + StandardServiceRegistryBuilder.destroy( ssr ); + } + } + + /** + * A baseline test, with an explicit @Convert annotation that should be in effect + */ + @Test + public void baseline() { + Metadata metadata = new MetadataSources( ssr ) + .addAnnotatedClass( TheEntity.class ) + .buildMetadata(); + + PersistentClass pc = metadata.getEntityBinding( TheEntity.class.getName() ); + Type type = pc.getProperty( "it" ).getType(); + AttributeConverterTypeAdapter adapter = assertTyping( AttributeConverterTypeAdapter.class, type ); + assertTyping( SillyStringConverter.class, adapter.getAttributeConverter() ); + } + + /** + * Test outcome of applying overrides via orm.xml, specifically at the attribute level + */ + @Test + public void testDefinitionAtAttributeLevel() { + // NOTE : simple-override.xml applied disable-conversion="true" at the attribute-level + Metadata metadata = new MetadataSources( ssr ) + .addAnnotatedClass( TheEntity.class ) + .addResource( "org/hibernate/test/converter/simple-override.xml" ) + .buildMetadata(); + + PersistentClass pc = metadata.getEntityBinding( TheEntity.class.getName() ); + Type type = pc.getProperty( "it" ).getType(); + assertTyping( StringType.class, type ); + } + + /** + * Test outcome of applying overrides via orm.xml, specifically at the entity level + */ + @Test + public void testDefinitionAtEntityLevel() { + // NOTE : simple-override2.xml applied disable-conversion="true" at the entity-level + Metadata metadata = new MetadataSources( ssr ) + .addAnnotatedClass( TheEntity2.class ) + .addResource( "org/hibernate/test/converter/simple-override2.xml" ) + .buildMetadata(); + + PersistentClass pc = metadata.getEntityBinding( TheEntity2.class.getName() ); + Type type = pc.getProperty( "it" ).getType(); + assertTyping( StringType.class, type ); + } + + @Entity(name="TheEntity") + public static class TheEntity { + @Id + public Integer id; + @Convert(converter = SillyStringConverter.class) + public String it; + } + + @Entity(name="TheEntity2") + @Convert( attributeName = "it", converter = SillyStringConverter.class ) + public static class TheEntity2 { + @Id + public Integer id; + public String it; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/StringClobConverter.java b/hibernate-core/src/test/java/org/hibernate/test/converter/StringClobConverter.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/type/StringClobConverter.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/StringClobConverter.java index 5b7f3cb9fa..59c9b5f524 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/StringClobConverter.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/StringClobConverter.java @@ -4,10 +4,9 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type; +package org.hibernate.test.converter; import java.sql.Clob; - import javax.persistence.AttributeConverter; import javax.persistence.Converter; diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/XmlWithExplicitConvertAnnotationsTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/XmlWithExplicitConvertAnnotationsTest.java similarity index 52% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/XmlWithExplicitConvertAnnotationsTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/XmlWithExplicitConvertAnnotationsTest.java index 995b28a4dd..e850f174ee 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/XmlWithExplicitConvertAnnotationsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/XmlWithExplicitConvertAnnotationsTest.java @@ -4,32 +4,24 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter; import java.net.MalformedURLException; -import java.util.Arrays; import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.Convert; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.Id; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.Type; import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; @@ -42,22 +34,37 @@ import static org.junit.Assert.assertTrue; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8812" ) -public class XmlWithExplicitConvertAnnotationsTest { +public class XmlWithExplicitConvertAnnotationsTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Entity1.class }; + } + + @Override + protected String[] getXmlFiles() { + return new String[] { "org/hibernate/test/converter/mixed.xml" }; + } // NOTE : essentially the same exact test as ExplicitDateConvertersTest, but here we will mix annotations and xml - static int callsToConverter = 0; + static boolean convertToDatabaseColumnCalled = false; + static boolean convertToEntityAttributeCalled = false; + + private void resetFlags() { + convertToDatabaseColumnCalled = false; + convertToEntityAttributeCalled = false; + } public static class LongToDateConverter implements AttributeConverter { @Override public Long convertToDatabaseColumn(Date attribute) { - callsToConverter++; + convertToDatabaseColumnCalled = true; return attribute.getTime(); } @Override public Date convertToEntityAttribute(Long dbData) { - callsToConverter++; + convertToEntityAttributeCalled = true; return new Date( dbData ); } } @@ -90,56 +97,38 @@ public class XmlWithExplicitConvertAnnotationsTest { @Test public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName() ); - } - - @Override - public List getMappingFileNames() { - return Arrays.asList( "org/hibernate/jpa/test/convert/mixed.xml" ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); + final EntityPersister ep = sessionFactory().getEntityPersister( Entity1.class.getName() ); final Type theDatePropertyType = ep.getPropertyType( "theDate" ); - final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); + final AttributeConverterTypeAdapter type = assertTyping( + AttributeConverterTypeAdapter.class, + theDatePropertyType + ); assertTyping( LongToDateConverter.class, type.getAttributeConverter() ); - int previousCallCount = 0; + resetFlags(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "1", new Date() ) ); - em.getTransaction().commit(); - em.close(); + Session session = openSession(); + session.getTransaction().begin(); + session.persist( new Entity1( 1, "1", new Date() ) ); + session.getTransaction().commit(); + session.close(); - assertTrue( previousCallCount < callsToConverter ); - previousCallCount = callsToConverter; + assertTrue( convertToDatabaseColumnCalled ); + resetFlags(); - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.find( Entity1.class, 1 ); - em.getTransaction().commit(); - em.close(); + session = openSession(); + session.getTransaction().begin(); + session.get( Entity1.class, 1 ); + session.getTransaction().commit(); + session.close(); - assertTrue( previousCallCount < callsToConverter ); + assertTrue( convertToEntityAttributeCalled ); - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Entity1" ).executeUpdate(); + session.getTransaction().commit(); + session.close(); } } diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionCompositeElementConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionCompositeElementConversionTest.java similarity index 63% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionCompositeElementConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionCompositeElementConversionTest.java index dc3a8b68ef..3ae560e885 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionCompositeElementConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionCompositeElementConversionTest.java @@ -4,35 +4,26 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter.elementCollection; +import java.util.HashSet; +import java.util.Set; import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; import javax.persistence.Converter; import javax.persistence.ElementCollection; import javax.persistence.Embeddable; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; - -import org.junit.Test; +import org.hibernate.Session; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -40,45 +31,34 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8529" ) -public class CollectionCompositeElementConversionTest extends BaseUnitTestCase { +public class CollectionCompositeElementConversionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Disguise.class, ColorTypeConverter.class }; + } + @Test public void testElementCollectionConversion() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Disguise.class.getName(), ColorTypeConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + Disguise disguise = new Disguise( 1 ); + disguise.traits.add( new Traits( ColorType.BLUE, ColorType.RED ) ); + session.persist( disguise ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + session = openSession(); + session.getTransaction().begin(); + assertEquals( 1, session.get( Disguise.class, 1 ).traits.size() ); + session.getTransaction().commit(); + session.close(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - Disguise disguise = new Disguise( 1 ); - disguise.traits.add( new Traits( ColorType.BLUE, ColorType.RED ) ); - em.persist( disguise ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals( 1, em.find( Disguise.class, 1 ).traits.size() ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - disguise = em.find( Disguise.class, 1 ); - em.remove( disguise ); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + disguise = session.get( Disguise.class, 1 ); + session.delete( disguise ); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Disguise" ) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementConversionTest.java similarity index 62% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementConversionTest.java index 09ab318cd2..fe8b988228 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementConversionTest.java @@ -4,36 +4,27 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter.elementCollection; +import java.util.HashSet; +import java.util.Set; import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.Converter; import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.persistence.UniqueConstraint; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; - -import org.junit.Test; +import org.hibernate.Session; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -41,45 +32,34 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8529" ) -public class CollectionElementConversionTest extends BaseUnitTestCase { +public class CollectionElementConversionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Customer.class, ColorTypeConverter.class }; + } + @Test public void testElementCollectionConversion() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Customer.class.getName(), ColorTypeConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + Customer customer = new Customer( 1 ); + customer.colors.add( ColorType.BLUE ); + session.persist( customer ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + session = openSession(); + session.getTransaction().begin(); + assertEquals( 1, session.get( Customer.class, 1 ).colors.size() ); + session.getTransaction().commit(); + session.close(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - Customer customer = new Customer( 1 ); - customer.colors.add( ColorType.BLUE ); - em.persist( customer ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals( 1, em.find( Customer.class, 1 ).colors.size() ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - customer = em.find( Customer.class, 1 ); - em.remove( customer ); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + customer = session.get( Customer.class, 1 ); + session.delete( customer ); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Customer" ) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementExplicitConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementExplicitConversionTest.java similarity index 64% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementExplicitConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementExplicitConversionTest.java index fcc5c4653a..b56f044109 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/CollectionElementExplicitConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/CollectionElementExplicitConversionTest.java @@ -4,13 +4,9 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter.elementCollection; -import java.util.Arrays; -import java.util.HashMap; import java.util.HashSet; -import java.util.List; -import java.util.Map; import java.util.Set; import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; @@ -19,20 +15,16 @@ import javax.persistence.Convert; import javax.persistence.Converter; import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.persistence.UniqueConstraint; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; +import org.hibernate.Session; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -41,45 +33,35 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8529" ) -public class CollectionElementExplicitConversionTest extends BaseUnitTestCase { +public class CollectionElementExplicitConversionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + // NOTE : ColorTypeConverter is autoApply=false here + return new Class[] { Customer.class, ColorTypeConverter.class }; + } + @Test public void testElementCollectionConversion() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Customer.class.getName(), ColorTypeConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + Customer customer = new Customer( 1 ); + customer.colors.add( ColorType.BLUE ); + session.persist( customer ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + session = openSession(); + session.getTransaction().begin(); + assertEquals( 1, session.get( Customer.class, 1 ).colors.size() ); + session.getTransaction().commit(); + session.close(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - Customer customer = new Customer( 1 ); - customer.colors.add( ColorType.BLUE ); - em.persist( customer ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals( 1, em.find( Customer.class, 1 ).colors.size() ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - customer = em.find( Customer.class, 1 ); - em.remove( customer ); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + customer = session.get( Customer.class, 1 ); + session.delete( customer ); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Customer" ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/converter/ElementCollectionTests.java b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/ElementCollectionTests.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/type/converter/ElementCollectionTests.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/ElementCollectionTests.java index 155c0883ae..ce72adae15 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/converter/ElementCollectionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/elementCollection/ElementCollectionTests.java @@ -4,14 +4,13 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type.converter; +package org.hibernate.test.converter.elementCollection; import java.net.MalformedURLException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; - import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; import javax.persistence.Column; @@ -26,7 +25,6 @@ import javax.persistence.MapKeyColumn; import javax.persistence.Table; import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.mapping.Collection; import org.hibernate.mapping.IndexedCollection; import org.hibernate.mapping.PersistentClass; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/ParameterizedAttributeConverterParameterTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/generics/ParameterizedAttributeConverterParameterTypeTest.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/test/type/ParameterizedAttributeConverterParameterTypeTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/generics/ParameterizedAttributeConverterParameterTypeTest.java index 89be89aaed..1bfd81c074 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/ParameterizedAttributeConverterParameterTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/generics/ParameterizedAttributeConverterParameterTypeTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.type; +package org.hibernate.test.converter.generics; import java.util.ArrayList; import java.util.List; diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterOnSuperclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/inheritence/AttributeConverterOnSuperclassTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterOnSuperclassTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/inheritence/AttributeConverterOnSuperclassTest.java index bd7ec72f9f..979dc51562 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/type/AttributeConverterOnSuperclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/inheritence/AttributeConverterOnSuperclassTest.java @@ -1,16 +1,16 @@ -package org.hibernate.test.type; - -import static org.junit.Assert.assertEquals; +package org.hibernate.test.converter.inheritence; import java.util.List; - import javax.persistence.AttributeConverter; import org.hibernate.cfg.AttributeConverterDefinition; + import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseUnitTestCase; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Test the ability to interpret and understand AttributeConverter impls when the base class does not * explicitly implement AttributeConverter but implements it via an interface or superclass. This also diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapElementConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/map/MapElementConversionTest.java similarity index 62% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapElementConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/map/MapElementConversionTest.java index c1bc63d061..4d571a9c5f 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapElementConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/map/MapElementConversionTest.java @@ -4,34 +4,27 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter.map; +import java.util.HashMap; +import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.Converter; import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.MapKeyColumn; import javax.persistence.Table; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; - -import org.junit.Test; +import org.hibernate.Session; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -39,45 +32,34 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8529" ) -public class MapElementConversionTest extends BaseUnitTestCase { +public class MapElementConversionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Customer.class, ColorTypeConverter.class }; + } + @Test public void testElementCollectionConversion() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Customer.class.getName(), ColorTypeConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + Customer customer = new Customer( 1 ); + customer.colors.put( "eyes", ColorType.BLUE ); + session.persist( customer ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + session = openSession(); + session.getTransaction().begin(); + assertEquals( 1, session.get( Customer.class, 1 ).colors.size() ); + session.getTransaction().commit(); + session.close(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - Customer customer = new Customer( 1 ); - customer.colors.put( "eyes", ColorType.BLUE ); - em.persist( customer ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals( 1, em.find( Customer.class, 1 ).colors.size() ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - customer = em.find( Customer.class, 1 ); - em.remove( customer ); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + customer = session.get( Customer.class, 1 ); + session.delete( customer ); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Customer" ) diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapKeyConversionTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/map/MapKeyConversionTest.java similarity index 61% rename from hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapKeyConversionTest.java rename to hibernate-core/src/test/java/org/hibernate/test/converter/map/MapKeyConversionTest.java index cec7d1ccb2..5911163f6e 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/MapKeyConversionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/map/MapKeyConversionTest.java @@ -4,32 +4,25 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.jpa.test.convert; +package org.hibernate.test.converter.map; +import java.util.HashMap; +import java.util.Map; import javax.persistence.AttributeConverter; import javax.persistence.CollectionTable; import javax.persistence.Converter; import javax.persistence.ElementCollection; import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; - -import org.junit.Test; +import org.hibernate.Session; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -37,45 +30,34 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ @TestForIssue( jiraKey = "HHH-8529" ) -public class MapKeyConversionTest extends BaseUnitTestCase { +public class MapKeyConversionTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Customer.class, ColorTypeConverter.class }; + } + @Test public void testElementCollectionConversion() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Customer.class.getName(), ColorTypeConverter.class.getName() ); - } - }; + Session session = openSession(); + session.getTransaction().begin(); + Customer customer = new Customer( 1 ); + customer.colors.put( ColorType.BLUE, "favorite" ); + session.persist( customer ); + session.getTransaction().commit(); + session.close(); - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); + session = openSession(); + session.getTransaction().begin(); + assertEquals( 1, session.get( Customer.class, 1 ).colors.size() ); + session.getTransaction().commit(); + session.close(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - Customer customer = new Customer( 1 ); - customer.colors.put( ColorType.BLUE, "favorite" ); - em.persist( customer ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals( 1, em.find( Customer.class, 1 ).colors.size() ); - em.getTransaction().commit(); - em.close(); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - customer = em.find( Customer.class, 1 ); - em.remove( customer ); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } + session = openSession(); + session.getTransaction().begin(); + customer = session.get( Customer.class, 1 ); + session.delete( customer ); + session.getTransaction().commit(); + session.close(); } @Entity( name = "Customer" ) diff --git a/hibernate-core/src/test/resources/log4j.properties b/hibernate-core/src/test/resources/log4j.properties index 7ffc73c6fb..07c8869084 100644 --- a/hibernate-core/src/test/resources/log4j.properties +++ b/hibernate-core/src/test/resources/log4j.properties @@ -48,6 +48,8 @@ log4j.logger.org.hibernate.loader.plan2.exec.spi.EntityLoadQueryDetails=debug log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=info log4j.logger.org.hibernate.boot.model.source.internal.hbm.ModelBinder=debug +log4j.logger.org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry=debug + ### When entity copy merge functionality is enabled using: ### hibernate.event.merge.entity_copy_observer=log, the following will diff --git a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/mixed.xml b/hibernate-core/src/test/resources/org/hibernate/test/converter/mixed.xml similarity index 76% rename from hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/mixed.xml rename to hibernate-core/src/test/resources/org/hibernate/test/converter/mixed.xml index cff8b40b3b..b976ced977 100644 --- a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/mixed.xml +++ b/hibernate-core/src/test/resources/org/hibernate/test/converter/mixed.xml @@ -10,13 +10,13 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" version="2.0"> - + - + diff --git a/hibernate-core/src/test/resources/org/hibernate/test/type/orm.xml b/hibernate-core/src/test/resources/org/hibernate/test/converter/orm.xml similarity index 83% rename from hibernate-core/src/test/resources/org/hibernate/test/type/orm.xml rename to hibernate-core/src/test/resources/org/hibernate/test/converter/orm.xml index 4afa3e2880..a4982888ef 100644 --- a/hibernate-core/src/test/resources/org/hibernate/test/type/orm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/test/converter/orm.xml @@ -9,5 +9,5 @@ org.hibernate.test.type - + diff --git a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override.xml b/hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override.xml similarity index 82% rename from hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override.xml rename to hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override.xml index 83a03baf78..ba63b4eda6 100644 --- a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override.xml +++ b/hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override.xml @@ -11,10 +11,10 @@ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm/orm_2_1.xsd" version="2.1"> - + org.hibernate.jpa.test.convert - + diff --git a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override2.xml b/hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override2.xml similarity index 81% rename from hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override2.xml rename to hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override2.xml index 11f78b3655..8ce3a766cf 100644 --- a/hibernate-entitymanager/src/test/resources/org/hibernate/jpa/test/convert/simple-override2.xml +++ b/hibernate-core/src/test/resources/org/hibernate/test/converter/simple-override2.xml @@ -10,10 +10,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm/orm_2_1.xsd" version="2.1"> - + org.hibernate.jpa.test.convert - + \ No newline at end of file diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitDateConvertersTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitDateConvertersTest.java deleted file mode 100644 index 02575c11d9..0000000000 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitDateConvertersTest.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.jpa.test.convert; - -import java.net.MalformedURLException; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.persistence.AttributeConverter; -import javax.persistence.Convert; -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Id; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.type.Type; -import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; - -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertTrue; - -/** - * @author Steve Ebersole - */ -@TestForIssue( jiraKey = "HHH-8807" ) -public class ExplicitDateConvertersTest { - - // NOTE : initially unable to reproduce the reported problem - - static int callsToConverter = 0; - - public static class LongToDateConverter implements AttributeConverter { - @Override - public Long convertToDatabaseColumn(Date attribute) { - callsToConverter++; - return attribute.getTime(); - } - - @Override - public Date convertToEntityAttribute(Long dbData) { - callsToConverter++; - return new Date( dbData ); - } - } - - @Entity( name = "Entity1" ) - public static class Entity1 { - @Id - private Integer id; - private String name; - @Convert( converter = LongToDateConverter.class ) - private Date theDate; - - public Entity1() { - } - - public Entity1(Integer id, String name, Date theDate) { - this.id = id; - this.name = name; - this.theDate = theDate; - } - } - - @Test - public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); - final Type theDatePropertyType = ep.getPropertyType( "theDate" ); - final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); - assertTyping( LongToDateConverter.class, type.getAttributeConverter() ); - - int previousCallCount = 0; - - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "1", new Date() ) ); - em.getTransaction().commit(); - em.close(); - - assertTrue( previousCallCount < callsToConverter ); - previousCallCount = callsToConverter; - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.find( Entity1.class, 1 ); - em.getTransaction().commit(); - em.close(); - - assertTrue( previousCallCount < callsToConverter ); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } - } -} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitEnumConvertersTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitEnumConvertersTest.java deleted file mode 100644 index 04e94b4603..0000000000 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/ExplicitEnumConvertersTest.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.jpa.test.convert; - -import java.net.MalformedURLException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.persistence.AttributeConverter; -import javax.persistence.Convert; -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Id; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.type.Type; -import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; - -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; -import static org.junit.Assert.assertTrue; - -/** - * @author Steve Ebersole - */ -@TestForIssue( jiraKey = "HHH-8809" ) -public class ExplicitEnumConvertersTest { - - // NOTE : initially unable to reproduce the reported problem - - public static enum MediaType { - MUSIC, - VIDEO, - PHOTO, - MUSIC_STREAM, - VIDEO_STREAM - } - - public static class MediaTypeConverter implements AttributeConverter { - @Override - public String convertToDatabaseColumn(MediaType attribute) { - callsToConverter++; - return attribute.name(); - } - - @Override - public MediaType convertToEntityAttribute(String dbData) { - callsToConverter++; - return MediaType.valueOf( dbData ); - } - } - - static int callsToConverter = 0; - - @Entity( name = "Entity1" ) - public static class Entity1 { - @Id - private Integer id; - private String name; - @Convert( converter = MediaTypeConverter.class ) - private MediaType mediaType; - - public Entity1() { - } - - public Entity1(Integer id, String name, MediaType mediaType) { - this.id = id; - this.name = name; - this.mediaType = mediaType; - } - } - - @Test - public void testSimpleConvertUsage() throws MalformedURLException { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Entity1.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); - final Type theDatePropertyType = ep.getPropertyType( "mediaType" ); - final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); - assertTyping( MediaTypeConverter.class, type.getAttributeConverter() ); - - int previousCallCount = 0; - - try { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - em.persist( new Entity1( 1, "300", MediaType.VIDEO ) ); - em.getTransaction().commit(); - em.close(); - - assertTrue( previousCallCount < callsToConverter ); - previousCallCount = callsToConverter; - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.find( Entity1.class, 1 ); - em.getTransaction().commit(); - em.close(); - - assertTrue( previousCallCount < callsToConverter ); - - em = emf.createEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Entity1" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); - } - finally { - emf.close(); - } - } -} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleOverriddenConverterTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleOverriddenConverterTest.java deleted file mode 100644 index 2d8973efbd..0000000000 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleOverriddenConverterTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.jpa.test.convert; - -import javax.persistence.Convert; -import javax.persistence.Entity; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Id; -import javax.persistence.MappedSuperclass; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.type.StringType; -import org.hibernate.type.Type; - -import org.junit.Test; - -import org.hibernate.testing.junit4.BaseUnitTestCase; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; - -/** - * Tests MappedSuperclass/Entity overriding of Convert definitions - * - * @author Steve Ebersole - */ -public class SimpleOverriddenConverterTest extends BaseUnitTestCase { - /** - * Test outcome of annotations exclusively. - */ - @Test - public void testSimpleConvertOverrides() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( Super.class.getName(), Sub.class.getName() ); - } - }; - - final Map settings = new HashMap(); - settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - - final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class ); - try { - final EntityPersister ep = sfi.getEntityPersister( Sub.class.getName() ); - - Type type = ep.getPropertyType( "it" ); - assertTyping( StringType.class, type ); - } - finally { - emf.close(); - } - } - - @MappedSuperclass - public static class Super { - @Id - public Integer id; - @Convert(converter = SillyStringConverter.class) - public String it; - } - - @Entity(name = "Sub") - @Convert( attributeName = "it", disableConversion = true ) - public static class Sub extends Super { - - } - -} diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleXmlOverriddenTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleXmlOverriddenTest.java deleted file mode 100644 index c30d85c114..0000000000 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/convert/SimpleXmlOverriddenTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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 . - */ -package org.hibernate.jpa.test.convert; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.persistence.Convert; -import javax.persistence.Entity; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Id; - -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; -import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.type.StringType; -import org.hibernate.type.Type; -import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; - -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.Test; - -import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; - -/** - * Test simple application of Convert annotation via XML. - * - * @author Steve Ebersole - */ -public class SimpleXmlOverriddenTest extends BaseUnitTestCase { - /** - * A baseline test, with an explicit @Convert annotation that should be in effect - */ - @Test - public void baseline() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( TheEntity.class.getName() ); - } - - // No mapping file should mean that the converter is applied - }; - - final Map settings = Collections.emptyMap(); - - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - - final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class ); - try { - final EntityPersister ep = sfi.getEntityPersister( TheEntity.class.getName() ); - - Type type = ep.getPropertyType( "it" ); - AttributeConverterTypeAdapter adapter = assertTyping( AttributeConverterTypeAdapter.class, type ); - assertTyping( SillyStringConverter.class, adapter.getAttributeConverter() ); - } - finally { - emf.close(); - } - } - - /** - * Test outcome of applying overrides via orm.xml, specifically at the attribute level - */ - @Test - public void testDefinitionAtAttributeLevel() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( TheEntity.class.getName() ); - } - - @Override - public List getMappingFileNames() { - return Arrays.asList( "org/hibernate/jpa/test/convert/simple-override.xml" ); - } - }; - - final Map settings = new HashMap(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - - final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class ); - try { - final EntityPersister ep = sfi.getEntityPersister( TheEntity.class.getName() ); - - Type type = ep.getPropertyType( "it" ); - assertTyping( StringType.class, type ); - } - finally { - emf.close(); - } - } - - /** - * Test outcome of applying overrides via orm.xml, specifically at the entity level - */ - @Test - public void testDefinitionAtEntityLevel() { - final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { - @Override - public List getManagedClassNames() { - return Arrays.asList( TheEntity2.class.getName() ); - } - - @Override - public List getMappingFileNames() { - return Arrays.asList( "org/hibernate/jpa/test/convert/simple-override2.xml" ); - } - }; - - final Map settings = new HashMap(); - EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); - - final SessionFactoryImplementor sfi = emf.unwrap( SessionFactoryImplementor.class ); - try { - final EntityPersister ep = sfi.getEntityPersister( TheEntity2.class.getName() ); - - Type type = ep.getPropertyType( "it" ); - assertTyping( StringType.class, type ); - } - finally { - emf.close(); - } - } - - @Entity(name="TheEntity") - public static class TheEntity { - @Id - public Integer id; - @Convert(converter = SillyStringConverter.class) - public String it; - } - - @Entity(name="TheEntity2") - @Convert( attributeName = "it", converter = SillyStringConverter.class ) - public static class TheEntity2 { - @Id - public Integer id; - public String it; - } -} diff --git a/hibernate-java8/src/main/java/org/hibernate/type/Java8DateTimeTypeContributor.java b/hibernate-java8/src/main/java/org/hibernate/type/Java8DateTimeTypeContributor.java index 2d4f75ceb4..e577cd3097 100644 --- a/hibernate-java8/src/main/java/org/hibernate/type/Java8DateTimeTypeContributor.java +++ b/hibernate-java8/src/main/java/org/hibernate/type/Java8DateTimeTypeContributor.java @@ -9,6 +9,14 @@ package org.hibernate.type; import org.hibernate.boot.model.TypeContributions; import org.hibernate.boot.model.TypeContributor; import org.hibernate.service.ServiceRegistry; +import org.hibernate.type.descriptor.java.DurationJavaDescriptor; +import org.hibernate.type.descriptor.java.InstantJavaDescriptor; +import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry; +import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor; +import org.hibernate.type.descriptor.java.LocalDateTimeJavaDescriptor; +import org.hibernate.type.descriptor.java.OffsetDateTimeJavaDescriptor; +import org.hibernate.type.descriptor.java.OffsetTimeJavaDescriptor; +import org.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor; /** * TypeContributor for adding Java8 Date/Time specific Type implementations @@ -18,16 +26,23 @@ import org.hibernate.service.ServiceRegistry; public class Java8DateTimeTypeContributor implements TypeContributor { @Override public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { + // register the Java type descriptors + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( DurationJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( InstantJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( LocalDateJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( LocalDateTimeJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( OffsetDateTimeJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( OffsetTimeJavaDescriptor.INSTANCE ); + JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( ZonedDateTimeJavaDescriptor.INSTANCE ); + + // register the Hibernate type mappings + typeContributions.contributeType( DurationType.INSTANCE ); + typeContributions.contributeType( InstantType.INSTANCE ); typeContributions.contributeType( LocalDateTimeType.INSTANCE ); typeContributions.contributeType( LocalDateType.INSTANCE ); typeContributions.contributeType( LocalTimeType.INSTANCE ); - - typeContributions.contributeType( InstantType.INSTANCE ); - - typeContributions.contributeType( ZonedDateTimeType.INSTANCE ); typeContributions.contributeType( OffsetDateTimeType.INSTANCE ); typeContributions.contributeType( OffsetTimeType.INSTANCE ); - - typeContributions.contributeType( DurationType.INSTANCE ); + typeContributions.contributeType( ZonedDateTimeType.INSTANCE ); } } diff --git a/hibernate-spatial/src/main/java/org/hibernate/spatial/GeolatteGeometryJavaTypeDescriptor.java b/hibernate-spatial/src/main/java/org/hibernate/spatial/GeolatteGeometryJavaTypeDescriptor.java index a47a714937..242a965e83 100644 --- a/hibernate-spatial/src/main/java/org/hibernate/spatial/GeolatteGeometryJavaTypeDescriptor.java +++ b/hibernate-spatial/src/main/java/org/hibernate/spatial/GeolatteGeometryJavaTypeDescriptor.java @@ -13,6 +13,7 @@ import org.geolatte.geom.jts.JTS; import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.java.AbstractTypeDescriptor; +import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry; /** * Descriptor for geolatte-geom {@code Geometry}s. @@ -32,6 +33,7 @@ public class GeolatteGeometryJavaTypeDescriptor extends AbstractTypeDescriptor