|
| 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 | + |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import io.vertx.junit5.VertxTestContext; |
| 16 | +import jakarta.persistence.Column; |
| 17 | +import jakarta.persistence.Embeddable; |
| 18 | +import jakarta.persistence.EmbeddedId; |
| 19 | +import jakarta.persistence.Entity; |
| 20 | +import jakarta.persistence.FetchType; |
| 21 | +import jakarta.persistence.JoinColumn; |
| 22 | +import jakarta.persistence.ManyToOne; |
| 23 | +import jakarta.persistence.MappedSuperclass; |
| 24 | +import jakarta.persistence.OneToMany; |
| 25 | +import jakarta.persistence.Table; |
| 26 | + |
| 27 | +public class EmbeddedIdWithManyTest extends BaseReactiveTest { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Collection<Class<?>> annotatedEntities() { |
| 31 | + return List.of( Flower.class, Fruit.class ); |
| 32 | + } |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void populateDb(VertxTestContext context) { |
| 36 | + Seed seed1 = new Seed( 1 ); |
| 37 | + Flower rose = new Flower( seed1, "Rose" ); |
| 38 | + Fruit cherry = new Fruit( seed1, "Cherry" ); |
| 39 | + cherry.addFriend( rose ); |
| 40 | + |
| 41 | + Seed seed2 = new Seed( 2 ); |
| 42 | + Flower sunflower = new Flower( seed2, "Sunflower" ); |
| 43 | + Fruit apple = new Fruit( seed2, "Apple" ); |
| 44 | + apple.addFriend( sunflower ); |
| 45 | + |
| 46 | + test( |
| 47 | + context, |
| 48 | + getMutinySessionFactory().withTransaction( s -> s |
| 49 | + .createNativeQuery( "INSERT INTO known_fruits(id, name) VALUES (1, 'Cherry')" ) |
| 50 | + .executeUpdate() |
| 51 | + .call( () -> s |
| 52 | + .createNativeQuery( "INSERT INTO known_fruits(id, name) VALUES (2, 'Apple');" ) |
| 53 | + .executeUpdate() ) |
| 54 | + .call( () -> s |
| 55 | + .createNativeQuery( "INSERT INTO known_fruits(id, name) VALUES (3, 'Banana')" ) |
| 56 | + .executeUpdate() ) |
| 57 | + .call( () -> s |
| 58 | + .createNativeQuery( "INSERT INTO known_flowers(id, name, friend) VALUES(1, 'Rose', 1)" ) |
| 59 | + .executeUpdate() ) |
| 60 | + .call( () -> s |
| 61 | + .createNativeQuery( "INSERT INTO known_flowers(id, name, friend) VALUES(2, 'Sunflower', 2)" ) |
| 62 | + .executeUpdate() ) |
| 63 | + .call( () -> s |
| 64 | + .createNativeQuery( "INSERT INTO known_flowers(id, name, friend) VALUES(3, 'Chrysanthemum', 2)" ) |
| 65 | + .executeUpdate() ) |
| 66 | + ) ); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void test(VertxTestContext context) { |
| 71 | + test( |
| 72 | + context, getMutinySessionFactory().withTransaction( s -> s |
| 73 | + .createSelectionQuery( "from Flower", Flower.class ) |
| 74 | + .getResultList() |
| 75 | + ) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + @Embeddable |
| 80 | + public static class Seed { |
| 81 | + |
| 82 | + @Column(nullable = false, updatable = false) |
| 83 | + private Integer id; |
| 84 | + |
| 85 | + public Seed() { |
| 86 | + } |
| 87 | + |
| 88 | + public Seed(Integer id) { |
| 89 | + this.id = id; |
| 90 | + } |
| 91 | + |
| 92 | + public Integer getId() { |
| 93 | + return id; |
| 94 | + } |
| 95 | + |
| 96 | + public void setId(Integer id) { |
| 97 | + this.id = id; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @MappedSuperclass |
| 102 | + public static abstract class Plant { |
| 103 | + |
| 104 | + @EmbeddedId |
| 105 | + private Seed seed; |
| 106 | + |
| 107 | + @Column(length = 40, unique = true) |
| 108 | + private String name; |
| 109 | + |
| 110 | + protected Plant() { |
| 111 | + } |
| 112 | + |
| 113 | + protected Plant(Seed seed, String name) { |
| 114 | + this.seed = seed; |
| 115 | + this.name = name; |
| 116 | + } |
| 117 | + |
| 118 | + public Seed getSeed() { |
| 119 | + return seed; |
| 120 | + } |
| 121 | + |
| 122 | + public void setSeed(Seed seed) { |
| 123 | + this.seed = seed; |
| 124 | + } |
| 125 | + |
| 126 | + public String getName() { |
| 127 | + return name; |
| 128 | + } |
| 129 | + |
| 130 | + public void setName(String name) { |
| 131 | + this.name = name; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Entity(name = "Fruit") |
| 136 | + @Table(name = "known_fruits") |
| 137 | + public static class Fruit extends Plant { |
| 138 | + |
| 139 | + @OneToMany(mappedBy = "friend", fetch = FetchType.LAZY) |
| 140 | + private List<Flower> friends = new ArrayList<>(); |
| 141 | + |
| 142 | + public Fruit() { |
| 143 | + } |
| 144 | + |
| 145 | + public Fruit(Seed seed, String name) { |
| 146 | + super( seed, name ); |
| 147 | + } |
| 148 | + |
| 149 | + public void addFriend(Flower flower) { |
| 150 | + this.friends.add( flower ); |
| 151 | + flower.friend = this; |
| 152 | + } |
| 153 | + |
| 154 | + public List<Flower> getFriends() { |
| 155 | + return friends; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String toString() { |
| 160 | + return "Fruit{" + getSeed().getId() + "," + getName() + '}'; |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + @Entity(name = "Flower") |
| 166 | + @Table(name = "known_flowers") |
| 167 | + public static class Flower extends Plant { |
| 168 | + |
| 169 | + @ManyToOne(fetch = FetchType.LAZY, optional = false) |
| 170 | + @JoinColumn(name = "friend", referencedColumnName = "id", insertable = false, updatable = false, nullable = false) |
| 171 | + private Fruit friend; |
| 172 | + |
| 173 | + public Flower() { |
| 174 | + } |
| 175 | + |
| 176 | + public Flower(Seed seed, String name) { |
| 177 | + super( seed, name ); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public String toString() { |
| 182 | + return "Flower{" + getSeed().getId() + "," + getName() + '}'; |
| 183 | + } |
| 184 | + |
| 185 | + } |
| 186 | + |
| 187 | +} |
0 commit comments