Skip to content

HHH-18891: Reproduction of not found bug #454

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.hibernate.bugs;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
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")
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
@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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.hibernate.bugs;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
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")
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
@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());
}
}
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;
Expand All @@ -8,6 +9,9 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

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.
*/
Expand All @@ -25,13 +29,32 @@ 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 notFoundItemWithIgnore() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();

entityManager.createNativeQuery("insert into ITEM_WITH_IGNORE(ITEM_ID,ITEM_TYPE) values(12345, 'BOB')").executeUpdate();

final var item = entityManager.find(ItemWithIgnore.class, 12345L);
assertThat(item.getItemId()).isEqualTo(12345L);
assertThat(item.getUnit()).isNull();

entityManager.getTransaction().commit();
entityManager.close();
}

@Test
void notFoundItemWithException() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// Do stuff...

entityManager.createNativeQuery("insert into ITEM_WITH_EXCEPTION(ITEM_ID,ITEM_TYPE) values(12345, 'BOB')").executeUpdate();

assertThatThrownBy(() -> entityManager.find(ItemWithException.class, 12345L))
.isInstanceOf(FetchNotFoundException.class)
.hasMessage("Entity `org.hibernate.bugs.UnitReferenceCode` with identifier value `ReferenceCode.Pk(domain=UNIT, code=BOB)` does not exist");

entityManager.getTransaction().commit();
entityManager.close();
}
Expand Down
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;
}
}
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";
}