|
| 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.orm.test.mapping.basic; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 12 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 15 | +import org.junit.jupiter.api.AfterAll; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import jakarta.persistence.Column; |
| 20 | +import jakarta.persistence.Entity; |
| 21 | +import jakarta.persistence.EnumType; |
| 22 | +import jakarta.persistence.Enumerated; |
| 23 | +import jakarta.persistence.GeneratedValue; |
| 24 | +import jakarta.persistence.Id; |
| 25 | + |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | + |
| 28 | +@SessionFactory |
| 29 | +@DomainModel(annotatedClasses = EnumSingleCharTest.Person.class) |
| 30 | +@JiraKey("HHH-17106") |
| 31 | +public class EnumSingleCharTest { |
| 32 | + @BeforeAll |
| 33 | + public void setUp(SessionFactoryScope scope) { |
| 34 | + scope.inTransaction( session -> { |
| 35 | + session.persist( new Person( 1L, SinglecharEnum.B ) ); |
| 36 | + session.persist( new Person( 2L, SinglecharEnum.R ) ); |
| 37 | + } ); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterAll |
| 41 | + public void tearDown(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( session -> session.createMutationQuery( "delete from Person" ).executeUpdate() ); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testRead(SessionFactoryScope scope) { |
| 47 | + scope.inTransaction( session -> { |
| 48 | + List<Person> resultList = session.createQuery( "from Person order by id", Person.class ).getResultList(); |
| 49 | + assertEquals( 2, resultList.size() ); |
| 50 | + assertEquals( SinglecharEnum.B, resultList.get( 0 ).getSingleChar() ); |
| 51 | + assertEquals( SinglecharEnum.R, resultList.get( 1 ).getSingleChar() ); |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + public enum SinglecharEnum { |
| 56 | + B("first"), |
| 57 | + R("second"), |
| 58 | + O("third"), |
| 59 | + K("fourth"), |
| 60 | + E("fifth"), |
| 61 | + N("sixth"); |
| 62 | + |
| 63 | + private final String item; |
| 64 | + |
| 65 | + SinglecharEnum(String item) { |
| 66 | + this.item = item; |
| 67 | + } |
| 68 | + |
| 69 | + public String getItem() { |
| 70 | + return item; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Entity(name = "Person") |
| 75 | + public static class Person { |
| 76 | + @Id |
| 77 | + private Long id; |
| 78 | + |
| 79 | + @Column(name="SINGLE_CHAR", length = 1) |
| 80 | + @Enumerated(EnumType.STRING) |
| 81 | + private SinglecharEnum singleChar; |
| 82 | + |
| 83 | + public Person() { |
| 84 | + } |
| 85 | + |
| 86 | + public Person(Long id, SinglecharEnum singleChar) { |
| 87 | + this.id = id; |
| 88 | + this.singleChar = singleChar; |
| 89 | + } |
| 90 | + |
| 91 | + public SinglecharEnum getSingleChar() { |
| 92 | + return singleChar; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments