|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive; |
| 7 | + |
| 8 | +import javax.persistence.Entity; |
| 9 | +import javax.persistence.Id; |
| 10 | +import javax.persistence.OneToOne; |
| 11 | + |
| 12 | +import org.hibernate.cfg.Configuration; |
| 13 | + |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import io.vertx.ext.unit.TestContext; |
| 19 | + |
| 20 | +public class OneToOneLazyOrphanRemovalTest extends BaseReactiveTest { |
| 21 | + @Override |
| 22 | + protected Configuration constructConfiguration() { |
| 23 | + Configuration configuration = super.constructConfiguration(); |
| 24 | + configuration.addAnnotatedClass( Car.class ); |
| 25 | + configuration.addAnnotatedClass( PaintColor.class ); |
| 26 | + configuration.addAnnotatedClass( Engine.class ); |
| 27 | + return configuration; |
| 28 | + } |
| 29 | + |
| 30 | + @Before |
| 31 | + public void populateDb(TestContext context) { |
| 32 | + final PaintColor color = new PaintColor( 1, "Red" ); |
| 33 | + final Engine engine = new Engine( 1, 275 ); |
| 34 | + final Car car = new Car( 1, engine, color ); |
| 35 | + test( context, getSessionFactory() |
| 36 | + .withTransaction( (session, tx) -> session.persist( color, engine, car ) ) ); |
| 37 | + } |
| 38 | + |
| 39 | + @After |
| 40 | + public void deleteAll(TestContext context) { |
| 41 | + test( context, deleteEntities( "Car", "PaintColor", "Engine" ) ); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testUnidirectionalOneToOneOrphanRemoval(TestContext context) { |
| 46 | + test( context, getMutinySessionFactory() |
| 47 | + .withTransaction( (session, tx) -> session |
| 48 | + .find( Car.class, 1 ) |
| 49 | + .invoke( car -> car.setEngine( null ) ) ) |
| 50 | + .call( () -> getMutinySessionFactory() |
| 51 | + .withSession( session -> session |
| 52 | + .find( Car.class, 1 ) |
| 53 | + .invoke( car -> context |
| 54 | + .assertNull( car.getEngine() ) ) |
| 55 | + .chain( () -> session |
| 56 | + .find( Engine.class, 1 ) |
| 57 | + .invoke( engine -> context.assertNull( engine ) ) ) ) ) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testBidirectionalOneToOneOrphanRemoval(TestContext context) { |
| 63 | + test( context, getMutinySessionFactory() |
| 64 | + .withTransaction( (session, tx) -> session |
| 65 | + .find( Car.class, 1 ) |
| 66 | + .invoke( car -> { |
| 67 | + car.getPaintColor().setCar( null ); |
| 68 | + car.setPaintColor( null ); |
| 69 | + } ) ) |
| 70 | + .call( () -> getMutinySessionFactory() |
| 71 | + .withSession( session -> session |
| 72 | + .find( Car.class, 1 ) |
| 73 | + .invoke( car -> context |
| 74 | + .assertNull( car.getPaintColor() ) ) |
| 75 | + .chain( () -> session |
| 76 | + .find( PaintColor.class, 1 ) |
| 77 | + .invoke( color -> context.assertNull( color ) ) ) ) ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Entity(name = "Car") |
| 82 | + public static class Car { |
| 83 | + @Id |
| 84 | + private Integer id; |
| 85 | + |
| 86 | + // represents a bidirectional one-to-one |
| 87 | + @OneToOne(orphanRemoval = true) |
| 88 | + private PaintColor paintColor; |
| 89 | + |
| 90 | + // represents a unidirectional one-to-one |
| 91 | + @OneToOne(orphanRemoval = true) |
| 92 | + private Engine engine; |
| 93 | + |
| 94 | + Car() { |
| 95 | + // Required by JPA |
| 96 | + } |
| 97 | + |
| 98 | + Car(Integer id, Engine engine, PaintColor paintColor) { |
| 99 | + this.id = id; |
| 100 | + this.engine = engine; |
| 101 | + this.paintColor = paintColor; |
| 102 | + paintColor.setCar( this ); |
| 103 | + } |
| 104 | + |
| 105 | + public Integer getId() { |
| 106 | + return id; |
| 107 | + } |
| 108 | + |
| 109 | + public void setId(Integer id) { |
| 110 | + this.id = id; |
| 111 | + } |
| 112 | + |
| 113 | + public PaintColor getPaintColor() { |
| 114 | + return paintColor; |
| 115 | + } |
| 116 | + |
| 117 | + public void setPaintColor(PaintColor paintColor) { |
| 118 | + this.paintColor = paintColor; |
| 119 | + } |
| 120 | + |
| 121 | + public Engine getEngine() { |
| 122 | + return engine; |
| 123 | + } |
| 124 | + |
| 125 | + public void setEngine(Engine engine) { |
| 126 | + this.engine = engine; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Entity(name = "Engine") |
| 131 | + public static class Engine { |
| 132 | + @Id |
| 133 | + private Integer id; |
| 134 | + private Integer horsePower; |
| 135 | + |
| 136 | + Engine() { |
| 137 | + // Required by JPA |
| 138 | + } |
| 139 | + |
| 140 | + Engine(Integer id, int horsePower) { |
| 141 | + this.id = id; |
| 142 | + this.horsePower = horsePower; |
| 143 | + } |
| 144 | + |
| 145 | + public Integer getId() { |
| 146 | + return id; |
| 147 | + } |
| 148 | + |
| 149 | + public void setId(Integer id) { |
| 150 | + this.id = id; |
| 151 | + } |
| 152 | + |
| 153 | + public Integer getHorsePower() { |
| 154 | + return horsePower; |
| 155 | + } |
| 156 | + |
| 157 | + public void setHorsePower(Integer horsePower) { |
| 158 | + this.horsePower = horsePower; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Entity(name = "PaintColor") |
| 163 | + public static class PaintColor { |
| 164 | + @Id |
| 165 | + private Integer id; |
| 166 | + private String color; |
| 167 | + |
| 168 | + @OneToOne(mappedBy = "paintColor") |
| 169 | + private Car car; |
| 170 | + |
| 171 | + PaintColor() { |
| 172 | + // Required by JPA |
| 173 | + } |
| 174 | + |
| 175 | + PaintColor(Integer id, String color) { |
| 176 | + this.id = id; |
| 177 | + this.color = color; |
| 178 | + } |
| 179 | + |
| 180 | + public Integer getId() { |
| 181 | + return id; |
| 182 | + } |
| 183 | + |
| 184 | + public void setId(Integer id) { |
| 185 | + this.id = id; |
| 186 | + } |
| 187 | + |
| 188 | + public String getColor() { |
| 189 | + return color; |
| 190 | + } |
| 191 | + |
| 192 | + public void setColor(String color) { |
| 193 | + this.color = color; |
| 194 | + } |
| 195 | + |
| 196 | + public Car getCar() { |
| 197 | + return car; |
| 198 | + } |
| 199 | + |
| 200 | + public void setCar(Car car) { |
| 201 | + this.car = car; |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments