|
| 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-8807" ) |
| 57 | +public class ExplicitDateConvertersTest { |
| 58 | + |
| 59 | + // NOTE : initially unable to reproduce the reported problem |
| 60 | + |
| 61 | + public static class LongToDateConverter implements AttributeConverter<Date,Long> { |
| 62 | + @Override |
| 63 | + public Long convertToDatabaseColumn(Date attribute) { |
| 64 | + callsToConverter++; |
| 65 | + return attribute.getTime(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Date convertToEntityAttribute(Long dbData) { |
| 70 | + callsToConverter++; |
| 71 | + return new Date( dbData ); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + static int callsToConverter = 0; |
| 76 | + |
| 77 | + @Entity( name = "Entity1" ) |
| 78 | + public static class Entity1 { |
| 79 | + @Id |
| 80 | + private Integer id; |
| 81 | + private String name; |
| 82 | + @Convert( converter = LongToDateConverter.class ) |
| 83 | + private Date theDate; |
| 84 | + |
| 85 | + public Entity1() { |
| 86 | + } |
| 87 | + |
| 88 | + public Entity1(Integer id, String name, Date theDate) { |
| 89 | + this.id = id; |
| 90 | + this.name = name; |
| 91 | + this.theDate = theDate; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testSimpleConvertUsage() throws MalformedURLException { |
| 97 | + final PersistenceUnitDescriptorAdapter pu = new PersistenceUnitDescriptorAdapter() { |
| 98 | + @Override |
| 99 | + public List<String> getManagedClassNames() { |
| 100 | + return Arrays.asList( Entity1.class.getName() ); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + final Map settings = new HashMap(); |
| 105 | + settings.put( AvailableSettings.HBM2DDL_AUTO, "create-drop" ); |
| 106 | + |
| 107 | + EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder( pu, settings ).build(); |
| 108 | + final EntityPersister ep = emf.unwrap( SessionFactoryImplementor.class ).getEntityPersister( Entity1.class.getName() ); |
| 109 | + final Type theDatePropertyType = ep.getPropertyType( "theDate" ); |
| 110 | + final AttributeConverterTypeAdapter type = assertTyping( AttributeConverterTypeAdapter.class, theDatePropertyType ); |
| 111 | + assertTyping( LongToDateConverter.class, type.getAttributeConverter() ); |
| 112 | + |
| 113 | + try { |
| 114 | + EntityManager em = emf.createEntityManager(); |
| 115 | + em.getTransaction().begin(); |
| 116 | + em.persist( new Entity1( 1, "1", new Date() ) ); |
| 117 | + em.getTransaction().commit(); |
| 118 | + em.close(); |
| 119 | + |
| 120 | + assertEquals( 1, callsToConverter ); |
| 121 | + |
| 122 | + em = emf.createEntityManager(); |
| 123 | + em.getTransaction().begin(); |
| 124 | + em.find( Entity1.class, 1 ); |
| 125 | + em.getTransaction().commit(); |
| 126 | + em.close(); |
| 127 | + |
| 128 | + assertEquals( 2, callsToConverter ); |
| 129 | + |
| 130 | + em = emf.createEntityManager(); |
| 131 | + em.getTransaction().begin(); |
| 132 | + em.createQuery( "delete Entity1" ).executeUpdate(); |
| 133 | + em.getTransaction().commit(); |
| 134 | + em.close(); |
| 135 | + } |
| 136 | + finally { |
| 137 | + emf.close(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments