|
| 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 java.io.Serializable; |
| 9 | +import java.time.OffsetDateTime; |
| 10 | +import java.util.UUID; |
| 11 | +import javax.persistence.CascadeType; |
| 12 | +import javax.persistence.Column; |
| 13 | +import javax.persistence.DiscriminatorColumn; |
| 14 | +import javax.persistence.DiscriminatorType; |
| 15 | +import javax.persistence.DiscriminatorValue; |
| 16 | +import javax.persistence.Entity; |
| 17 | +import javax.persistence.FetchType; |
| 18 | +import javax.persistence.GeneratedValue; |
| 19 | +import javax.persistence.Id; |
| 20 | +import javax.persistence.Inheritance; |
| 21 | +import javax.persistence.InheritanceType; |
| 22 | +import javax.persistence.JoinColumn; |
| 23 | +import javax.persistence.OneToOne; |
| 24 | + |
| 25 | +import org.hibernate.cfg.Configuration; |
| 26 | +import org.hibernate.reactive.mutiny.Mutiny; |
| 27 | + |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import io.vertx.ext.unit.TestContext; |
| 33 | +import org.assertj.core.api.Assertions; |
| 34 | + |
| 35 | +public class LazyReplaceOrphanedEntityTest extends BaseReactiveTest { |
| 36 | + |
| 37 | + private Campaign theCampaign; |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Configuration constructConfiguration() { |
| 41 | + Configuration configuration = super.constructConfiguration(); |
| 42 | + configuration.addAnnotatedClass( Campaign.class ); |
| 43 | + configuration.addAnnotatedClass( ExecutionDate.class ); |
| 44 | + configuration.addAnnotatedClass( Schedule.class ); |
| 45 | + return configuration; |
| 46 | + } |
| 47 | + |
| 48 | + @Before |
| 49 | + public void populateDb(TestContext context) { |
| 50 | + theCampaign = new Campaign(); |
| 51 | + theCampaign.setSchedule( new ExecutionDate(OffsetDateTime.now(), "ALPHA") ); |
| 52 | + |
| 53 | + Mutiny.Session session = openMutinySession(); |
| 54 | + test( context, session.persist( theCampaign ).call( session::flush ) ); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void cleanDB(TestContext context) { |
| 59 | + test( context, deleteEntities( "Campaign", "Schedule" ) ); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testUpdateScheduleChange(TestContext context) { |
| 64 | + test( |
| 65 | + context, |
| 66 | + getMutinySessionFactory().withSession( |
| 67 | + session -> session.find( Campaign.class, theCampaign.getId() ) |
| 68 | + .invoke( foundCampaign -> foundCampaign.setSchedule( new ExecutionDate( |
| 69 | + OffsetDateTime.now(), |
| 70 | + "BETA" |
| 71 | + ) ) ) |
| 72 | + .call( session::flush ) |
| 73 | + .chain( () -> openMutinySession().find( Campaign.class, theCampaign.getId() ) ) |
| 74 | + .invoke( updatedCampaign -> Assertions.assertThat( |
| 75 | + updatedCampaign.getSchedule().getCodeName() ) |
| 76 | + .isNotEqualTo( theCampaign.getSchedule().getCodeName() ) ) |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testUpdateWithMultipleScheduleChanges(TestContext context) { |
| 83 | + test( |
| 84 | + context, |
| 85 | + getMutinySessionFactory().withSession( |
| 86 | + session -> session.find( Campaign.class, theCampaign.getId() ) |
| 87 | + .invoke( foundCampaign -> foundCampaign.setSchedule( new ExecutionDate( |
| 88 | + OffsetDateTime.now(), |
| 89 | + "BETA" |
| 90 | + ) ) ) |
| 91 | + .call( session::flush ) ) |
| 92 | + .call( () -> getMutinySessionFactory().withSession( |
| 93 | + session -> session.find( Campaign.class, theCampaign.getId() ) |
| 94 | + .invoke( foundCampaign -> foundCampaign.setSchedule( new ExecutionDate( |
| 95 | + OffsetDateTime.now(), |
| 96 | + "GAMMA" |
| 97 | + ) ) ) |
| 98 | + .call( session::flush ) |
| 99 | + ) ) |
| 100 | + .chain( () -> openMutinySession().find( Campaign.class, theCampaign.getId() ) ) |
| 101 | + .invoke( updatedCampaign -> Assertions.assertThat( |
| 102 | + updatedCampaign.getSchedule().getCodeName() ) |
| 103 | + .isNotEqualTo( theCampaign.getSchedule().getCodeName() ) |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + @Entity (name="Campaign") |
| 109 | + public static class Campaign implements Serializable { |
| 110 | + |
| 111 | + @Id @GeneratedValue |
| 112 | + private Integer id; |
| 113 | + |
| 114 | + @OneToOne(mappedBy = "campaign", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) |
| 115 | + public Schedule schedule; |
| 116 | + |
| 117 | + public Campaign() { |
| 118 | + } |
| 119 | + |
| 120 | + // Getters and setters |
| 121 | + public void setSchedule(Schedule schedule) { |
| 122 | + this.schedule = schedule; |
| 123 | + if( schedule != null ) { |
| 124 | + this.schedule.setCampaign( this ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public Schedule getSchedule() { |
| 129 | + return this.schedule; |
| 130 | + } |
| 131 | + |
| 132 | + public Integer getId() { |
| 133 | + return id; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Entity (name="Schedule") |
| 138 | + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) |
| 139 | + @DiscriminatorColumn(name = "schedule_type", discriminatorType = DiscriminatorType.STRING) |
| 140 | + public static abstract class Schedule implements Serializable { |
| 141 | + @Id |
| 142 | + @Column(name = "id") |
| 143 | + private String id = UUID.randomUUID().toString(); |
| 144 | + |
| 145 | + @Column(name = "code_name") |
| 146 | + private String code_name; |
| 147 | + |
| 148 | + @OneToOne |
| 149 | + @JoinColumn(name = "campaign_id") |
| 150 | + private Campaign campaign; |
| 151 | + |
| 152 | + // Getters and setters |
| 153 | + public String getId() { |
| 154 | + return id; |
| 155 | + } |
| 156 | + |
| 157 | + public void setCampaign(Campaign campaign) { |
| 158 | + this.campaign = campaign; |
| 159 | + } |
| 160 | + |
| 161 | + public Campaign getCampaign() { |
| 162 | + return campaign; |
| 163 | + } |
| 164 | + |
| 165 | + public void setCodeName(String code_name) { |
| 166 | + this.code_name = code_name; |
| 167 | + } |
| 168 | + |
| 169 | + public String getCodeName() { |
| 170 | + return code_name; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @Entity (name="ExecutionDate") |
| 175 | + @DiscriminatorValue("EXECUTION_DATE") |
| 176 | + public static class ExecutionDate extends Schedule { |
| 177 | + |
| 178 | + @Column(name = "start_date") |
| 179 | + private OffsetDateTime start; |
| 180 | + |
| 181 | + public ExecutionDate() { |
| 182 | + } |
| 183 | + |
| 184 | + public ExecutionDate( OffsetDateTime start, String code_name ) { |
| 185 | + this.start = start; |
| 186 | + setCodeName( code_name ); |
| 187 | + } |
| 188 | + |
| 189 | + // Getters and setters |
| 190 | + |
| 191 | + public Schedule setStart(OffsetDateTime start) { |
| 192 | + this.start = start; |
| 193 | + return null; |
| 194 | + } |
| 195 | + |
| 196 | + public OffsetDateTime getStart() { |
| 197 | + return start; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | +} |
0 commit comments