|
| 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.Date; |
| 10 | +import javax.persistence.Column; |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.Id; |
| 13 | +import javax.persistence.ManyToOne; |
| 14 | +import javax.persistence.Table; |
| 15 | +import javax.persistence.Temporal; |
| 16 | +import javax.persistence.TemporalType; |
| 17 | +import javax.validation.constraints.NotNull; |
| 18 | + |
| 19 | +import org.hibernate.boot.MetadataSources; |
| 20 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 21 | +import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor; |
| 22 | +import org.hibernate.cfg.AvailableSettings; |
| 23 | +import org.hibernate.cfg.Environment; |
| 24 | +import org.hibernate.engine.spi.PersistentAttributeInterceptable; |
| 25 | + |
| 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.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | + |
| 34 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 35 | +import static org.hamcrest.CoreMatchers.is; |
| 36 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | + |
| 38 | +@RunWith(BytecodeEnhancerRunner.class) |
| 39 | +@CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class, DirtyCheckEnhancementContext.class }) |
| 40 | +public class EntityWithMutableAttributesTest extends BaseNonConfigCoreFunctionalTestCase { |
| 41 | + |
| 42 | + boolean skipTest; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void configureStandardServiceRegistryBuilder(StandardServiceRegistryBuilder ssrb) { |
| 46 | + super.configureStandardServiceRegistryBuilder( ssrb ); |
| 47 | + ssrb.applySetting( AvailableSettings.ALLOW_ENHANCEMENT_AS_PROXY, "true" ); |
| 48 | + ssrb.applySetting( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "100" ); |
| 49 | + ssrb.applySetting( AvailableSettings.GENERATE_STATISTICS, "true" ); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void applyMetadataSources(MetadataSources sources) { |
| 54 | + String byteCodeProvider = Environment.getProperties().getProperty( AvailableSettings.BYTECODE_PROVIDER ); |
| 55 | + if ( byteCodeProvider != null && !Environment.BYTECODE_PROVIDER_NAME_BYTEBUDDY.equals( byteCodeProvider ) ) { |
| 56 | + // skip the test if the bytecode provider is Javassist |
| 57 | + skipTest = true; |
| 58 | + } |
| 59 | + else { |
| 60 | + sources.addAnnotatedClass( User.class ); |
| 61 | + sources.addAnnotatedClass( Role.class ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setUp() { |
| 67 | + inTransaction( |
| 68 | + session -> { |
| 69 | + User user = new User(); |
| 70 | + user.setId( 1 ); |
| 71 | + user.setDate( new Date() ); |
| 72 | + user.setEmail( "not null string" ); |
| 73 | + |
| 74 | + |
| 75 | + Role role = new Role(); |
| 76 | + role.setId( 2 ); |
| 77 | + role.setDate( new Date() ); |
| 78 | + role.setName( "manager" ); |
| 79 | + |
| 80 | + user.setRole( role ); |
| 81 | + |
| 82 | + session.save( role ); |
| 83 | + session.save( user ); |
| 84 | + } |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @After |
| 89 | + public void tearDown() { |
| 90 | + inTransaction( |
| 91 | + session -> { |
| 92 | + session.createQuery( "delete from User" ).executeUpdate(); |
| 93 | + session.createQuery( "delete from Role" ).executeUpdate(); |
| 94 | + } |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testLoad() { |
| 100 | + inTransaction( |
| 101 | + session -> { |
| 102 | + User user = session.load( User.class, 1 ); |
| 103 | + assertThat( |
| 104 | + user, instanceOf( PersistentAttributeInterceptable.class ) |
| 105 | + ); |
| 106 | + final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) user; |
| 107 | + assertThat( |
| 108 | + interceptable.$$_hibernate_getInterceptor(), |
| 109 | + instanceOf( EnhancementAsProxyLazinessInterceptor.class ) |
| 110 | + ); |
| 111 | + } |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testMutableAttributeIsUpdated() { |
| 117 | + inTransaction( |
| 118 | + session -> { |
| 119 | + User user = session.load( User.class, 1 ); |
| 120 | + user.getDate().setTime( 0 ); |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + inTransaction( |
| 125 | + session -> { |
| 126 | + User user = session.getReference( User.class, 1 ); |
| 127 | + assertThat( user.getDate().getTime(), is( 0L ) ); |
| 128 | + } |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + @Entity(name = "User") |
| 134 | + @Table(name = "appuser") |
| 135 | + public static class User { |
| 136 | + @Id |
| 137 | + private Integer id; |
| 138 | + |
| 139 | + @NotNull |
| 140 | + private String email; |
| 141 | + |
| 142 | + private String name; |
| 143 | + |
| 144 | + @Temporal(TemporalType.TIMESTAMP) |
| 145 | + @Column(name = "t_date") |
| 146 | + private Date date; |
| 147 | + |
| 148 | + @ManyToOne |
| 149 | + private Role role; |
| 150 | + |
| 151 | + public Integer getId() { |
| 152 | + return id; |
| 153 | + } |
| 154 | + |
| 155 | + public void setId(Integer id) { |
| 156 | + this.id = id; |
| 157 | + } |
| 158 | + |
| 159 | + public String getEmail() { |
| 160 | + return email; |
| 161 | + } |
| 162 | + |
| 163 | + public void setEmail(String email) { |
| 164 | + this.email = email; |
| 165 | + } |
| 166 | + |
| 167 | + public String getName() { |
| 168 | + return name; |
| 169 | + } |
| 170 | + |
| 171 | + public void setName(String name) { |
| 172 | + this.name = name; |
| 173 | + } |
| 174 | + |
| 175 | + public Role getRole() { |
| 176 | + return role; |
| 177 | + } |
| 178 | + |
| 179 | + public void setRole(Role role) { |
| 180 | + this.role = role; |
| 181 | + } |
| 182 | + |
| 183 | + public Date getDate() { |
| 184 | + return date; |
| 185 | + } |
| 186 | + |
| 187 | + public void setDate(Date date) { |
| 188 | + this.date = date; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Entity(name = "Role") |
| 193 | + @Table(name = "approle") |
| 194 | + public static class Role { |
| 195 | + @Id |
| 196 | + private Integer id; |
| 197 | + |
| 198 | + @NotNull |
| 199 | + private String name; |
| 200 | + |
| 201 | + @Temporal(TemporalType.TIMESTAMP) |
| 202 | + @Column(name = "t_date") |
| 203 | + private Date date; |
| 204 | + |
| 205 | + private String description; |
| 206 | + |
| 207 | + public Integer getId() { |
| 208 | + return id; |
| 209 | + } |
| 210 | + |
| 211 | + public void setId(Integer id) { |
| 212 | + this.id = id; |
| 213 | + } |
| 214 | + |
| 215 | + public String getName() { |
| 216 | + return name; |
| 217 | + } |
| 218 | + |
| 219 | + public void setName(String name) { |
| 220 | + this.name = name; |
| 221 | + } |
| 222 | + |
| 223 | + public String getDescription() { |
| 224 | + return description; |
| 225 | + } |
| 226 | + |
| 227 | + public void setDescription(String description) { |
| 228 | + this.description = description; |
| 229 | + } |
| 230 | + |
| 231 | + public Date getDate() { |
| 232 | + return date; |
| 233 | + } |
| 234 | + |
| 235 | + public void setDate(Date date) { |
| 236 | + this.date = date; |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | +} |
0 commit comments