|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.component; |
| 6 | + |
| 7 | + |
| 8 | +import jakarta.persistence.EnumType; |
| 9 | +import jakarta.persistence.Enumerated; |
| 10 | +import org.hibernate.annotations.JdbcTypeCode; |
| 11 | +import org.hibernate.annotations.Struct; |
| 12 | + |
| 13 | +import org.hibernate.dialect.PostgreSQLDialect; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.hibernate.type.SqlTypes; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import jakarta.persistence.Embeddable; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | + |
| 27 | +@DomainModel( |
| 28 | + annotatedClasses = { |
| 29 | + StructComponentEnumTest.Book.class, |
| 30 | + StructComponentEnumTest.Publisher.class |
| 31 | + } |
| 32 | +) |
| 33 | +@SessionFactory |
| 34 | +@RequiresDialect(PostgreSQLDialect.class) |
| 35 | +public class StructComponentEnumTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void save(SessionFactoryScope scope){ |
| 39 | + scope.inTransaction( |
| 40 | + session -> { |
| 41 | + |
| 42 | + Book book = new Book(); |
| 43 | + book.title = "Hibernate"; |
| 44 | + book.author = "Steve"; |
| 45 | + book.publisher = new Publisher( "penguin", PublisherType.TRADITIONAL ); |
| 46 | + |
| 47 | + session.persist( book ); |
| 48 | + } |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + public void tearDown(SessionFactoryScope scope){ |
| 54 | + scope.getSessionFactory().getSchemaManager().truncate(); |
| 55 | + } |
| 56 | + |
| 57 | + @Entity(name = "Book") |
| 58 | + public static class Book { |
| 59 | + |
| 60 | + @Id |
| 61 | + @GeneratedValue |
| 62 | + private Long id; |
| 63 | + |
| 64 | + private String title; |
| 65 | + |
| 66 | + private String author; |
| 67 | + |
| 68 | + private Publisher publisher; |
| 69 | + } |
| 70 | + |
| 71 | + @Embeddable |
| 72 | + @Struct( name = "publisher_type") |
| 73 | + public static class Publisher { |
| 74 | + |
| 75 | + private String name; |
| 76 | + |
| 77 | + @Enumerated(EnumType.STRING) |
| 78 | + @JdbcTypeCode(SqlTypes.NAMED_ENUM) |
| 79 | + private PublisherType type; |
| 80 | + |
| 81 | + public Publisher(String name, PublisherType type) { |
| 82 | + this.name = name; |
| 83 | + this.type = type; |
| 84 | + } |
| 85 | + |
| 86 | + public Publisher() { |
| 87 | + } |
| 88 | + |
| 89 | + public String getName() { |
| 90 | + return name; |
| 91 | + } |
| 92 | + |
| 93 | + public void setName(String name) { |
| 94 | + this.name = name; |
| 95 | + } |
| 96 | + |
| 97 | + public PublisherType getType() { |
| 98 | + return type; |
| 99 | + } |
| 100 | + |
| 101 | + public void setType(PublisherType type) { |
| 102 | + this.type = type; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public enum PublisherType { |
| 107 | + TRADITIONAL, SELF_PUBLISHED, PRINT_ON_DEMAND |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | +} |
0 commit comments