-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19585 Object relationship mapping issues | java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "one" is null #10516
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
272 changes: 272 additions & 0 deletions
272
.../orm/test/annotations/collectionelement/TestCollectionWithOrderInsertAndOrderUpdates.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,272 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.orm.test.annotations.collectionelement; | ||
|
||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.JoinColumns; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import org.hibernate.cfg.BatchSettings; | ||
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced; | ||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.orm.junit.Setting; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
TestCollectionWithOrderInsertAndOrderUpdates.ChildEntity.class, | ||
TestCollectionWithOrderInsertAndOrderUpdates.ParentEntity.class, | ||
TestCollectionWithOrderInsertAndOrderUpdates.AnotherEntity.class, | ||
TestCollectionWithOrderInsertAndOrderUpdates.ThirdEntity.class, | ||
} | ||
) | ||
@SessionFactory | ||
@ServiceRegistry( | ||
settings = { | ||
@Setting(name = BatchSettings.ORDER_INSERTS, value = "true"), | ||
@Setting(name = BatchSettings.ORDER_UPDATES, value = "true"), | ||
} | ||
) | ||
@BytecodeEnhanced(runNotEnhancedAsWell = true) | ||
@JiraKey("HHH-19585") | ||
public class TestCollectionWithOrderInsertAndOrderUpdates { | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
ParentEntity parentEntity = new ParentEntity( 1L, "Parent 1", 0L ); | ||
parentEntity.addChild( new ChildEntity( 1L, "Child 1" ) ); | ||
session.persist( parentEntity ); | ||
|
||
ParentEntity parentEntity2 = new ParentEntity( 2l, "Parent 2", 0L ); | ||
parentEntity2.addChild( new ChildEntity( 2L, "Child 2" ) ); | ||
session.persist( parentEntity2 ); | ||
|
||
ParentEntity parentEntity3 = new ParentEntity( 3l, "Parent 3", 0L ); | ||
session.persist( parentEntity3 ); | ||
|
||
AnotherEntity anotherEntity = new AnotherEntity( 1L, "1", 1L, parentEntity ); | ||
|
||
AnotherEntity anotherEntity2 = new AnotherEntity( 2L, "2", 2L, parentEntity2 ); | ||
AnotherEntity anotherEntity3 = new AnotherEntity( 3L, "3", 3L, parentEntity3 ); | ||
|
||
ThirdEntity thirdEntity = new ThirdEntity( 1L, "123" ); | ||
thirdEntity.addAnotherEntity( anotherEntity ); | ||
thirdEntity.addAnotherEntity( anotherEntity2 ); | ||
thirdEntity.addAnotherEntity( anotherEntity3 ); | ||
|
||
session.persist( thirdEntity ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testGetReference(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
ThirdEntity thirdEntity = session.getReference( ThirdEntity.class, 1L ); | ||
for ( AnotherEntity thirdEntityDetail : thirdEntity.getAnotherEntities() ) { | ||
thirdEntityDetail.getParentEntity().getName(); | ||
} | ||
} | ||
); | ||
|
||
scope.inTransaction( | ||
session -> { | ||
ParentEntity parentEntity = session.find( ParentEntity.class, 1L ); | ||
assertThat( parentEntity.getChildren().size() ).isEqualTo( 1 ); | ||
|
||
ParentEntity parentEntity2 = session.find( ParentEntity.class, 2L ); | ||
assertThat( parentEntity2.getChildren().size() ).isEqualTo( 1 ); | ||
|
||
ParentEntity parentEntity3 = session.find( ParentEntity.class, 3L ); | ||
assertThat( parentEntity3.getChildren().size() ).isEqualTo( 0 ); | ||
} | ||
); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); | ||
} | ||
|
||
@Entity(name = "ChildEntity") | ||
public static class ChildEntity { | ||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
public ChildEntity() { | ||
} | ||
|
||
public ChildEntity(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Entity(name = "ParentEntity") | ||
public static class ParentEntity { | ||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@Column(name = "secondid") | ||
private Long secondId; | ||
|
||
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) | ||
@JoinColumns({ | ||
@JoinColumn(name = "parentid", referencedColumnName = "id"), | ||
@JoinColumn(name = "secondid", referencedColumnName = "secondid") | ||
}) | ||
private List<ChildEntity> children; | ||
|
||
public ParentEntity() { | ||
} | ||
|
||
public ParentEntity(Long id, String name, Long secondId) { | ||
this.id = id; | ||
this.name = name; | ||
this.secondId = secondId; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<ChildEntity> getChildren() { | ||
return children; | ||
} | ||
|
||
public void addChild(ChildEntity childEntity) { | ||
if ( this.children == null ) { | ||
this.children = new ArrayList<>(); | ||
} | ||
this.children.add( childEntity ); | ||
} | ||
} | ||
|
||
@Entity(name = "AnotherEntity") | ||
public static class AnotherEntity { | ||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@Column(name = "parentid") | ||
private Long parentId; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "parentid", insertable = false, updatable = false, referencedColumnName = "id") | ||
private ParentEntity parentEntity; | ||
|
||
public AnotherEntity() { | ||
} | ||
|
||
public AnotherEntity(Long id, String name, Long parentId, ParentEntity parentEntity) { | ||
this.id = id; | ||
this.name = name; | ||
this.parentId = parentId; | ||
this.parentEntity = parentEntity; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Long getParentId() { | ||
return parentId; | ||
} | ||
|
||
public ParentEntity getParentEntity() { | ||
return parentEntity; | ||
} | ||
} | ||
|
||
@Entity(name = "ThirdEntity") | ||
public static class ThirdEntity { | ||
@Id | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) | ||
@JoinColumn(name = "thirdEntityHeader", referencedColumnName = "id") | ||
private List<AnotherEntity> anotherEntities; | ||
|
||
public ThirdEntity() { | ||
} | ||
|
||
public ThirdEntity(Long id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public List<AnotherEntity> getAnotherEntities() { | ||
return anotherEntities; | ||
} | ||
|
||
public void addAnotherEntity(AnotherEntity anotherEntity) { | ||
if ( this.anotherEntities == null ) { | ||
this.anotherEntities = new ArrayList<>(); | ||
} | ||
this.anotherEntities.add( anotherEntity ); | ||
} | ||
} | ||
|
||
|
||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this not work?