Skip to content

[6.6] HHH-18771 + HHH-18876 Backports #10270

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 3 commits into from
Jun 11, 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 @@ -132,10 +132,13 @@ protected void resolveInstanceSubInitializers(ImmediateCollectionInitializerData
final Initializer<?> initializer = elementAssembler.getInitializer();
if ( initializer != null ) {
final RowProcessingState rowProcessingState = data.getRowProcessingState();
final Integer index = listIndexAssembler.assemble( rowProcessingState );
Integer index = listIndexAssembler.assemble( rowProcessingState );
if ( index != null ) {
final PersistentArrayHolder<?> arrayHolder = getCollectionInstance( data );
assert arrayHolder != null;
if ( indexBase != 0 ) {
index -= indexBase;
}
initializer.resolveInstance( Array.get( arrayHolder.getArray(), index ), rowProcessingState );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ protected void resolveInstanceSubInitializers(ImmediateCollectionInitializerData
final Initializer<?> initializer = elementAssembler.getInitializer();
if ( initializer != null ) {
final RowProcessingState rowProcessingState = data.getRowProcessingState();
final Integer index = listIndexAssembler.assemble( rowProcessingState );
Integer index = listIndexAssembler.assemble( rowProcessingState );
if ( index != null ) {
final PersistentList<?> list = getCollectionInstance( data );
assert list != null;
if ( listIndexBase != 0 ) {
index -= listIndexBase;
}
initializer.resolveInstance( list.get( index ), rowProcessingState );
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.collections;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderColumn;
import org.hibernate.annotations.ListIndexBase;
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;

@JiraKey("HHH-18876")
@DomainModel(
annotatedClasses = {
OrderColumnListIndexArrayInitializerTest.Person.class,
OrderColumnListIndexArrayInitializerTest.Phone.class,
}
)
@SessionFactory
class OrderColumnListIndexArrayInitializerTest {

@Test
void hhh18876Test(SessionFactoryScope scope) {

// prepare data
scope.inTransaction( session -> {

// person
Person person = new Person();
person.id = 1L;
session.persist( person );

// add phone
Phone phone = new Phone();
phone.id = 1L;
phone.person = person;

person.phones = new Phone[1];
person.phones[0] = phone;

// add children
Person children = new Person();
children.id = 2L;
children.mother = person;

person.children = new Person[1];
person.children[0] = children;
} );

// load and assert
scope.inTransaction( session -> {
Person person = session.createSelectionQuery( "select p from Person p where id=1", Person.class )
.getSingleResult();
assertNotNull( person );
assertEquals( 1, person.phones.length );
assertNotNull( person.phones[0] );
assertEquals( 1, person.children.length );
assertEquals( person, person.children[0].mother );
} );
}

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

@Id
Long id;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
@ListIndexBase(100)
Phone[] phones;

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "parent_child_relationships", joinColumns = @JoinColumn(name = "parent_id"),
inverseJoinColumns = @JoinColumn(name = "child_id"))
@OrderColumn(name = "pos")
@ListIndexBase(200)
Person[] children;

@ManyToOne
@JoinColumn(name = "mother_id")
Person mother;
}

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

@Id
Long id;

@ManyToOne
Person person;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.mapping.collections;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.ListIndexBase;
import org.hibernate.orm.test.jpa.BaseEntityManagerFunctionalTestCase;

import org.junit.Test;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderColumn;

import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Selaron
*/
public class OrderColumnListIndexHHH18771ListInitializerTest extends BaseEntityManagerFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Person.class,
Phone.class,
};
}

@Test
public void testLifecycle() {
doInJPA( this::entityManagerFactory, entityManager -> {
Person person = new Person( 1L );
entityManager.persist( person );
person.addPhone( new Phone( 1L ) );
person.getChildren().add( new Person( 2L ) );
person.getChildren().get( 0 ).setMother( person );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
Person person = entityManager.find( Person.class, 1L );
assertNotNull( person );
assertEquals( 1, person.getPhones().size() );
assertNotNull( person.getPhones().get( 0 ) );
assertEquals( 1, person.getChildren().size() );
assertEquals( person, person.getChildren().get( 0 ).getMother() );
} );
}

@Entity(name = "Person")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public static class Person {

@Id
private Long id;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "person", cascade = CascadeType.ALL)
@OrderColumn(name = "order_id")
@ListIndexBase(100)
private List<Phone> phones = new ArrayList<>();

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "parent_child_relationships", joinColumns = @JoinColumn(name = "parent_id"), inverseJoinColumns = @JoinColumn(name = "child_id"))
@OrderColumn(name = "pos")
@ListIndexBase(200)
private List<Person> children = new ArrayList<>();

@ManyToOne
@JoinColumn(name = "mother_id")
private Person mother;

public Person() {
}

public Person(Long id) {
this.id = id;
}

public List<Phone> getPhones() {
return phones;
}

public void addPhone(Phone phone) {
phones.add( phone );
phone.setPerson( this );
}

public List<Person> getChildren() {
return children;
}

public Person getMother() {
return mother;
}

public void setMother(Person mother) {
this.mother = mother;
}

public void removePhone(Phone phone) {
phones.remove( phone );
phone.setPerson( null );
}
}

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

@Id
private Long id;

@ManyToOne
private Person person;

public Phone() {
}

public Phone(Long id) {
this.id = id;
}

public Long getId() {
return id;
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}
}
}
Loading