Tomáš Müller (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5b50823...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiZDE1MjRiOTVj...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16742?atlOrigin=eyJpIjoiZDE1Mj...
) HHH-16742 (
https://hibernate.atlassian.net/browse/HHH-16742?atlOrigin=eyJpIjoiZDE1Mj...
) ArrayIndexOutOfBoundsException when calling Tuple.getElements() (
https://hibernate.atlassian.net/browse/HHH-16742?atlOrigin=eyJpIjoiZDE1Mj...
)
Change By: Tomáš Müller (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5b50823...
)
We are in the process of migrating an application from Hibernate 4.3 to Hibernate 6.2.
Within our application, we can create custom reports that rely on the aliases provided in
the custom HQL when rendering the table. In Hibernate 4, we used
{{String[] alias = query.getReturnAliases();}}
which no longer exists. So in Hibernate 6, we have started using jakarta.persistence.Tuple
as the result class for the query. However, if the same column is returned two times, with
different aliases, calling Tuple.getElements() throws ArrayIndexOutOfBoundsException.
Here is an example. Imagine a simple entity:
{noformat}@Entity
@Table(name = "test_entity")
public class TestEntity {
private UUID id;
private String name;
private String value;
@Id
@GeneratedValue
@Column(name = "id")
public UUID getId() { return id; }
public void setId(UUID id) { this.id = id; }
@Column(name = "name")
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Column(name = "val")
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}{noformat}
And the following code that runs using the following query
{{select name as Reference, name as Name, value as Value from TestEntity}}
returning the TestEntity. name column two times:
{noformat}// Get a list of tuples
List<Tuple> list = hibSession.createQuery(
"select name as Reference, name as Name, value as Value from TestEntity",
Tuple.class).list();
// Print the returned tuples
for (Tuple tuple: list)
for (TupleElement<?> e: tuple.getElements())
System.out.println(e.getAlias() + ": " + tuple.get(e));{noformat}
Calling tuple.getElements() on line 6 results in the following exception:
{noformat}java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at org.hibernate.sql.results.internal.TupleMetadata.getList(TupleMetadata.java:52)
at org.hibernate.sql.results.internal.TupleImpl.getElements(TupleImpl.java:113)
at org.hibernate.bugs.tuples.TupleTest.testDuplicates(TupleTest.java:67)
...{noformat}
The problem only occurs when the same column is listed two times (in any position). This
can happen as our custom reports are used to provide integration data with other
applications, which may need the same property listed two times under different names.
See the attached files for the whole test.
[^TupleTest.java]
[^TestEntity.java]
(
https://hibernate.atlassian.net/browse/HHH-16742#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16742#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:e03cc87 )