|
| 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.List; |
| 10 | +import javax.persistence.Entity; |
| 11 | +import javax.persistence.Id; |
| 12 | +import javax.persistence.JoinColumn; |
| 13 | +import javax.persistence.ManyToOne; |
| 14 | +import javax.persistence.OneToMany; |
| 15 | +import javax.persistence.Table; |
| 16 | + |
| 17 | +import org.hibernate.cfg.Configuration; |
| 18 | + |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import io.vertx.ext.unit.TestContext; |
| 24 | + |
| 25 | +public class NonNullableManyToOneTest extends BaseReactiveTest { |
| 26 | + |
| 27 | + @Override |
| 28 | + protected Configuration constructConfiguration() { |
| 29 | + Configuration configuration = super.constructConfiguration(); |
| 30 | + configuration.addAnnotatedClass( Dealer.class ); |
| 31 | + configuration.addAnnotatedClass( Artist.class ); |
| 32 | + configuration.addAnnotatedClass( Painting.class ); |
| 33 | + return configuration; |
| 34 | + } |
| 35 | + |
| 36 | + @Before |
| 37 | + public void populateDB(TestContext context) { |
| 38 | + Artist artist = new Artist( "Grand Master Painter" ); |
| 39 | + artist.id = 1L; |
| 40 | + Dealer dealer = new Dealer( "Dealer" ); |
| 41 | + dealer.id = 1L; |
| 42 | + Painting painting = new Painting( "Mona Lisa"); |
| 43 | + painting.id = 2L; |
| 44 | + artist.addPainting( painting ); |
| 45 | + dealer.addPainting( painting ); |
| 46 | + |
| 47 | + test( context, getMutinySessionFactory() |
| 48 | + .withTransaction( s -> s.persistAll( painting, artist, dealer ) ) ); |
| 49 | + } |
| 50 | + |
| 51 | + @After |
| 52 | + public void cleanDB(TestContext context) { |
| 53 | + test( context, deleteEntities( "Painting", "Artist" ) ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testNonNullableSuccess(TestContext context) { |
| 58 | + test( |
| 59 | + context, |
| 60 | + getMutinySessionFactory().withTransaction( session -> session |
| 61 | + .createQuery( "from Artist", Artist.class ) |
| 62 | + .getSingleResult().chain( a -> session.fetch( a.paintings ) ) |
| 63 | + .invoke( paintings -> { |
| 64 | + context.assertNotNull( paintings ); |
| 65 | + context.assertEquals( 1, paintings.size() ); |
| 66 | + context.assertEquals( "Mona Lisa", paintings.get( 0 ).name ); |
| 67 | + } ) ) |
| 68 | + .chain( () -> getMutinySessionFactory().withTransaction( s1 -> s1 |
| 69 | + .createQuery( "from Dealer", Dealer.class ) |
| 70 | + .getSingleResult().chain( d -> s1.fetch( d.paintings ) ) |
| 71 | + .invoke( paintings -> { |
| 72 | + context.assertNotNull( paintings ); |
| 73 | + context.assertEquals( 1, paintings.size() ); |
| 74 | + context.assertEquals( "Mona Lisa", paintings.get( 0 ).name ); |
| 75 | + } ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + @Entity(name = "Painting") |
| 82 | + @Table(name = "painting") |
| 83 | + public static class Painting { |
| 84 | + @Id |
| 85 | + Long id; |
| 86 | + String name; |
| 87 | + |
| 88 | + @JoinColumn(nullable = false) |
| 89 | + @ManyToOne(optional = true) |
| 90 | + Artist author; |
| 91 | + |
| 92 | + @JoinColumn(nullable = true) |
| 93 | + @ManyToOne(optional = false) |
| 94 | + Dealer dealer; |
| 95 | + |
| 96 | + public Painting() { |
| 97 | + } |
| 98 | + |
| 99 | + public Painting(String name) { |
| 100 | + this.name = name; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Entity(name = "Artist") |
| 105 | + @Table(name = "artist") |
| 106 | + public static class Artist { |
| 107 | + |
| 108 | + @Id |
| 109 | + Long id; |
| 110 | + String name; |
| 111 | + |
| 112 | + @OneToMany(mappedBy = "author") |
| 113 | + List<Painting> paintings = new ArrayList<>(); |
| 114 | + |
| 115 | + public Artist() { |
| 116 | + } |
| 117 | + |
| 118 | + public Artist(String name) { |
| 119 | + this.name = name; |
| 120 | + } |
| 121 | + |
| 122 | + public void addPainting(Painting painting) { |
| 123 | + this.paintings.add( painting ); |
| 124 | + painting.author = this; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + @Entity(name = "Dealer") |
| 130 | + @Table(name = "dealer") |
| 131 | + public static class Dealer { |
| 132 | + |
| 133 | + @Id |
| 134 | + Long id; |
| 135 | + String name; |
| 136 | + |
| 137 | + @OneToMany(mappedBy = "dealer") |
| 138 | + List<Painting> paintings = new ArrayList<>(); |
| 139 | + |
| 140 | + public Dealer() { |
| 141 | + } |
| 142 | + |
| 143 | + public Dealer(String name) { |
| 144 | + this.name = name; |
| 145 | + } |
| 146 | + |
| 147 | + public void addPainting(Painting painting) { |
| 148 | + this.paintings.add( painting ); |
| 149 | + painting.dealer = this; |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | +} |
0 commit comments