|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2014, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.jpa.test.convert; |
| 25 | + |
| 26 | +import java.net.MalformedURLException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Date; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import javax.persistence.AttributeConverter; |
| 33 | +import javax.persistence.Convert; |
| 34 | +import javax.persistence.Entity; |
| 35 | +import javax.persistence.EntityManager; |
| 36 | +import javax.persistence.EntityManagerFactory; |
| 37 | +import javax.persistence.Id; |
| 38 | + |
| 39 | +import org.hibernate.cfg.AvailableSettings; |
| 40 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 41 | +import org.hibernate.jpa.boot.spi.Bootstrap; |
| 42 | +import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter; |
| 43 | +import org.hibernate.persister.entity.EntityPersister; |
| 44 | +import org.hibernate.type.Type; |
| 45 | +import org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter; |
| 46 | + |
| 47 | +import org.hibernate.testing.TestForIssue; |
| 48 | +import org.junit.Test; |
| 49 | + |
| 50 | +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; |
| 51 | +import static org.junit.Assert.assertEquals; |
| 52 | + |
| 53 | +/** |
| 54 | + * @author Steve Ebersole |
| 55 | + */ |
| 56 | +@TestForIssue( jiraKey = "HHH-8809" ) |
| 57 | +public class ExplicitEnumConvertersTest { |
| 58 | + |
| 59 | + // NOTE : initially unable to reproduce the reported problem |
| 60 | + |
| 61 | + public static enum MediaType { |
| 62 | + MUSIC, |
| 63 | + VIDEO, |
| 64 | + PHOTO, |
| 65 | + MUSIC_STREAM, |
| 66 | + VIDEO_STREAM |
| 67 | + } |
| 68 | + |
| 69 | + public static class MediaTypeConverter implements AttributeConverter<MediaType,String> { |
| 70 | + @Override |
| 71 | + public String convertToDatabaseColumn(MediaType attribute) { |
| 72 | + callsToConverter++; |
| 73 | + return attribute.name(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public MediaType convertToEntityAttribute(String dbData) { |
| 78 | + callsToConverter++; |
| 79 | + return MediaType.valueOf( dbData ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + static int callsToConverter = 0; |
| 84 | + |
| 85 | + @Entity( name = "Entity1" ) |
| 86 | + public static class Entity1 { |
| 87 | + @Id |
| 88 | + private Integer id; |
| 89 | + private String name; |
| 90 | + @Convert( converter = MediaTypeConverter.class ) |
| 91 | + private MediaType mediaType; |
| 92 | + |
| 93 | + public Entity1() { |
| 94 | + } |
| 95 | + |
| 96 | + public Entity1(Integer id, String name, MediaType mediaType) { |
| 97 | + this.id = id; |
| 98 | + this.name = name; |
| 99 | + this.mediaType = mediaType; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testSimpleConvertUsage() throws MalformedURLException { |
| 105 | + final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { |
| 106 | + @Override |
| 107 | + public List<String> getManagedClassNames() { |
| 108 | + return Arrays.asList( Entity1.class.getName() ); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + final Map settings = new HashMap(); |
| 113 | + settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); |
| 114 | + |
| 115 | + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); |
| 116 | + final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); |
| 117 | + final Type theDatePropertyType = ep.getPropertyType( "mediaType" ); |
| 118 | + final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); |
| 119 | + assertTyping( MediaTypeConverter.class, type.getAttributeConverter() ); |
| 120 | + |
| 121 | + try { |
| 122 | + EntityManager em = emf.createEntityManager(); |
| 123 | + em.getTransaction().begin(); |
| 124 | + em.persist( new Entity1( 1, "300", MediaType.VIDEO ) ); |
| 125 | + em.getTransaction().commit(); |
| 126 | + em.close(); |
| 127 | + |
| 128 | + assertEquals( 1, callsToConverter ); |
| 129 | + |
| 130 | + em = emf.createEntityManager(); |
| 131 | + em.getTransaction().begin(); |
| 132 | + em.find( Entity1.class, 1 ); |
| 133 | + em.getTransaction().commit(); |
| 134 | + em.close(); |
| 135 | + |
| 136 | + assertEquals( 2, callsToConverter ); |
| 137 | + |
| 138 | + em = emf.createEntityManager(); |
| 139 | + em.getTransaction().begin(); |
| 140 | + em.createQuery( "delete Entity1" ).executeUpdate(); |
| 141 | + em.getTransaction().commit(); |
| 142 | + em.close(); |
| 143 | + } |
| 144 | + finally { |
| 145 | + emf.close(); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments