|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.hql; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Inheritance; |
| 10 | +import jakarta.persistence.InheritanceType; |
| 11 | +import jakarta.persistence.Table; |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.Jira; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +@DomainModel( |
| 22 | + annotatedClasses = { |
| 23 | + JoinInheritanceInsertTest.Book.class, |
| 24 | + JoinInheritanceInsertTest.SpellBook.class |
| 25 | + } |
| 26 | +) |
| 27 | +@SessionFactory |
| 28 | +@Jira( "HHH-19669" ) |
| 29 | +public class JoinInheritanceInsertTest { |
| 30 | + |
| 31 | + @AfterEach |
| 32 | + public void tearDown(SessionFactoryScope scope) { |
| 33 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testInsertNoRootFieldsExceptForTheId(SessionFactoryScope scope) { |
| 38 | + Integer id = 1; |
| 39 | + scope.inTransaction( session -> { |
| 40 | + session.createMutationQuery( "insert into SpellBook (id, forbidden) values (:id, :forbidden)" ) |
| 41 | + .setParameter( "id", id ) |
| 42 | + .setParameter( "forbidden", true ) |
| 43 | + .executeUpdate(); |
| 44 | + } ); |
| 45 | + |
| 46 | + scope.inTransaction( session -> { |
| 47 | + SpellBook spellBook = session.find( SpellBook.class, id ); |
| 48 | + assertThat( spellBook ).isNotNull(); |
| 49 | + assertThat( spellBook.getForbidden() ).isTrue(); |
| 50 | + assertThat( spellBook.getTitle() ).isNull(); |
| 51 | + } ); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testInsert2(SessionFactoryScope scope) { |
| 56 | + Integer id = 1; |
| 57 | + String title = "Spell Book: A Comprehensive Guide to Magic Spells and Incantations"; |
| 58 | + |
| 59 | + scope.inTransaction( session -> { |
| 60 | + session.createMutationQuery( "insert into SpellBook (id, title, forbidden) values (:id, :title, :forbidden)" ) |
| 61 | + .setParameter( "id", id ) |
| 62 | + .setParameter( "title", title ) |
| 63 | + .setParameter( "forbidden", true ) |
| 64 | + .executeUpdate(); |
| 65 | + } ); |
| 66 | + |
| 67 | + scope.inTransaction( session -> { |
| 68 | + SpellBook spellBook = session.find( SpellBook.class, id ); |
| 69 | + assertThat( spellBook ).isNotNull(); |
| 70 | + assertThat( spellBook.getTitle() ).isEqualTo( title ); |
| 71 | + assertThat( spellBook.getForbidden() ).isTrue(); |
| 72 | + } ); |
| 73 | + } |
| 74 | + |
| 75 | + @Entity(name = "Book") |
| 76 | + @Table(name = "BookJS") |
| 77 | + @Inheritance(strategy = InheritanceType.JOINED) |
| 78 | + public static class Book { |
| 79 | + |
| 80 | + @Id |
| 81 | + private Integer id; |
| 82 | + |
| 83 | + private String title; |
| 84 | + |
| 85 | + public Book() { |
| 86 | + } |
| 87 | + |
| 88 | + public Book(Integer id, String title) { |
| 89 | + this.id = id; |
| 90 | + this.title = title; |
| 91 | + } |
| 92 | + |
| 93 | + public Integer getId() { |
| 94 | + return id; |
| 95 | + } |
| 96 | + |
| 97 | + public String getTitle() { |
| 98 | + return title; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Entity(name = "SpellBook") |
| 103 | + @Table(name = "SpellBookJS") |
| 104 | + public static class SpellBook extends Book { |
| 105 | + |
| 106 | + private boolean forbidden; |
| 107 | + |
| 108 | + SpellBook() { |
| 109 | + } |
| 110 | + |
| 111 | + public SpellBook(Integer id, String title, boolean forbidden) { |
| 112 | + super( id, title ); |
| 113 | + this.forbidden = forbidden; |
| 114 | + } |
| 115 | + |
| 116 | + public boolean getForbidden() { |
| 117 | + return forbidden; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments