-
Notifications
You must be signed in to change notification settings - Fork 96
Fix issue persisting entity mapped with non-nullable association #1174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
hibernate-reactive-core/src/test/java/org/hibernate/reactive/NonNullableManyToOneTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
import javax.persistence.Table; | ||
|
||
import org.hibernate.cfg.Configuration; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import io.vertx.ext.unit.TestContext; | ||
|
||
public class NonNullableManyToOneTest extends BaseReactiveTest { | ||
|
||
@Override | ||
protected Configuration constructConfiguration() { | ||
Configuration configuration = super.constructConfiguration(); | ||
configuration.addAnnotatedClass( Dealer.class ); | ||
configuration.addAnnotatedClass( Artist.class ); | ||
configuration.addAnnotatedClass( Painting.class ); | ||
return configuration; | ||
} | ||
|
||
@Before | ||
public void populateDB(TestContext context) { | ||
Artist artist = new Artist( "Grand Master Painter" ); | ||
artist.id = 1L; | ||
Dealer dealer = new Dealer( "Dealer" ); | ||
dealer.id = 1L; | ||
Painting painting = new Painting( "Mona Lisa"); | ||
painting.id = 2L; | ||
artist.addPainting( painting ); | ||
dealer.addPainting( painting ); | ||
|
||
test( context, getMutinySessionFactory() | ||
.withTransaction( s -> s.persistAll( painting, artist, dealer ) ) ); | ||
} | ||
|
||
@After | ||
public void cleanDB(TestContext context) { | ||
test( context, deleteEntities( "Painting", "Artist" ) ); | ||
} | ||
|
||
@Test | ||
public void testNonNullableSuccess(TestContext context) { | ||
test( | ||
context, | ||
getMutinySessionFactory().withTransaction( session -> session | ||
.createQuery( "from Artist", Artist.class ) | ||
.getSingleResult().chain( a -> session.fetch( a.paintings ) ) | ||
.invoke( paintings -> { | ||
context.assertNotNull( paintings ); | ||
context.assertEquals( 1, paintings.size() ); | ||
context.assertEquals( "Mona Lisa", paintings.get( 0 ).name ); | ||
} ) ) | ||
.chain( () -> getMutinySessionFactory().withTransaction( s1 -> s1 | ||
.createQuery( "from Dealer", Dealer.class ) | ||
.getSingleResult().chain( d -> s1.fetch( d.paintings ) ) | ||
.invoke( paintings -> { | ||
context.assertNotNull( paintings ); | ||
context.assertEquals( 1, paintings.size() ); | ||
context.assertEquals( "Mona Lisa", paintings.get( 0 ).name ); | ||
} ) | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Entity(name = "Painting") | ||
@Table(name = "painting") | ||
public static class Painting { | ||
@Id | ||
Long id; | ||
String name; | ||
|
||
@JoinColumn(nullable = false) | ||
@ManyToOne(optional = true) | ||
Artist author; | ||
|
||
@JoinColumn(nullable = true) | ||
@ManyToOne(optional = false) | ||
Dealer dealer; | ||
|
||
public Painting() { | ||
} | ||
|
||
public Painting(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Entity(name = "Artist") | ||
@Table(name = "artist") | ||
public static class Artist { | ||
|
||
@Id | ||
Long id; | ||
String name; | ||
|
||
@OneToMany(mappedBy = "author") | ||
List<Painting> paintings = new ArrayList<>(); | ||
|
||
public Artist() { | ||
} | ||
|
||
public Artist(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void addPainting(Painting painting) { | ||
this.paintings.add( painting ); | ||
painting.author = this; | ||
} | ||
|
||
} | ||
|
||
@Entity(name = "Dealer") | ||
@Table(name = "dealer") | ||
public static class Dealer { | ||
|
||
@Id | ||
Long id; | ||
String name; | ||
|
||
@OneToMany(mappedBy = "dealer") | ||
List<Painting> paintings = new ArrayList<>(); | ||
|
||
public Dealer() { | ||
} | ||
|
||
public Dealer(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void addPainting(Painting painting) { | ||
this.paintings.add( painting ); | ||
painting.dealer = this; | ||
} | ||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.