|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.bytecode.enhancement.access; |
| 6 | + |
| 7 | +import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import jakarta.persistence.Access; |
| 16 | +import jakarta.persistence.AccessType; |
| 17 | +import jakarta.persistence.Basic; |
| 18 | +import jakarta.persistence.DiscriminatorColumn; |
| 19 | +import jakarta.persistence.DiscriminatorValue; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.Id; |
| 22 | +import jakarta.persistence.Inheritance; |
| 23 | +import jakarta.persistence.Table; |
| 24 | +import jakarta.persistence.Transient; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | + |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + HierarchyPropertyAccessTest.ChildEntity.class, |
| 31 | + } |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +@JiraKey("HHH-19140") |
| 35 | +@BytecodeEnhanced |
| 36 | +public class HierarchyPropertyAccessTest { |
| 37 | + |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testParent(SessionFactoryScope scope) { |
| 41 | + scope.inTransaction( session -> { |
| 42 | + session.persist( new ParentEntity( 1L, "field", "transient: property" ) ); |
| 43 | + } ); |
| 44 | + |
| 45 | + scope.inTransaction( session -> { |
| 46 | + ParentEntity entity = session.get( ParentEntity.class, 1L ); |
| 47 | + assertThat( entity.persistProperty ).isEqualTo( "property" ); |
| 48 | + assertThat( entity.property ).isEqualTo( "transient: property" ); |
| 49 | + |
| 50 | + entity.setProperty( "transient: updated" ); |
| 51 | + } ); |
| 52 | + |
| 53 | + scope.inTransaction( session -> { |
| 54 | + ParentEntity entity = session.get( ParentEntity.class, 1L ); |
| 55 | + assertThat( entity.persistProperty ).isEqualTo( "updated" ); |
| 56 | + assertThat( entity.property ).isEqualTo( "transient: updated" ); |
| 57 | + } ); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testChild(SessionFactoryScope scope) { |
| 62 | + scope.inTransaction( session -> { |
| 63 | + session.persist( new ChildEntity( 2L, "field", "transient: property" ) ); |
| 64 | + } ); |
| 65 | + |
| 66 | + scope.inTransaction( session -> { |
| 67 | + ChildEntity entity = session.get( ChildEntity.class, 2L ); |
| 68 | + assertThat( entity.persistProperty ).isEqualTo( "property" ); |
| 69 | + assertThat( entity.property ).isEqualTo( "transient: property" ); |
| 70 | + |
| 71 | + entity.setProperty( "transient: updated" ); |
| 72 | + } ); |
| 73 | + |
| 74 | + scope.inTransaction( session -> { |
| 75 | + ChildEntity entity = session.get( ChildEntity.class, 2L ); |
| 76 | + assertThat( entity.persistProperty ).isEqualTo( "updated" ); |
| 77 | + assertThat( entity.property ).isEqualTo( "transient: updated" ); |
| 78 | + } ); |
| 79 | + } |
| 80 | + |
| 81 | + @AfterEach |
| 82 | + public void cleanup(SessionFactoryScope scope) { |
| 83 | + scope.inTransaction( session -> { |
| 84 | + ParentEntity parentEntity = session.get( ParentEntity.class, 1L ); |
| 85 | + if (parentEntity != null) { |
| 86 | + session.remove( parentEntity ); |
| 87 | + } |
| 88 | + ChildEntity childEntity = session.get( ChildEntity.class, 2L ); |
| 89 | + if (childEntity != null) { |
| 90 | + session.remove( childEntity ); |
| 91 | + } |
| 92 | + } ); |
| 93 | + } |
| 94 | + |
| 95 | + @Entity |
| 96 | + @Table(name = "PARENT_ENTITY") |
| 97 | + @Inheritance |
| 98 | + @DiscriminatorColumn(name = "type") |
| 99 | + @DiscriminatorValue("Parent") |
| 100 | + static class ParentEntity { |
| 101 | + @Id |
| 102 | + Long id; |
| 103 | + |
| 104 | + @Basic |
| 105 | + String field; |
| 106 | + |
| 107 | + String persistProperty; |
| 108 | + |
| 109 | + @Transient |
| 110 | + String property; |
| 111 | + |
| 112 | + public ParentEntity() { |
| 113 | + } |
| 114 | + |
| 115 | + public ParentEntity(Long id, String field, String property) { |
| 116 | + this.id = id; |
| 117 | + this.field = field; |
| 118 | + this.property = property; |
| 119 | + } |
| 120 | + |
| 121 | + @Basic |
| 122 | + @Access(AccessType.PROPERTY) |
| 123 | + public String getPersistProperty() { |
| 124 | + this.persistProperty = this.property.substring( 11 ); |
| 125 | + return this.persistProperty; |
| 126 | + } |
| 127 | + |
| 128 | + public void setPersistProperty(String persistProperty) { |
| 129 | + this.property = "transient: " + persistProperty; |
| 130 | + this.persistProperty = persistProperty; |
| 131 | + } |
| 132 | + |
| 133 | + public String getProperty() { |
| 134 | + return this.property; |
| 135 | + } |
| 136 | + |
| 137 | + public void setProperty(String property) { |
| 138 | + this.property = property; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Entity |
| 143 | + @DiscriminatorValue("Child") |
| 144 | + static class ChildEntity extends ParentEntity { |
| 145 | + |
| 146 | + public ChildEntity() { |
| 147 | + } |
| 148 | + |
| 149 | + public ChildEntity(Long id, String field, String property) { |
| 150 | + super(id, field, property); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments