HHH-8809 - Support @Converter on Enumerations

(cherry picked from commit dd08640725)
This commit is contained in:
Steve Ebersole 2014-01-09 13:49:18 -06:00
parent 37255e688a
commit 5620744c53
1 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,148 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
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.assertEquals;
/**
* @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() );
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist( new Entity1( 1, "300", MediaType.VIDEO ) );
em.getTransaction().commit();
em.close();
assertEquals( 1, callsToConverter );
em = emf.createEntityManager();
em.getTransaction().begin();
em.find( Entity1.class, 1 );
em.getTransaction().commit();
em.close();
assertEquals( 2, callsToConverter );
em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery( "delete Entity1" ).executeUpdate();
em.getTransaction().commit();
em.close();
}
finally {
emf.close();
}
}
}