Skip to content

HHH-18891 fix of an AssertionError when using a NotFound annotation #10208

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

Merged
merged 1 commit into from
May 30, 2025
Merged
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
Expand Up @@ -32,6 +32,9 @@ public JavaType getAssembledJavaType() {
public Object assemble(RowProcessingState rowProcessingState) {
final InitializerData data = initializer.getData( rowProcessingState );
final Initializer.State state = data.getState();
if ( state == Initializer.State.UNINITIALIZED ) {
initializer.resolveKey( data );
}
if ( state == Initializer.State.KEY_RESOLVED ) {
initializer.resolveInstance( data );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.notfound;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import org.hibernate.FetchNotFoundException;
import org.hibernate.annotations.JoinColumnOrFormula;
import org.hibernate.annotations.JoinFormula;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.query.Query;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@JiraKey(value = "HHH-18891")
@DomainModel(
annotatedClasses = {CompositeForeignKeyNotFoundTest.Document.class, CompositeForeignKeyNotFoundTest.DocumentIgnore.class,
CompositeForeignKeyNotFoundTest.DocumentException.class, CompositeForeignKeyNotFoundTest.Person.class})
@SessionFactory
public class CompositeForeignKeyNotFoundTest {

@Test
void hhh18891TestWithNotFoundIgnore(SessionFactoryScope scope) {

// prepare document
scope.inTransaction( session -> {
Query nativeQuery = session.createNativeQuery(
"insert into DocumentIgnore (id,owner) values (123,42)" );
nativeQuery.executeUpdate();
} );

// assert document
scope.inTransaction( session -> {
final DocumentIgnore document = session.find( DocumentIgnore.class, 123 );
assertNotNull( document );
assertEquals( 123, document.id );
assertNull( document.owner );
} );
}

@Test
void hhh18891TestWithNotFoundException(SessionFactoryScope scope) {

// prepare document
scope.inTransaction( session -> {
Query nativeQuery = session.createNativeQuery(
"insert into DocumentException (id,owner) values (123,42)" );
nativeQuery.executeUpdate();
} );

// assert document
scope.inTransaction( session -> {
assertThrows( FetchNotFoundException.class, () ->
session.find( DocumentException.class, 123 ) );
} );
}

@Test
void hhh18891TestWithoutNotFoundAnnotation(SessionFactoryScope scope) {

// prepare document
scope.inTransaction( session -> {
Query nativeQuery = session.createNativeQuery(
"insert into Document (id,owner) values (123,42)" );
nativeQuery.executeUpdate();
} );

// assert document
scope.inTransaction( session -> {
final Document document = session.find( Document.class, 123 );
assertNotNull( document );
assertEquals( 123, document.id );
assertNull( document.owner );
} );
}

@Entity(name = "DocumentIgnore")
public static class DocumentIgnore {

@Id
@GeneratedValue
Long id;

@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false,
updatable = false))
@JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name"))
Person owner;
}

@Entity(name = "DocumentException")
public static class DocumentException {

@Id
@GeneratedValue
Long id;

@ManyToOne
@NotFound(action = NotFoundAction.EXCEPTION)
@JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false,
updatable = false))
@JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name"))
Person owner;
}

@Entity(name = "Document")
public static class Document {

@Id
@GeneratedValue
Long id;

@ManyToOne
@JoinColumnOrFormula(column = @JoinColumn(name = "owner", referencedColumnName = "id", insertable = false,
updatable = false))
@JoinColumnOrFormula(formula = @JoinFormula(value = "'fubar'", referencedColumnName = "name"))
Person owner;
}

@Entity(name = "Person")
public static class Person {

@Id
Long id;

String name;
}

}