HHH-10129 - Evaluate AttributeConverter tests in hibernate-entitymanager for move to hibernate-core;

HHH-10154 - Change built-in JavaTypeDescriptor impls to not auto-register themselves with JavaTypeDescriptorRegistry
This commit is contained in:
Steve Ebersole 2015-09-30 12:36:57 -05:00
parent 14e207d692
commit 7af7ebaf87
41 changed files with 888 additions and 1071 deletions

View File

@ -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

View File

@ -48,8 +48,6 @@ public abstract class AbstractTypeDescriptor<T> implements JavaTypeDescriptor<T>
this.comparator = Comparable.class.isAssignableFrom( type )
? (Comparator<T>) ComparableComparator.INSTANCE
: null;
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
}
@Override

View File

@ -17,6 +17,8 @@ public class EnumJavaTypeDescriptor<T extends Enum> extends AbstractTypeDescript
@SuppressWarnings("unchecked")
protected EnumJavaTypeDescriptor(Class<T> type) {
super( type, ImmutableMutabilityPlan.INSTANCE );
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
}
@Override

View File

@ -27,13 +27,63 @@ public class JavaTypeDescriptorRegistry {
private ConcurrentHashMap<Class,JavaTypeDescriptor> descriptorsByClass = new ConcurrentHashMap<Class, JavaTypeDescriptor>();
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")

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.type.converter;
package org.hibernate.test.converter;
import java.sql.Types;
import javax.persistence.AttributeConverter;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<LocalDate, Date> {
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<String> 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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.type.converter;
package org.hibernate.test.converter;
import javax.persistence.AttributeConverter;
import javax.persistence.Column;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Date,Long> {
@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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<MediaType,String> {
@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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.type.converter;
package org.hibernate.test.converter;
import java.sql.Connection;
import java.sql.ResultSet;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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" )

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.jpa.test.convert;
package org.hibernate.test.converter;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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<URL,String> {
@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;
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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<URL,String> {
@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;
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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) {

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 {
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.type;
package org.hibernate.test.converter;
import java.sql.Clob;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Date,Long> {
@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<String> getManagedClassNames() {
return Arrays.asList( Entity1.class.getName() );
}
@Override
public List<String> 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();
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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" )

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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" )

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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" )

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.type;
package org.hibernate.test.converter.generics;
import java.util.ArrayList;
import java.util.List;

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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" )

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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" )

View File

@ -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

View File

@ -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">
<!-- Intended for use in org.hibernate.jpa.test.convert.XmlWithExplicitConvertAnnotationsTest -->
<!-- Intended for use in org.hibernate.test.converter.XmlWithExplicitConvertAnnotationsTest -->
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.hibernate.jpa.test.convert.XmlWithExplicitConvertAnnotationsTest$TestEntityListener"/>
<entity-listener class="org.hibernate.test.converter.XmlWithExplicitConvertAnnotationsTest$TestEntityListener"/>
</entity-listeners>
</persistence-unit-defaults>

View File

@ -9,5 +9,5 @@
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
version="2.1">
<package>org.hibernate.test.type</package>
<converter class="org.hibernate.test.type.StringClobConverter" auto-apply="true"/>
<converter class="org.hibernate.test.converter.StringClobConverter" auto-apply="true"/>
</entity-mappings>

View File

@ -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">
<!-- Intended for use in org.hibernate.jpa.test.convert.SimpleXmlOverriddenTest -->
<!-- Intended for use in org.hibernate.test.converter.SimpleXmlOverriddenTest -->
<package>org.hibernate.jpa.test.convert</package>
<entity class="org.hibernate.jpa.test.convert.SimpleXmlOverriddenTest$TheEntity">
<entity class="org.hibernate.test.converter.SimpleXmlOverriddenTest$TheEntity">
<attributes>
<basic name="it">
<convert disable-conversion="true" />

View File

@ -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">
<!-- Intended for use in org.hibernate.jpa.test.convert.SimpleXmlOverriddenTest -->
<!-- Intended for use in org.hibernate.test.converter.SimpleXmlOverriddenTest -->
<package>org.hibernate.jpa.test.convert</package>
<entity class="org.hibernate.jpa.test.convert.SimpleXmlOverriddenTest$TheEntity2">
<entity class="org.hibernate.test.converter.SimpleXmlOverriddenTest$TheEntity2">
<convert attribute-name="it" disable-conversion="true"/>
</entity>
</entity-mappings>

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<Date,Long> {
@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<String> 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();
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<MediaType,String> {
@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<String> 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();
}
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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 {
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String> 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<String> getManagedClassNames() {
return Arrays.asList( TheEntity.class.getName() );
}
@Override
public List<String> 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<String> getManagedClassNames() {
return Arrays.asList( TheEntity2.class.getName() );
}
@Override
public List<String> 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;
}
}

View File

@ -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 );
}
}

View File

@ -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<G
*/
public GeolatteGeometryJavaTypeDescriptor() {
super( Geometry.class );
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
}
@Override

View File

@ -15,6 +15,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.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptorRegistry;
/**
* Descriptor for JTS {@code Geometry}s.
@ -34,6 +35,7 @@ public class JTSGeometryJavaTypeDescriptor extends AbstractTypeDescriptor<Geomet
*/
public JTSGeometryJavaTypeDescriptor() {
super( Geometry.class );
JavaTypeDescriptorRegistry.INSTANCE.addDescriptor( this );
}
@Override