|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.flush; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.FetchType; |
| 9 | +import jakarta.persistence.GeneratedValue; |
| 10 | +import jakarta.persistence.Id; |
| 11 | +import jakarta.persistence.ManyToOne; |
| 12 | +import jakarta.persistence.OneToMany; |
| 13 | +import jakarta.persistence.Table; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.Jira; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.BeforeAll; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +@SessionFactory |
| 28 | +@DomainModel( annotatedClasses = { |
| 29 | + AutoFlushRemoveTest.Item.class, |
| 30 | + AutoFlushRemoveTest.Comment.class, |
| 31 | +} ) |
| 32 | +@Jira( "https://hibernate.atlassian.net/browse/HHH-3354" ) |
| 33 | +public class AutoFlushRemoveTest { |
| 34 | + @BeforeAll |
| 35 | + public void setUp(SessionFactoryScope scope) { |
| 36 | + scope.inTransaction( session -> { |
| 37 | + final Item first = new Item( 1L, "item" ); |
| 38 | + session.persist( first ); |
| 39 | + } ); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterAll |
| 43 | + public void tearDown(SessionFactoryScope scope) { |
| 44 | + scope.inTransaction( session -> { |
| 45 | + session.createMutationQuery( "delete from Comment" ).executeUpdate(); |
| 46 | + session.createMutationQuery( "delete from Item" ).executeUpdate(); |
| 47 | + } ); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void test(SessionFactoryScope scope) { |
| 52 | + scope.inTransaction( session -> { |
| 53 | + final Item item = session.find( Item.class, 1L ); |
| 54 | + |
| 55 | + final Comment comment1 = new Comment( "c1", item ); |
| 56 | + session.persist( comment1 ); |
| 57 | + final Comment comment2 = new Comment( "c2", item ); |
| 58 | + session.persist( comment2 ); |
| 59 | + item.getComments().add( comment1 ); |
| 60 | + item.getComments().add( comment2 ); |
| 61 | + |
| 62 | + session.flush(); |
| 63 | + |
| 64 | + session.remove( comment1 ); |
| 65 | + comment1.item = null; |
| 66 | + } ); |
| 67 | + scope.inTransaction( session -> { |
| 68 | + final Item item = session.find( Item.class, 1L ); |
| 69 | + assertThat( item.getComments().size() ).isEqualTo( 1 ); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Entity( name = "Item" ) |
| 74 | + public static class Item { |
| 75 | + @Id |
| 76 | + private Long id; |
| 77 | + private String name; |
| 78 | + @OneToMany(mappedBy = "item") |
| 79 | + private Set<Comment> comments = new HashSet<>(); |
| 80 | + |
| 81 | + public Item() { |
| 82 | + } |
| 83 | + |
| 84 | + public Item(Long id, String name) { |
| 85 | + this.id = id; |
| 86 | + this.name = name; |
| 87 | + } |
| 88 | + |
| 89 | + public String getName() { |
| 90 | + return name; |
| 91 | + } |
| 92 | + |
| 93 | + public Set<Comment> getComments() { |
| 94 | + return comments; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Entity( name = "Comment" ) |
| 99 | + @Table(name = "item_comment") |
| 100 | + public static class Comment { |
| 101 | + @Id |
| 102 | + @GeneratedValue |
| 103 | + private Long id; |
| 104 | + private String text; |
| 105 | + @ManyToOne(fetch = FetchType.LAZY) |
| 106 | + private Item item; |
| 107 | + |
| 108 | + public Comment() { |
| 109 | + } |
| 110 | + |
| 111 | + public Comment(String text, Item item) { |
| 112 | + this.text = text; |
| 113 | + this.item = item; |
| 114 | + } |
| 115 | + |
| 116 | + public String getText() { |
| 117 | + return text; |
| 118 | + } |
| 119 | + |
| 120 | + public Item getItem() { |
| 121 | + return item; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments