Skip to content
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

Hhh 18771 index out of bounds exception by list initializer for list index base #9169

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 @@ -121,10 +121,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,141 @@
/*
* 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;

/**
* @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 -> {
entityManager.find( Person.class, 1L );
} );
}

@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