-
Notifications
You must be signed in to change notification settings - Fork 375
HHH-19119: Reproduction of not found bug for entity graph #473
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
Open
petergphillips
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
petergphillips:pgp-HHH-19119-not-found-entity-graph
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
82 changes: 82 additions & 0 deletions
82
orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ItemWithException.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,82 @@ | ||
package org.hibernate.bugs; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.NamedAttributeNode; | ||
import jakarta.persistence.NamedEntityGraph; | ||
import jakarta.persistence.Table; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.annotations.JoinColumnOrFormula; | ||
import org.hibernate.annotations.JoinColumnsOrFormulas; | ||
import org.hibernate.annotations.JoinFormula; | ||
import org.hibernate.annotations.NotFound; | ||
|
||
import java.util.Objects; | ||
|
||
import static org.hibernate.annotations.NotFoundAction.EXCEPTION; | ||
import static org.hibernate.annotations.NotFoundAction.IGNORE; | ||
|
||
@Entity | ||
@Table(name = "ITEM_WITH_EXCEPTION") | ||
@NamedEntityGraph( | ||
name = "ItemWithException.fetch", | ||
attributeNodes = @NamedAttributeNode("unit") | ||
) | ||
public class ItemWithException { | ||
public ItemWithException() { | ||
} | ||
|
||
public ItemWithException(Long itemId) { | ||
this.itemId = itemId; | ||
} | ||
|
||
public ItemWithException(Long itemId, UnitReferenceCode unit) { | ||
this.itemId = itemId; | ||
this.unit = unit; | ||
} | ||
|
||
@Id | ||
@Column(name = "ITEM_ID") | ||
private Long itemId; | ||
|
||
public Long getItemId() { | ||
return itemId; | ||
} | ||
|
||
public void setItemId(Long itemId) { | ||
this.itemId = itemId; | ||
} | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@NotFound(action = EXCEPTION) | ||
@JoinColumnsOrFormulas(value = { | ||
@JoinColumnOrFormula(formula = @JoinFormula(value = "'" + UnitReferenceCode.DOMAIN + "'", referencedColumnName = "domain")), | ||
@JoinColumnOrFormula(column = @JoinColumn(name = "ITEM_TYPE", referencedColumnName = "code", insertable = false, updatable = false)) | ||
}) | ||
private UnitReferenceCode unit; | ||
|
||
public UnitReferenceCode getUnit() { | ||
return unit; | ||
} | ||
|
||
public void setUnit(UnitReferenceCode unit) { | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; | ||
final ItemWithException that = (ItemWithException) o; | ||
return Objects.equals(getItemId(), that.getItemId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(getItemId()); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ItemWithIgnore.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,81 @@ | ||
package org.hibernate.bugs; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.NamedAttributeNode; | ||
import jakarta.persistence.NamedEntityGraph; | ||
import jakarta.persistence.Table; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.annotations.JoinColumnOrFormula; | ||
import org.hibernate.annotations.JoinColumnsOrFormulas; | ||
import org.hibernate.annotations.JoinFormula; | ||
import org.hibernate.annotations.NotFound; | ||
|
||
import java.util.Objects; | ||
|
||
import static org.hibernate.annotations.NotFoundAction.IGNORE; | ||
|
||
@Entity | ||
@Table(name = "ITEM_WITH_IGNORE") | ||
@NamedEntityGraph( | ||
name = "ItemWithIgnore.fetch", | ||
attributeNodes = @NamedAttributeNode("unit") | ||
) | ||
public class ItemWithIgnore { | ||
public ItemWithIgnore() { | ||
} | ||
|
||
public ItemWithIgnore(Long itemId) { | ||
this.itemId = itemId; | ||
} | ||
|
||
public ItemWithIgnore(Long itemId, UnitReferenceCode unit) { | ||
this.itemId = itemId; | ||
this.unit = unit; | ||
} | ||
|
||
@Id | ||
@Column(name = "ITEM_ID") | ||
private Long itemId; | ||
|
||
public Long getItemId() { | ||
return itemId; | ||
} | ||
|
||
public void setItemId(Long itemId) { | ||
this.itemId = itemId; | ||
} | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@NotFound(action = IGNORE) | ||
@JoinColumnsOrFormulas(value = { | ||
@JoinColumnOrFormula(formula = @JoinFormula(value = "'" + UnitReferenceCode.DOMAIN + "'", referencedColumnName = "domain")), | ||
@JoinColumnOrFormula(column = @JoinColumn(name = "ITEM_TYPE", referencedColumnName = "code", insertable = false, updatable = false)) | ||
}) | ||
private UnitReferenceCode unit; | ||
|
||
public UnitReferenceCode getUnit() { | ||
return unit; | ||
} | ||
|
||
public void setUnit(UnitReferenceCode unit) { | ||
this.unit = unit; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; | ||
final ItemWithIgnore that = (ItemWithIgnore) o; | ||
return Objects.equals(getItemId(), that.getItemId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(getItemId()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.hibernate.bugs; | ||
|
||
import org.hibernate.FetchNotFoundException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -8,6 +9,11 @@ | |
import jakarta.persistence.EntityManagerFactory; | ||
import jakarta.persistence.Persistence; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
/** | ||
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API. | ||
*/ | ||
|
@@ -25,13 +31,35 @@ void destroy() { | |
entityManagerFactory.close(); | ||
} | ||
|
||
// Entities are auto-discovered, so just add them anywhere on class-path | ||
// Add your tests, using standard JUnit. | ||
@Test | ||
void hhh123Test() throws Exception { | ||
void notFoundItemIgnoreUsingFetchOnEntityGraph() { | ||
EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
|
||
entityManager.createNativeQuery("insert into ITEM_WITH_IGNORE(ITEM_ID,ITEM_TYPE) values(12345, 'BOB')").executeUpdate(); | ||
|
||
final var graph = entityManager.getEntityGraph("ItemWithIgnore.fetch"); | ||
final var item = entityManager.find(ItemWithIgnore.class, 12345L, Collections.singletonMap("jakarta.persistence.fetchgraph", graph)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both tests fail here with
|
||
|
||
assertThat(item.getItemId()).isEqualTo(12345L); | ||
assertThat(item.getUnit()).isNull(); | ||
|
||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
||
@Test | ||
void notFoundItemExceptionUsingFetchOnEntityGraph() { | ||
EntityManager entityManager = entityManagerFactory.createEntityManager(); | ||
entityManager.getTransaction().begin(); | ||
// Do stuff... | ||
|
||
entityManager.createNativeQuery("insert into ITEM_WITH_EXCEPTION(ITEM_ID,ITEM_TYPE) values(12346, 'BOB')").executeUpdate(); | ||
|
||
final var graph = entityManager.getEntityGraph("ItemWithException.fetch"); | ||
assertThatThrownBy(() -> { | ||
entityManager.find(ItemWithException.class, 12346L, Collections.singletonMap("jakarta.persistence.fetchgraph", graph)); | ||
}).isInstanceOf(FetchNotFoundException.class); | ||
|
||
entityManager.getTransaction().commit(); | ||
entityManager.close(); | ||
} | ||
|
122 changes: 122 additions & 0 deletions
122
orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ReferenceCode.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,122 @@ | ||
package org.hibernate.bugs; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Convert; | ||
import jakarta.persistence.DiscriminatorColumn; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.IdClass; | ||
import jakarta.persistence.Inheritance; | ||
import org.hibernate.Hibernate; | ||
import org.hibernate.type.YesNoConverter; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
@Entity(name = "REFERENCE_CODES") | ||
@DiscriminatorColumn(name = "domain") | ||
@Inheritance | ||
@IdClass(ReferenceCode.Pk.class) | ||
public abstract class ReferenceCode implements Serializable { | ||
|
||
public ReferenceCode() { | ||
} | ||
|
||
public ReferenceCode(String domain, String code) { | ||
this.domain = domain; | ||
this.code = code; | ||
} | ||
|
||
public String getDomain() { | ||
return this.domain; | ||
} | ||
|
||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
public String toString() { | ||
return "ReferenceCode(domain=" + this.getDomain() + ", code=" + this.getCode() + ")"; | ||
} | ||
|
||
public static class Pk implements Serializable { | ||
private String domain; | ||
private String code; | ||
|
||
public Pk(String domain, String code) { | ||
this.domain = domain; | ||
this.code = code; | ||
} | ||
|
||
public Pk() { | ||
} | ||
|
||
public String getDomain() { | ||
return this.domain; | ||
} | ||
|
||
public String getCode() { | ||
return this.code; | ||
} | ||
|
||
public boolean equals(final Object o) { | ||
if (o == this) return true; | ||
if (!(o instanceof Pk)) return false; | ||
final Pk other = (Pk) o; | ||
if (!other.canEqual((Object) this)) return false; | ||
final Object this$domain = this.getDomain(); | ||
final Object other$domain = other.getDomain(); | ||
if (this$domain == null ? other$domain != null : !this$domain.equals(other$domain)) return false; | ||
final Object this$code = this.getCode(); | ||
final Object other$code = other.getCode(); | ||
if (this$code == null ? other$code != null : !this$code.equals(other$code)) return false; | ||
return true; | ||
} | ||
|
||
protected boolean canEqual(final Object other) { | ||
return other instanceof Pk; | ||
} | ||
|
||
public int hashCode() { | ||
final int PRIME = 59; | ||
int result = 1; | ||
final Object $domain = this.getDomain(); | ||
result = result * PRIME + ($domain == null ? 43 : $domain.hashCode()); | ||
final Object $code = this.getCode(); | ||
result = result * PRIME + ($code == null ? 43 : $code.hashCode()); | ||
return result; | ||
} | ||
|
||
public String toString() { | ||
return "ReferenceCode.Pk(domain=" + this.getDomain() + ", code=" + this.getCode() + ")"; | ||
} | ||
} | ||
|
||
@Id | ||
@Column(insertable = false, updatable = false) | ||
private String domain; | ||
|
||
@Id | ||
private String code; | ||
|
||
public static String getCodeOrNull(final ReferenceCode referenceCode) { | ||
return referenceCode != null ? referenceCode.getCode() : null; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) return true; | ||
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; | ||
final ReferenceCode that = (ReferenceCode) o; | ||
|
||
if (!Objects.equals(getDomain(), that.getDomain())) return false; | ||
return Objects.equals(getCode(), that.getCode()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hashCode(getDomain()); | ||
result = 31 * result + (Objects.hashCode(getCode())); | ||
return result; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/UnitReferenceCode.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,17 @@ | ||
package org.hibernate.bugs; | ||
|
||
import jakarta.persistence.DiscriminatorValue; | ||
import jakarta.persistence.Entity; | ||
|
||
@Entity | ||
@DiscriminatorValue(UnitReferenceCode.DOMAIN) | ||
public class UnitReferenceCode extends ReferenceCode { | ||
public UnitReferenceCode() { | ||
} | ||
|
||
public UnitReferenceCode(String code) { | ||
super(DOMAIN, code); | ||
} | ||
|
||
public static final String DOMAIN = "UNIT"; | ||
} |
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.
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.
just to demonstrate it fails on latest version