|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import io.vertx.junit5.Timeout; |
| 16 | +import io.vertx.junit5.VertxTestContext; |
| 17 | +import jakarta.persistence.Entity; |
| 18 | +import jakarta.persistence.FetchType; |
| 19 | +import jakarta.persistence.Id; |
| 20 | +import jakarta.persistence.LockModeType; |
| 21 | +import jakarta.persistence.ManyToOne; |
| 22 | +import jakarta.persistence.OneToMany; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +@Timeout(value = 10, timeUnit = TimeUnit.MINUTES) |
| 27 | +public class FindByIdWithLockTest extends BaseReactiveTest { |
| 28 | + private static final Long CHILD_ID = 1L; |
| 29 | + |
| 30 | + @Override |
| 31 | + protected Collection<Class<?>> annotatedEntities() { |
| 32 | + return List.of( Parent.class, Child.class ); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testFindChild(VertxTestContext context) { |
| 37 | + Parent parent = new Parent( 1L, "Lio" ); |
| 38 | + Child child = new Child( CHILD_ID, "And" ); |
| 39 | + test( |
| 40 | + context, getMutinySessionFactory() |
| 41 | + .withTransaction( session -> session.persistAll( parent, child ) ) |
| 42 | + .chain( () -> getMutinySessionFactory() |
| 43 | + .withTransaction( session -> session |
| 44 | + .find( Child.class, CHILD_ID, LockModeType.PESSIMISTIC_WRITE ) |
| 45 | + .invoke( c -> { |
| 46 | + assertThat( c ).isNotNull(); |
| 47 | + assertThat( c.getId() ).isEqualTo( CHILD_ID ); |
| 48 | + } |
| 49 | + ) ) ) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + @Entity(name = "Parent") |
| 54 | + public static class Parent { |
| 55 | + |
| 56 | + @Id |
| 57 | + private Long id; |
| 58 | + |
| 59 | + private String name; |
| 60 | + |
| 61 | + @OneToMany(fetch = FetchType.EAGER) |
| 62 | + public List<Child> children; |
| 63 | + |
| 64 | + public Parent() { |
| 65 | + } |
| 66 | + |
| 67 | + public Parent(Long id, String name) { |
| 68 | + this.id = id; |
| 69 | + this.name = name; |
| 70 | + } |
| 71 | + |
| 72 | + public void add(Child child) { |
| 73 | + if ( children == null ) { |
| 74 | + children = new ArrayList<>(); |
| 75 | + } |
| 76 | + children.add( child ); |
| 77 | + } |
| 78 | + |
| 79 | + public Long getId() { |
| 80 | + return id; |
| 81 | + } |
| 82 | + |
| 83 | + public String getName() { |
| 84 | + return name; |
| 85 | + } |
| 86 | + |
| 87 | + public List<Child> getChildren() { |
| 88 | + return children; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + @Entity(name = "Child") |
| 94 | + public static class Child { |
| 95 | + |
| 96 | + @Id |
| 97 | + private Long id; |
| 98 | + |
| 99 | + public String name; |
| 100 | + |
| 101 | + @ManyToOne |
| 102 | + public Parent parent; |
| 103 | + |
| 104 | + public Child() { |
| 105 | + } |
| 106 | + |
| 107 | + public Child(Long id, String name) { |
| 108 | + this.id = id; |
| 109 | + this.name = name; |
| 110 | + } |
| 111 | + |
| 112 | + public Child(Long id, String name, Parent parent) { |
| 113 | + this.id = id; |
| 114 | + this.name = name; |
| 115 | + this.parent = parent; |
| 116 | + parent.add( this ); |
| 117 | + } |
| 118 | + |
| 119 | + public Long getId() { |
| 120 | + return id; |
| 121 | + } |
| 122 | + |
| 123 | + public String getName() { |
| 124 | + return name; |
| 125 | + } |
| 126 | + |
| 127 | + public Parent getParent() { |
| 128 | + return parent; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | +} |
0 commit comments