|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.notfound; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.GeneratedValue; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.JoinColumn; |
| 11 | +import jakarta.persistence.ManyToOne; |
| 12 | +import org.hibernate.FetchNotFoundException; |
| 13 | +import org.hibernate.annotations.JoinColumnOrFormula; |
| 14 | +import org.hibernate.annotations.JoinFormula; |
| 15 | +import org.hibernate.annotations.NotFound; |
| 16 | +import org.hibernate.annotations.NotFoundAction; |
| 17 | +import org.hibernate.query.Query; |
| 18 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 19 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 20 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 21 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 28 | + |
| 29 | +@JiraKey(value = "HHH-18891") |
| 30 | +@DomainModel( |
| 31 | + annotatedClasses = {CompositeForeignKeyNotFoundTest.Document.class, CompositeForeignKeyNotFoundTest.DocumentIgnore.class, |
| 32 | + CompositeForeignKeyNotFoundTest.DocumentException.class, CompositeForeignKeyNotFoundTest.Person.class}) |
| 33 | +@SessionFactory |
| 34 | +public class CompositeForeignKeyNotFoundTest { |
| 35 | + |
| 36 | + @Test |
| 37 | + void hhh18891TestWithNotFoundIgnore(SessionFactoryScope scope) { |
| 38 | + |
| 39 | + // prepare document |
| 40 | + scope.inTransaction( session -> { |
| 41 | + Query nativeQuery = session.createNativeQuery( |
| 42 | + "insert into DocumentIgnore (id,owner) values (123,42)" ); |
| 43 | + nativeQuery.executeUpdate(); |
| 44 | + } ); |
| 45 | + |
| 46 | + // assert document |
| 47 | + scope.inTransaction( session -> { |
| 48 | + final DocumentIgnore document = session.find( DocumentIgnore.class, 123 ); |
| 49 | + assertNotNull( document ); |
| 50 | + assertEquals( 123, document.id ); |
| 51 | + assertNull( document.owner ); |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void hhh18891TestWithNotFoundException(SessionFactoryScope scope) { |
| 57 | + |
| 58 | + // prepare document |
| 59 | + scope.inTransaction( session -> { |
| 60 | + Query nativeQuery = session.createNativeQuery( |
| 61 | + "insert into DocumentException (id,owner) values (123,42)" ); |
| 62 | + nativeQuery.executeUpdate(); |
| 63 | + } ); |
| 64 | + |
| 65 | + // assert document |
| 66 | + scope.inTransaction( session -> { |
| 67 | + assertThrows( FetchNotFoundException.class, () -> |
| 68 | + session.find( DocumentException.class, 123 ) ); |
| 69 | + } ); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void hhh18891TestWithoutNotFoundAnnotation(SessionFactoryScope scope) { |
| 74 | + |
| 75 | + // prepare document |
| 76 | + scope.inTransaction( session -> { |
| 77 | + Query nativeQuery = session.createNativeQuery( |
| 78 | + "insert into Document (id,owner) values (123,42)" ); |
| 79 | + nativeQuery.executeUpdate(); |
| 80 | + } ); |
| 81 | + |
| 82 | + // assert document |
| 83 | + scope.inTransaction( session -> { |
| 84 | + final Document document = session.find( Document.class, 123 ); |
| 85 | + assertNotNull( document ); |
| 86 | + assertEquals( 123, document.id ); |
| 87 | + assertNull( document.owner ); |
| 88 | + } ); |
| 89 | + } |
| 90 | + |
| 91 | + @Entity(name = "DocumentIgnore") |
| 92 | + public static class DocumentIgnore { |
| 93 | + |
| 94 | + @Id |
| 95 | + @GeneratedValue |
| 96 | + Long id; |
| 97 | + |
| 98 | + @ManyToOne |
| 99 | + @NotFound(action = NotFoundAction.IGNORE) |
| 100 | + @JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false, |
| 101 | + updatable = false)) |
| 102 | + @JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name")) |
| 103 | + Person owner; |
| 104 | + } |
| 105 | + |
| 106 | + @Entity(name = "DocumentException") |
| 107 | + public static class DocumentException { |
| 108 | + |
| 109 | + @Id |
| 110 | + @GeneratedValue |
| 111 | + Long id; |
| 112 | + |
| 113 | + @ManyToOne |
| 114 | + @NotFound(action = NotFoundAction.EXCEPTION) |
| 115 | + @JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false, |
| 116 | + updatable = false)) |
| 117 | + @JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name")) |
| 118 | + Person owner; |
| 119 | + } |
| 120 | + |
| 121 | + @Entity(name = "Document") |
| 122 | + public static class Document { |
| 123 | + |
| 124 | + @Id |
| 125 | + @GeneratedValue |
| 126 | + Long id; |
| 127 | + |
| 128 | + @ManyToOne |
| 129 | + @JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false, |
| 130 | + updatable = false)) |
| 131 | + @JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name")) |
| 132 | + Person owner; |
| 133 | + } |
| 134 | + |
| 135 | + @Entity(name = "Person") |
| 136 | + public static class Person { |
| 137 | + |
| 138 | + @Id |
| 139 | + Long id; |
| 140 | + |
| 141 | + String name; |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments