HHH-8807 - Wrong type mapped when using AttributeConverter

This commit is contained in:
Steve Ebersole 2014-01-09 13:35:04 -06:00
parent 09b2f11afb
commit 20d813f457
1 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,140 @@
/*
* 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-8807" )
public class ExplicitDateConvertersTest {
// NOTE : initially unable to reproduce the reported problem
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 );
}
}
static int callsToConverter = 0;
@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() );
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist( new Entity1( 1, "1", new Date() ) );
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();
}
}
}