Skip to content

Commit 29ddb99

Browse files
committed
Added test to verify new support for non-nullable associations
1 parent e63b060 commit 29ddb99

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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.FetchType;
12+
import javax.persistence.Id;
13+
import javax.persistence.JoinColumn;
14+
import javax.persistence.ManyToOne;
15+
import javax.persistence.OneToMany;
16+
import javax.persistence.Table;
17+
18+
import org.hibernate.cfg.Configuration;
19+
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import io.vertx.ext.unit.TestContext;
25+
26+
public class NonNullableManyToOneTest extends BaseReactiveTest {
27+
28+
@Override
29+
protected Configuration constructConfiguration() {
30+
Configuration configuration = super.constructConfiguration();
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 artemisia = new Artist( "Grand Master Painter" );
39+
artemisia.id = 1L;
40+
Painting painting = new Painting( "Mona Lisa");
41+
painting.id = 2L;
42+
artemisia.addPainting( painting );
43+
44+
test( context, getMutinySessionFactory()
45+
.withTransaction( s -> s.persistAll( painting, artemisia ) ) );
46+
}
47+
48+
@After
49+
public void cleanDB(TestContext context) {
50+
test( context, deleteEntities( "Painting", "Artist" ) );
51+
}
52+
53+
@Test
54+
public void testNonNullableSuccess(TestContext context) {
55+
test( context,
56+
openMutinySession().chain( session -> session
57+
.createQuery( "from Artist", Artist.class )
58+
.getSingleResult() )
59+
// We are checking `.getPaintings()` to verify success in persistence
60+
.onItem().invoke( Artist::getPaintings )
61+
);
62+
}
63+
64+
@Entity(name = "Painting")
65+
@Table(name = "painting")
66+
public static class Painting {
67+
@Id
68+
public Long id;
69+
String name;
70+
71+
@JoinColumn(name = "author_id", nullable = false)
72+
@ManyToOne(fetch = FetchType.LAZY, targetEntity = Artist.class) //, optional = false)
73+
Artist author;
74+
75+
public Painting() {
76+
}
77+
78+
public Painting(String name) {
79+
this.name = name;
80+
}
81+
82+
public Artist getAuthor() {
83+
return author;
84+
}
85+
86+
public void setAuthor(Artist author) {
87+
this.author = author;
88+
}
89+
}
90+
91+
@Entity(name = "Artist")
92+
@Table(name = "artist")
93+
public static class Artist {
94+
95+
@Id
96+
public Long id;
97+
private String name;
98+
99+
@OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
100+
private List<Painting> paintings = new ArrayList<>();
101+
102+
public Artist() {
103+
}
104+
105+
public Artist(String name) {
106+
this.name = name;
107+
}
108+
109+
public List<Painting> getPaintings() {
110+
return paintings;
111+
}
112+
113+
public void setPaintings(List<Painting> paintings) {
114+
this.paintings = paintings;
115+
}
116+
117+
public void addPainting(Painting painting) {
118+
this.getPaintings().add( painting );
119+
painting.setAuthor( this );
120+
}
121+
122+
}
123+
}

0 commit comments

Comments
 (0)