|
| 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.annotations.enumerated.mappedSuperclass; |
| 8 | + |
| 9 | +import java.io.Serializable; |
| 10 | +import java.sql.Types; |
| 11 | +import javax.persistence.Column; |
| 12 | +import javax.persistence.Enumerated; |
| 13 | +import javax.persistence.GeneratedValue; |
| 14 | +import javax.persistence.Id; |
| 15 | +import javax.persistence.MappedSuperclass; |
| 16 | +import javax.persistence.Table; |
| 17 | + |
| 18 | +import org.hibernate.annotations.GenericGenerator; |
| 19 | +import org.hibernate.boot.Metadata; |
| 20 | +import org.hibernate.boot.MetadataSources; |
| 21 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 22 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 23 | +import org.hibernate.cfg.AvailableSettings; |
| 24 | +import org.hibernate.dialect.PostgreSQL81Dialect; |
| 25 | +import org.hibernate.engine.spi.SessionFactoryImplementor; |
| 26 | +import org.hibernate.mapping.PersistentClass; |
| 27 | +import org.hibernate.mapping.Property; |
| 28 | +import org.hibernate.persister.entity.EntityPersister; |
| 29 | +import org.hibernate.type.CustomType; |
| 30 | +import org.hibernate.type.EnumType; |
| 31 | + |
| 32 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import static javax.persistence.EnumType.STRING; |
| 38 | +import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping; |
| 39 | +import static org.junit.Assert.assertEquals; |
| 40 | + |
| 41 | +/** |
| 42 | + * Originally developed to verify/diagnose HHH-10128 |
| 43 | + * |
| 44 | + * @author Steve Ebersole |
| 45 | + */ |
| 46 | +public class EnumeratedWithMappedSuperclassTest extends BaseUnitTestCase { |
| 47 | + private StandardServiceRegistry ssr; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void before() { |
| 51 | + ssr = new StandardServiceRegistryBuilder() |
| 52 | + .applySetting( AvailableSettings.DIALECT, PostgreSQL81Dialect.class ) |
| 53 | + .build(); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void after() { |
| 58 | + if ( ssr != null ) { |
| 59 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testHHH10128() { |
| 65 | + final Metadata metadata = new MetadataSources( ssr ) |
| 66 | + .addAnnotatedClass( Entity.class ) |
| 67 | + .addAnnotatedClass( DescriptionEntity.class ) |
| 68 | + .addAnnotatedClass( AddressLevel.class ) |
| 69 | + .buildMetadata(); |
| 70 | + |
| 71 | + final PersistentClass addressLevelBinding = metadata.getEntityBinding( AddressLevel.class.getName() ); |
| 72 | + |
| 73 | + final Property natureProperty = addressLevelBinding.getProperty( "nature" ); |
| 74 | + CustomType customType = assertTyping( CustomType.class, natureProperty.getType() ); |
| 75 | + EnumType enumType = assertTyping( EnumType.class, customType.getUserType() ); |
| 76 | + assertEquals( Types.VARCHAR, enumType.sqlTypes()[0] ); |
| 77 | + |
| 78 | + SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory(); |
| 79 | + try { |
| 80 | + EntityPersister p = sf.getEntityPersister( AddressLevel.class.getName() ); |
| 81 | + CustomType runtimeType = assertTyping( CustomType.class, p.getPropertyType( "nature" ) ); |
| 82 | + EnumType runtimeEnumType = assertTyping( EnumType.class, runtimeType.getUserType() ); |
| 83 | + assertEquals( Types.VARCHAR, runtimeEnumType.sqlTypes()[0] ); |
| 84 | + } |
| 85 | + finally { |
| 86 | + sf.close(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @MappedSuperclass |
| 91 | + public static abstract class Entity implements Serializable { |
| 92 | + public static final String PROPERTY_NAME_ID = "id"; |
| 93 | + @Id |
| 94 | + @GeneratedValue(generator = "uuid2") |
| 95 | + @GenericGenerator(name = "uuid2", strategy = "uuid2") |
| 96 | + @Column(columnDefinition = "varchar", unique = true, nullable = false) |
| 97 | + private String id; |
| 98 | + |
| 99 | + public String getId() { |
| 100 | + return id; |
| 101 | + } |
| 102 | + |
| 103 | + public void setId(final String id) { |
| 104 | + this.id = id; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @MappedSuperclass |
| 109 | + public static abstract class DescriptionEntity extends Entity { |
| 110 | + @Column(name = "description_lang1", nullable = false, length = 100) |
| 111 | + private String descriptionLang1; |
| 112 | + @Column(name = "description_lang2", length = 100) |
| 113 | + private String descriptionLang2; |
| 114 | + @Column(name = "description_lang3", length = 100) |
| 115 | + private String descriptionLang3; |
| 116 | + |
| 117 | + public String getDescriptionLang1() { |
| 118 | + return this.descriptionLang1; |
| 119 | + } |
| 120 | + |
| 121 | + public void setDescriptionLang1(final String descriptionLang1) { |
| 122 | + this.descriptionLang1 = descriptionLang1; |
| 123 | + } |
| 124 | + |
| 125 | + public String getDescriptionLang2() { |
| 126 | + return this.descriptionLang2; |
| 127 | + } |
| 128 | + |
| 129 | + public void setDescriptionLang2(final String descriptionLang2) { |
| 130 | + this.descriptionLang2 = descriptionLang2; |
| 131 | + } |
| 132 | + |
| 133 | + public String getDescriptionLang3() { |
| 134 | + return this.descriptionLang3; |
| 135 | + } |
| 136 | + |
| 137 | + public void setDescriptionLang3(final String descriptionLang3) { |
| 138 | + this.descriptionLang3 = descriptionLang3; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static enum Nature { |
| 143 | + LIST, EXLIST, INPUT |
| 144 | + } |
| 145 | + |
| 146 | + @javax.persistence.Entity(name = "AddressLevel") |
| 147 | + @Table(name = "address_level") |
| 148 | + public static class AddressLevel extends DescriptionEntity { |
| 149 | +// @Column(columnDefinition = "varchar", nullable = false, length = 100) |
| 150 | + @Enumerated(STRING) |
| 151 | + private Nature nature; |
| 152 | + @Column(nullable = false) |
| 153 | + private Integer rank; |
| 154 | + @Column(nullable = false) |
| 155 | + private boolean required; |
| 156 | + |
| 157 | + public AddressLevel() { // Do nothing, default constructor needed by JPA / Hibernate |
| 158 | + } |
| 159 | + |
| 160 | + public Nature getNature() { |
| 161 | + return this.nature; |
| 162 | + } |
| 163 | + |
| 164 | + public void setNature(final Nature nature) { |
| 165 | + this.nature = nature; |
| 166 | + } |
| 167 | + |
| 168 | + public Integer getRank() { |
| 169 | + return this.rank; |
| 170 | + } |
| 171 | + |
| 172 | + public void setRank(final Integer rank) { |
| 173 | + this.rank = rank; |
| 174 | + } |
| 175 | + |
| 176 | + public boolean getRequired() { |
| 177 | + return this.required; |
| 178 | + } |
| 179 | + |
| 180 | + public void isRequired(final boolean required) { |
| 181 | + this.required = required; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments