|
| 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.bytecode.enhancement.lazy.proxy.inlinedirtychecking; |
| 8 | + |
| 9 | +import java.util.Objects; |
| 10 | +import javax.persistence.AttributeConverter; |
| 11 | +import javax.persistence.Column; |
| 12 | +import javax.persistence.Convert; |
| 13 | +import javax.persistence.Converter; |
| 14 | +import javax.persistence.Entity; |
| 15 | +import javax.persistence.GeneratedValue; |
| 16 | +import javax.persistence.GenerationType; |
| 17 | +import javax.persistence.Id; |
| 18 | +import javax.validation.constraints.NotNull; |
| 19 | + |
| 20 | +import org.hibernate.boot.MetadataSources; |
| 21 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 22 | +import org.hibernate.cfg.AvailableSettings; |
| 23 | + |
| 24 | +import org.hibernate.testing.FailureExpected; |
| 25 | +import org.hibernate.testing.TestForIssue; |
| 26 | +import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner; |
| 27 | +import org.hibernate.testing.bytecode.enhancement.CustomEnhancementContext; |
| 28 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | + |
| 33 | +import static org.hamcrest.CoreMatchers.is; |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Andrea Boriero |
| 38 | + */ |
| 39 | +@RunWith(BytecodeEnhancerRunner.class) |
| 40 | +@CustomEnhancementContext({ DirtyCheckEnhancementContext.class }) |
| 41 | +@TestForIssue( jiraKey = "HHH-13766") |
| 42 | +public class AttributeConverterTest extends BaseNonConfigCoreFunctionalTestCase { |
| 43 | + @Override |
| 44 | + protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { |
| 45 | + super.configureStandardServiceRegistryBuilder( ssrb ); |
| 46 | + ssrb.applySetting( AvailableSettings.ALLOW_ENHANCEMENT_AS_PROXY, "true" ); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void applyMetadataSources(MetadataSources sources) { |
| 51 | + super.applyMetadataSources( sources ); |
| 52 | + sources.addAnnotatedClass( TestEntity.class ); |
| 53 | + } |
| 54 | + |
| 55 | + private Long testEntityId; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() { |
| 59 | + TestEntity entity = new TestEntity(); |
| 60 | + inTransaction( |
| 61 | + session -> { |
| 62 | + TestData testData = new TestData(); |
| 63 | + testData.setValue( "initial" ); |
| 64 | + entity.setData( testData ); |
| 65 | + session.save( entity ); |
| 66 | + } |
| 67 | + ); |
| 68 | + testEntityId = entity.getId(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testUpdate() { |
| 73 | + inTransaction( |
| 74 | + session -> { |
| 75 | + TestEntity entity = session.find( TestEntity.class, testEntityId ); |
| 76 | + entity.getData().setValue( "new" ); |
| 77 | + |
| 78 | + } |
| 79 | + ); |
| 80 | + |
| 81 | + inTransaction( |
| 82 | + session -> { |
| 83 | + TestEntity entity = session.find( TestEntity.class, testEntityId ); |
| 84 | + assertThat( entity.getData().getValue(), is( "new" ) ); |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUpdate2() { |
| 92 | + inTransaction( |
| 93 | + session -> { |
| 94 | + TestEntity entity = session.find( TestEntity.class, testEntityId ); |
| 95 | + TestData testData = new TestData(); |
| 96 | + testData.setValue( "new" ); |
| 97 | + entity.setData( testData ); |
| 98 | + } |
| 99 | + ); |
| 100 | + |
| 101 | + inTransaction( |
| 102 | + session -> { |
| 103 | + TestEntity entity = session.find( TestEntity.class, testEntityId ); |
| 104 | + assertThat( entity.getData().getValue(), is( "new" ) ); |
| 105 | + } |
| 106 | + ); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @Entity(name = "TestEntity") |
| 111 | + public static class TestEntity { |
| 112 | + @Id |
| 113 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 114 | + @Column(name = "id") |
| 115 | + private Long id; |
| 116 | + |
| 117 | + @NotNull |
| 118 | + @Convert(converter = TestConverter.class) |
| 119 | + @Column(name = "data") |
| 120 | + private TestData data; |
| 121 | + |
| 122 | + public Long getId() { |
| 123 | + return id; |
| 124 | + } |
| 125 | + |
| 126 | + public void setId(Long id) { |
| 127 | + this.id = id; |
| 128 | + } |
| 129 | + |
| 130 | + public TestData getData() { |
| 131 | + return data; |
| 132 | + } |
| 133 | + |
| 134 | + public void setData(TestData data) { |
| 135 | + this.data = data; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Converter |
| 140 | + public static class TestConverter implements AttributeConverter<TestData, String> { |
| 141 | + |
| 142 | + @Override |
| 143 | + public String convertToDatabaseColumn(TestData attribute) { |
| 144 | + return attribute.getValue(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public TestData convertToEntityAttribute(String dbData) { |
| 149 | + TestData testData = new TestData(); |
| 150 | + testData.setValue( dbData ); |
| 151 | + return testData; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + public static class TestData { |
| 156 | + private String value; |
| 157 | + |
| 158 | + public String getValue() { |
| 159 | + return value; |
| 160 | + } |
| 161 | + |
| 162 | + public void setValue(String value) { |
| 163 | + this.value = value; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean equals(Object o) { |
| 168 | + if ( this == o ) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + if ( o == null || getClass() != o.getClass() ) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + TestData testData = (TestData) o; |
| 175 | + return Objects.equals( value, testData.value ); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public int hashCode() { |
| 180 | + return Objects.hash( value ); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments