|
| 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.flush; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 13 | +import org.hibernate.testing.orm.junit.FailureExpected; |
| 14 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 15 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import jakarta.persistence.CascadeType; |
| 22 | +import jakarta.persistence.Entity; |
| 23 | +import jakarta.persistence.GeneratedValue; |
| 24 | +import jakarta.persistence.Id; |
| 25 | +import jakarta.persistence.ManyToOne; |
| 26 | +import jakarta.persistence.OneToMany; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Jan Schatteman |
| 32 | + */ |
| 33 | +@DomainModel( |
| 34 | + annotatedClasses = { HHH10445Test.Person.class, HHH10445Test.Phone.class } |
| 35 | +) |
| 36 | +@SessionFactory |
| 37 | +@JiraKey( value = "HHH-10445") |
| 38 | +public class HHH10445Test { |
| 39 | + |
| 40 | + @AfterAll |
| 41 | + public void tearDown(SessionFactoryScope scope) { |
| 42 | + scope.inTransaction( |
| 43 | + session -> session.createMutationQuery( "delete from Person" ).executeUpdate() |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @FailureExpected(jiraKey = "HHH-10445") |
| 49 | + public void doTest(SessionFactoryScope scope) { |
| 50 | + Person thePerson = scope.fromTransaction( |
| 51 | + session -> { |
| 52 | + Person p = new Person("John Doe"); |
| 53 | + session.persist( p ); |
| 54 | + return p; |
| 55 | + } |
| 56 | + ); |
| 57 | + |
| 58 | + scope.inTransaction( |
| 59 | + session -> { |
| 60 | + Phone phone = new Phone( "1234567890" ); |
| 61 | + thePerson.addPhone( phone ); |
| 62 | + session.persist( phone ); |
| 63 | + Person person = session.createQuery( "select p from Person p", Person.class).getSingleResult(); |
| 64 | + assertEquals(1, person.getPhones().size()); |
| 65 | + } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Entity(name = "Person") |
| 70 | + public static class Person { |
| 71 | + |
| 72 | + @Id |
| 73 | + @GeneratedValue |
| 74 | + private Long id; |
| 75 | + |
| 76 | + private String name; |
| 77 | + |
| 78 | + @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) |
| 79 | + private List<Phone> phones = new ArrayList<>( ); |
| 80 | + |
| 81 | + public Person() {} |
| 82 | + |
| 83 | + public Person(String name) { |
| 84 | + this.name = name; |
| 85 | + } |
| 86 | + |
| 87 | + public Long getId() { |
| 88 | + return id; |
| 89 | + } |
| 90 | + |
| 91 | + public String getName() { |
| 92 | + return name; |
| 93 | + } |
| 94 | + |
| 95 | + public List<Phone> getPhones() { |
| 96 | + return phones; |
| 97 | + } |
| 98 | + |
| 99 | + public void addPhone(Phone phone) { |
| 100 | + phones.add( phone ); |
| 101 | + phone.setPerson( this ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Entity(name = "Phone") |
| 106 | + public static class Phone { |
| 107 | + |
| 108 | + @Id |
| 109 | + @GeneratedValue |
| 110 | + private Long id; |
| 111 | + |
| 112 | + @ManyToOne |
| 113 | + private Person person; |
| 114 | + |
| 115 | + private String number; |
| 116 | + |
| 117 | + public Phone() { |
| 118 | + } |
| 119 | + |
| 120 | + public Phone(String number) { |
| 121 | + this.number = number; |
| 122 | + } |
| 123 | + |
| 124 | + public Long getId() { |
| 125 | + return id; |
| 126 | + } |
| 127 | + |
| 128 | + public String getNumber() { |
| 129 | + return number; |
| 130 | + } |
| 131 | + |
| 132 | + public Person getPerson() { |
| 133 | + return person; |
| 134 | + } |
| 135 | + |
| 136 | + public void setPerson(Person person) { |
| 137 | + this.person = person; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments