|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.converter; |
| 8 | + |
| 9 | +import javax.persistence.AttributeConverter; |
| 10 | +import javax.persistence.Convert; |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.GeneratedValue; |
| 13 | +import javax.persistence.Id; |
| 14 | +import javax.persistence.Table; |
| 15 | +import javax.persistence.TemporalType; |
| 16 | +import java.io.Serializable; |
| 17 | +import java.util.Date; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import org.hibernate.Session; |
| 21 | +import org.hibernate.query.Query; |
| 22 | + |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.hibernate.testing.TestForIssue; |
| 26 | +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 27 | + |
| 28 | +import static org.hamcrest.CoreMatchers.is; |
| 29 | +import static org.junit.Assert.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Andrea Boriero |
| 33 | + */ |
| 34 | +@TestForIssue(jiraKey = "HHH-10959") |
| 35 | +public class LongToDateConversionTest extends BaseCoreFunctionalTestCase { |
| 36 | + |
| 37 | + @Override |
| 38 | + protected Class<?>[] getAnnotatedClasses() { |
| 39 | + return new Class[] {TestEntity.class}; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void prepareTest() throws Exception { |
| 44 | + try (Session session = openSession()) { |
| 45 | + session.getTransaction().begin(); |
| 46 | + TestEntity entity = new TestEntity(); |
| 47 | + entity.setDate( new DateAttribute( System.currentTimeMillis() ) ); |
| 48 | + try { |
| 49 | + session.persist( entity ); |
| 50 | + session.getTransaction().commit(); |
| 51 | + } |
| 52 | + catch (Exception e) { |
| 53 | + if ( session.getTransaction().isActive() ) { |
| 54 | + session.getTransaction().rollback(); |
| 55 | + } |
| 56 | + throw e; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected boolean isCleanupTestDataRequired() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void cleanupTestData() throws Exception { |
| 68 | + try (Session session = openSession()) { |
| 69 | + session.getTransaction().begin(); |
| 70 | + try { |
| 71 | + session.createQuery( "delete from TestEntity" ).executeUpdate(); |
| 72 | + session.getTransaction().commit(); |
| 73 | + } |
| 74 | + catch (Exception e) { |
| 75 | + if ( session.getTransaction().isActive() ) { |
| 76 | + session.getTransaction().rollback(); |
| 77 | + } |
| 78 | + throw e; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testSetParameter() throws Exception { |
| 85 | + try (Session session = openSession()) { |
| 86 | + final Query<TestEntity> query = session.createQuery( |
| 87 | + "SELECT e FROM TestEntity e WHERE e.date <= :ts", |
| 88 | + TestEntity.class |
| 89 | + ).setParameter( "ts", new DateAttribute( System.currentTimeMillis() ), TemporalType.TIMESTAMP ); |
| 90 | + |
| 91 | + final Stream<TestEntity> stream = query.stream(); |
| 92 | + |
| 93 | + assertThat( stream.count(), is( 1L ) ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Entity(name = "TestEntity") |
| 98 | + @Table(name = "TEST_ENTITY") |
| 99 | + public static class TestEntity { |
| 100 | + |
| 101 | + @Id |
| 102 | + @GeneratedValue |
| 103 | + private long id; |
| 104 | + |
| 105 | + @Convert(converter = DateAttributeConverter.class) |
| 106 | + private DateAttribute date; |
| 107 | + |
| 108 | + public DateAttribute getDate() { |
| 109 | + return date; |
| 110 | + } |
| 111 | + |
| 112 | + public void setDate(DateAttribute date) { |
| 113 | + this.date = date; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public static class DateAttribute implements Serializable { |
| 118 | + private long field; |
| 119 | + |
| 120 | + public DateAttribute(long field) { |
| 121 | + this.field = field; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public static class DateAttributeConverter implements AttributeConverter<DateAttribute, Date> { |
| 126 | + |
| 127 | + @Override |
| 128 | + public Date convertToDatabaseColumn(DateAttribute attribute) { |
| 129 | + if ( attribute == null ) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + return new Date( attribute.field ); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public DateAttribute convertToEntityAttribute(Date dbData) { |
| 137 | + if ( dbData == null ) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + return new DateAttribute( dbData.getTime() ); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | +} |
0 commit comments