|
| 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.jpa.convert; |
| 8 | + |
| 9 | +import java.util.UUID; |
| 10 | +import javax.persistence.AttributeConverter; |
| 11 | +import javax.persistence.Convert; |
| 12 | +import javax.persistence.Entity; |
| 13 | +import javax.persistence.GeneratedValue; |
| 14 | +import javax.persistence.Id; |
| 15 | + |
| 16 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 17 | + |
| 18 | +import org.hibernate.testing.TestForIssue; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Christian Beikov |
| 26 | + */ |
| 27 | +public class UUIDConverterTest extends BaseEntityManagerFunctionalTestCase { |
| 28 | + |
| 29 | + private UUID uuid = UUID.randomUUID(); |
| 30 | + |
| 31 | + @Override |
| 32 | + protected Class<?>[] getAnnotatedClasses() { |
| 33 | + return new Class<?>[] { TestEntity.class }; |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + @TestForIssue(jiraKey = "HHH-15097") |
| 38 | + public void testSqlTypeDescriptorForConverted() { |
| 39 | + // persist the record. |
| 40 | + Integer rowId = doInJPA( this::entityManagerFactory, entityManager -> { |
| 41 | + TestEntity e = new TestEntity(); |
| 42 | + e.setSomeValue( new SomeValue( uuid = UUID.randomUUID() ) ); |
| 43 | + entityManager.persist( e ); |
| 44 | + return e.getId(); |
| 45 | + } ); |
| 46 | + |
| 47 | + // retrieve the record and verify values. |
| 48 | + doInJPA( this::entityManagerFactory, entityManager -> { |
| 49 | + final TestEntity e = entityManager.find( TestEntity.class, rowId ); |
| 50 | + assertEquals( uuid, e.getSomeValue().uuid ); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @Entity(name = "TestEntity") |
| 55 | + public static class TestEntity { |
| 56 | + @Id |
| 57 | + @GeneratedValue |
| 58 | + private Integer id; |
| 59 | + |
| 60 | + @Convert(converter = UUIDConverter.class) |
| 61 | + private SomeValue someValue; |
| 62 | + |
| 63 | + public Integer getId() { |
| 64 | + return id; |
| 65 | + } |
| 66 | + |
| 67 | + public void setId(Integer id) { |
| 68 | + this.id = id; |
| 69 | + } |
| 70 | + |
| 71 | + public SomeValue getSomeValue() { |
| 72 | + return someValue; |
| 73 | + } |
| 74 | + |
| 75 | + public void setSomeValue(SomeValue someValue) { |
| 76 | + this.someValue = someValue; |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + public static class UUIDConverter implements AttributeConverter<SomeValue, UUID> { |
| 82 | + @Override |
| 83 | + public UUID convertToDatabaseColumn(SomeValue attribute) { |
| 84 | + return attribute == null ? null : attribute.uuid; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public SomeValue convertToEntityAttribute(UUID dbData) { |
| 89 | + return dbData == null ? null : new SomeValue( dbData ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static class SomeValue { |
| 94 | + private final UUID uuid; |
| 95 | + |
| 96 | + public SomeValue(UUID uuid) { |
| 97 | + this.uuid = uuid; |
| 98 | + } |
| 99 | + |
| 100 | + public UUID getUuid() { |
| 101 | + return uuid; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments