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-18353 Reproducer #403

Open
wants to merge 1 commit 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
20 changes: 20 additions & 0 deletions orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.hibernate.bugs;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Author {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

public Long getId() {
return id;
}

}
35 changes: 35 additions & 0 deletions orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.hibernate.bugs;

import jakarta.persistence.*;

@Entity
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;

@ManyToOne
private Author mainAuthor;

@ManyToOne
private Author contributedAuthor;

public Author getMainAuthor() {
return mainAuthor;
}

public Author getContributedAuthor() {
return contributedAuthor;
}

public Book setMainAuthor(Author value) {
mainAuthor = value;
return this;
}

public Book setContributedAuthor(Author value) {
contributedAuthor = value;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

import jakarta.persistence.TypedQuery;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
Expand All @@ -28,10 +36,45 @@ public void destroy() {
// Entities are auto-discovered, so just add them anywhere on class-path
// Add your tests, using standard JUnit.
@Test
public void hhh123Test() throws Exception {
public void hhh18353Test() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// Do stuff...

/*
You can see first assertion will go through but second will fail.
Only different in queries is that one casts array elements explicitly,
while other tries to infer element types and assure all elements has are
of same type.
*/

Author author1 = entityManager.merge(new Author());
Author author2 = entityManager.merge(new Author());
Book book = entityManager.merge(new Book().setMainAuthor(author1).setContributedAuthor(author2));

Set<Long> expectedResults = Set.of(author1.getId(), author2.getId());

String castedQueryStr = "SELECT ARRAY(CAST(b.mainAuthor.id as Long), CAST(b.contributedAuthor.id as Long)) FROM Book b WHERE b = :book";
TypedQuery<Long[]> castedQuery = entityManager.createQuery(castedQueryStr, Long[].class);
castedQuery.setParameter("book", book);
Long[] castedResults = castedQuery.getSingleResult();

Assert.assertEquals(
"Expect found ids to be ones that we inserted into database",
expectedResults,
Set.of(castedResults)
);

String nonCastedQueryStr = "SELECT ARRAY(b.mainAuthor.id, b.contributedAuthor.id) FROM Book b WHERE b = :book";
TypedQuery<Long[]> nonCastedQuery = entityManager.createQuery(nonCastedQueryStr, Long[].class);
nonCastedQuery.setParameter("book", book);
Long[] nonCastedResults = nonCastedQuery.getSingleResult();

Assert.assertEquals(
"Expect found ids to be ones that we inserted into database",
expectedResults,
Set.of(nonCastedResults)
);

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